CCPhysics3DComponent.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /****************************************************************************
  2. Copyright (c) 2015-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #ifndef __PHYSICS_3D_COMPONENT_H__
  21. #define __PHYSICS_3D_COMPONENT_H__
  22. #include "base/ccConfig.h"
  23. #include "math/CCMath.h"
  24. #include "2d/CCComponent.h"
  25. #if CC_USE_3D_PHYSICS
  26. #if (CC_ENABLE_BULLET_INTEGRATION)
  27. NS_CC_BEGIN
  28. /**
  29. * @addtogroup _3d
  30. * @{
  31. */
  32. class Physics3DObject;
  33. class Physics3DWorld;
  34. /** @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 */
  35. class CC_DLL Physics3DComponent : public cocos2d::Component
  36. {
  37. friend class Physics3DWorld;
  38. public:
  39. enum class PhysicsSyncFlag
  40. {
  41. NONE = 0,
  42. NODE_TO_PHYSICS = 1, //align node transform to the physics
  43. PHYSICS_TO_NODE = 2, // align physics transform to the node
  44. 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
  45. };
  46. CREATE_FUNC(Physics3DComponent);
  47. virtual ~Physics3DComponent();
  48. virtual bool init() override;
  49. /**
  50. * create Physics3DComponent
  51. * @param physicsObj pointer to a Physics object contain in the component
  52. * @param translateInPhysics offset that the owner node in the physics object's space
  53. * @param rotInPhsyics offset rotation that the owner node in the physics object's space
  54. * @return created Physics3DComponent
  55. */
  56. static Physics3DComponent* create(Physics3DObject* physicsObj, const cocos2d::Vec3& translateInPhysics = cocos2d::Vec3::ZERO, const cocos2d::Quaternion& rotInPhsyics = cocos2d::Quaternion::ZERO);
  57. /**
  58. * set Physics object to the component
  59. */
  60. void setPhysics3DObject(Physics3DObject* physicsObj);
  61. /**
  62. * get physics object
  63. */
  64. Physics3DObject* getPhysics3DObject() const { return _physics3DObj; }
  65. /**
  66. * get the component name, it is used to find whether it is Physics3DComponent
  67. */
  68. static std::string& getPhysics3DComponentName();
  69. /**
  70. * set it enable or not
  71. */
  72. virtual void setEnabled(bool b) override;
  73. virtual void onEnter() override;
  74. virtual void onExit() override;
  75. /**
  76. * add this component to physics world, called by scene
  77. */
  78. void addToPhysicsWorld(Physics3DWorld* world);
  79. /**
  80. * The node's transform in physics object space
  81. */
  82. void setTransformInPhysics(const cocos2d::Vec3& translateInPhysics, const cocos2d::Quaternion& rotInPhsyics);
  83. /**
  84. * synchronization between node and physics is time consuming, you can skip some synchronization using this function
  85. */
  86. void setSyncFlag(PhysicsSyncFlag syncFlag);
  87. /**
  88. * synchronize node transformation to physics
  89. */
  90. void syncNodeToPhysics();
  91. /**
  92. * synchronize physics transformation to node
  93. */
  94. void syncPhysicsToNode();
  95. CC_CONSTRUCTOR_ACCESS:
  96. Physics3DComponent();
  97. protected:
  98. void preSimulate();
  99. void postSimulate();
  100. cocos2d::Mat4 _transformInPhysics; //transform in physics space
  101. cocos2d::Mat4 _invTransformInPhysics;
  102. Physics3DObject* _physics3DObj;
  103. PhysicsSyncFlag _syncFlag;
  104. };
  105. // end of 3d group
  106. /// @}
  107. NS_CC_END
  108. #endif // CC_ENABLE_BULLET_INTEGRATION
  109. #endif //CC_USE_3D_PHYSICS
  110. #endif // __PHYSICS_3D_COMPONENT_H__