axmol/core/physics3d/CCPhysics3DComponent.h

151 lines
4.7 KiB
C
Raw Normal View History

2015-05-08 15:49:33 +08:00
/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-25 10:04:45 +08:00
https://axis-project.github.io/
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __PHYSICS_3D_COMPONENT_H__
#define __PHYSICS_3D_COMPONENT_H__
#include "base/ccConfig.h"
#include "math/CCMath.h"
#include "2d/CCComponent.h"
#if CC_USE_3D_PHYSICS
2021-12-25 10:04:45 +08:00
# if (CC_ENABLE_BULLET_INTEGRATION)
2015-05-08 15:49:33 +08:00
NS_AX_BEGIN
2015-05-08 15:49:33 +08:00
/**
* @addtogroup _3d
* @{
*/
class Physics3DObject;
class Physics3DWorld;
2021-12-25 10:04:45 +08:00
/** @brief Physics3DComponent: A component with 3D physics, you can add a rigid body to it, and then add this component
* to a node, the node will move and rotate with this rigid body */
class CC_DLL Physics3DComponent : public axis::Component
2015-05-08 15:49:33 +08:00
{
friend class Physics3DWorld;
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
public:
enum class PhysicsSyncFlag
{
2021-12-25 10:04:45 +08:00
NONE = 0,
NODE_TO_PHYSICS = 1, // align node transform to the physics
PHYSICS_TO_NODE = 2, // align physics transform to the node
NODE_AND_NODE =
NODE_TO_PHYSICS | PHYSICS_TO_NODE, // pre simulation, align the physics object to the node and align the
// node transform according to physics object after simulation
2015-05-08 15:49:33 +08:00
};
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
CREATE_FUNC(Physics3DComponent);
virtual ~Physics3DComponent();
virtual bool init() override;
/**
* create Physics3DComponent
* @param physicsObj pointer to a Physics object contain in the component
* @param translateInPhysics offset that the owner node in the physics object's space
* @param rotInPhsyics offset rotation that the owner node in the physics object's space
* @return created Physics3DComponent
*/
2021-12-25 10:04:45 +08:00
static Physics3DComponent* create(Physics3DObject* physicsObj,
const axis::Vec3& translateInPhysics = axis::Vec3::ZERO,
const axis::Quaternion& rotInPhsyics = axis::Quaternion::ZERO);
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
* set Physics object to the component
*/
void setPhysics3DObject(Physics3DObject* physicsObj);
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
* get physics object
*/
Physics3DObject* getPhysics3DObject() const { return _physics3DObj; }
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
* get the component name, it is used to find whether it is Physics3DComponent
*/
static std::string& getPhysics3DComponentName();
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
* set it enable or not
*/
virtual void setEnabled(bool b) override;
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
virtual void onEnter() override;
virtual void onExit() override;
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
* add this component to physics world, called by scene
*/
void addToPhysicsWorld(Physics3DWorld* world);
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
* The node's transform in physics object space
*/
void setTransformInPhysics(const axis::Vec3& translateInPhysics, const axis::Quaternion& rotInPhsyics);
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
* synchronization between node and physics is time consuming, you can skip some synchronization using this function
*/
void setSyncFlag(PhysicsSyncFlag syncFlag);
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
2015-06-04 12:04:41 +08:00
* synchronize node transformation to physics
2015-05-08 15:49:33 +08:00
*/
2015-06-04 12:04:41 +08:00
void syncNodeToPhysics();
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
/**
2015-06-04 12:04:41 +08:00
* synchronize physics transformation to node
2015-05-08 15:49:33 +08:00
*/
2015-06-04 12:04:41 +08:00
void syncPhysicsToNode();
2021-12-25 10:04:45 +08:00
Physics3DComponent();
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
protected:
void preSimulate();
2021-12-25 10:04:45 +08:00
2015-05-08 15:49:33 +08:00
void postSimulate();
2021-12-25 10:04:45 +08:00
axis::Mat4 _transformInPhysics; // transform in physics space
axis::Mat4 _invTransformInPhysics;
2021-12-25 10:04:45 +08:00
Physics3DObject* _physics3DObj;
PhysicsSyncFlag _syncFlag;
2015-05-08 15:49:33 +08:00
};
// end of 3d group
/// @}
NS_AX_END
2015-05-08 15:49:33 +08:00
2021-12-25 10:04:45 +08:00
# endif // CC_ENABLE_BULLET_INTEGRATION
2015-05-08 15:49:33 +08:00
2021-12-25 10:04:45 +08:00
#endif // CC_USE_3D_PHYSICS
2015-05-08 15:49:33 +08:00
2021-12-25 10:04:45 +08:00
#endif // __PHYSICS_3D_COMPONENT_H__