2019-11-23 20:27:39 +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
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
2019-11-23 20:27:39 +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)
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 */
|
2019-11-23 20:27:39 +08:00
|
|
|
class CC_DLL Physics3DComponent : public cocos2d::Component
|
|
|
|
{
|
|
|
|
friend class Physics3DWorld;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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 cocos2d::Vec3& translateInPhysics = cocos2d::Vec3::ZERO,
|
|
|
|
const cocos2d::Quaternion& rotInPhsyics = cocos2d::Quaternion::ZERO);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* set Physics object to the component
|
|
|
|
*/
|
|
|
|
void setPhysics3DObject(Physics3DObject* physicsObj);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* get physics object
|
|
|
|
*/
|
|
|
|
Physics3DObject* getPhysics3DObject() const { return _physics3DObj; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* set it enable or not
|
|
|
|
*/
|
|
|
|
virtual void setEnabled(bool b) override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
virtual void onEnter() override;
|
|
|
|
virtual void onExit() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* add this component to physics world, called by scene
|
|
|
|
*/
|
|
|
|
void addToPhysicsWorld(Physics3DWorld* world);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* The node's transform in physics object space
|
|
|
|
*/
|
|
|
|
void setTransformInPhysics(const cocos2d::Vec3& translateInPhysics, const cocos2d::Quaternion& rotInPhsyics);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +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
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* synchronize node transformation to physics
|
|
|
|
*/
|
|
|
|
void syncNodeToPhysics();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
/**
|
|
|
|
* synchronize physics transformation to node
|
|
|
|
*/
|
|
|
|
void syncPhysicsToNode();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-03-18 21:46:07 +08:00
|
|
|
Physics3DComponent();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
protected:
|
|
|
|
void preSimulate();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
void postSimulate();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
cocos2d::Mat4 _transformInPhysics; // transform in physics space
|
|
|
|
cocos2d::Mat4 _invTransformInPhysics;
|
|
|
|
|
|
|
|
Physics3DObject* _physics3DObj;
|
|
|
|
PhysicsSyncFlag _syncFlag;
|
2019-11-23 20:27:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// end of 3d group
|
|
|
|
/// @}
|
|
|
|
NS_CC_END
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# endif // CC_ENABLE_BULLET_INTEGRATION
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // CC_USE_3D_PHYSICS
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#endif // __PHYSICS_3D_COMPONENT_H__
|