CCPhysics3DObject.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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_OBJECT_H__
  21. #define __PHYSICS_3D_OBJECT_H__
  22. #include "math/CCMath.h"
  23. #include "base/CCRef.h"
  24. #include "base/ccConfig.h"
  25. #include <vector>
  26. #if CC_USE_3D_PHYSICS
  27. #if (CC_ENABLE_BULLET_INTEGRATION)
  28. class btCollisionShape;
  29. class btRigidBody;
  30. class btPersistentManifold;
  31. class btGhostObject;
  32. NS_CC_BEGIN
  33. /**
  34. * @addtogroup _3d
  35. * @{
  36. */
  37. class Physics3DShape;
  38. class Physics3DWorld;
  39. class Physics3DConstraint;
  40. class Physics3DObject;
  41. /**
  42. * @brief The collision information of Physics3DObject.
  43. */
  44. struct CC_DLL Physics3DCollisionInfo
  45. {
  46. struct CollisionPoint
  47. {
  48. Vec3 localPositionOnA;
  49. Vec3 worldPositionOnA;
  50. Vec3 localPositionOnB;
  51. Vec3 worldPositionOnB;
  52. Vec3 worldNormalOnB;
  53. };
  54. Physics3DObject *objA;
  55. Physics3DObject *objB;
  56. std::vector<CollisionPoint> collisionPointList;
  57. };
  58. /**
  59. * @brief Inherit from Ref, base class
  60. */
  61. class CC_DLL Physics3DObject : public Ref
  62. {
  63. public:
  64. typedef std::function<void(const Physics3DCollisionInfo &ci)> CollisionCallbackFunc;
  65. enum class PhysicsObjType
  66. {
  67. UNKNOWN = 0,
  68. RIGID_BODY,
  69. COLLIDER,
  70. };
  71. /** Get the Physics3DObject Type. */
  72. virtual PhysicsObjType getObjType() const { return _type; }
  73. /** Set the user data. */
  74. void setUserData(void* userData) { _userData = userData; }
  75. /** Get the user data. */
  76. void* getUserData() const { return _userData; }
  77. /** Internal method. Set the pointer of Physics3DWorld. */
  78. void setPhysicsWorld(Physics3DWorld* world) { _physicsWorld = world; };
  79. /** Get the pointer of Physics3DWorld. */
  80. Physics3DWorld* getPhysicsWorld() const { return _physicsWorld; }
  81. /** Get the world matrix of Physics3DObject. */
  82. virtual cocos2d::Mat4 getWorldTransform() const = 0;
  83. /** Set the collision callback function. */
  84. void setCollisionCallback(const CollisionCallbackFunc &func) { _collisionCallbackFunc = func; };
  85. /** Get the collision callback function. */
  86. const CollisionCallbackFunc& getCollisionCallback() const { return _collisionCallbackFunc; }
  87. /** Check has collision callback function. */
  88. bool needCollisionCallback() { return _collisionCallbackFunc != nullptr; };
  89. /** Set the mask of Physics3DObject. */
  90. void setMask(unsigned int mask) { _mask = mask; };
  91. /** Get the mask of Physics3DObject. */
  92. unsigned int getMask() const { return _mask; };
  93. CC_CONSTRUCTOR_ACCESS:
  94. Physics3DObject()
  95. : _type(PhysicsObjType::UNKNOWN)
  96. , _userData(nullptr)
  97. , _isEnabled(true)
  98. , _physicsWorld(nullptr)
  99. , _mask(-1)
  100. {
  101. }
  102. virtual ~Physics3DObject(){}
  103. protected:
  104. bool _isEnabled;
  105. PhysicsObjType _type;
  106. void* _userData;
  107. Physics3DWorld* _physicsWorld;
  108. CollisionCallbackFunc _collisionCallbackFunc;
  109. unsigned int _mask;
  110. };
  111. /**
  112. * @brief The description of Physics3DRigidBody.
  113. */
  114. struct CC_DLL Physics3DRigidBodyDes
  115. {
  116. float mass; //Note: mass equals zero means static, default 0
  117. cocos2d::Vec3 localInertia; //default (0, 0, 0)
  118. Physics3DShape* shape;
  119. cocos2d::Mat4 originalTransform;
  120. bool disableSleep; //it is always active if disabled
  121. Physics3DRigidBodyDes()
  122. : mass(0.f)
  123. , localInertia(0.f, 0.f, 0.f)
  124. , shape(nullptr)
  125. , disableSleep(false)
  126. {
  127. }
  128. };
  129. /**
  130. * @brief Inherit from Physics3DObject, the main class for rigid body objects
  131. */
  132. class CC_DLL Physics3DRigidBody : public Physics3DObject
  133. {
  134. friend class Physics3DWorld;
  135. public:
  136. /**
  137. * Creates a Physics3DRigidBody with Physics3DRigidBody.
  138. *
  139. * @return An autoreleased Physics3DRigidBody object.
  140. */
  141. static Physics3DRigidBody* create(Physics3DRigidBodyDes* info);
  142. /** Get the pointer of btRigidBody. */
  143. btRigidBody* getRigidBody() const { return _btRigidBody; }
  144. /**
  145. * Apply a force.
  146. *
  147. * @param force the value of the force
  148. * @param rel_pos the position of the force
  149. */
  150. void applyForce(const cocos2d::Vec3& force, const cocos2d::Vec3& rel_pos);
  151. /**
  152. * Apply a central force.
  153. *
  154. * @param force the value of the force
  155. */
  156. void applyCentralForce(const cocos2d::Vec3& force);
  157. /**
  158. * Apply a central impulse.
  159. *
  160. * @param impulse the value of the impulse
  161. */
  162. void applyCentralImpulse(const cocos2d::Vec3& impulse);
  163. /**
  164. * Apply a torque.
  165. *
  166. * @param torque the value of the torque
  167. */
  168. void applyTorque(const cocos2d::Vec3& torque);
  169. /**
  170. * Apply a torque impulse.
  171. *
  172. * @param torque the value of the torque
  173. */
  174. void applyTorqueImpulse(const cocos2d::Vec3& torque);
  175. /**
  176. * Apply a impulse.
  177. *
  178. * @param impulse the value of the impulse
  179. * @param rel_pos the position of the impulse
  180. */
  181. void applyImpulse(const cocos2d::Vec3& impulse, const cocos2d::Vec3& rel_pos);
  182. /** Damps the velocity, using the given linearDamping and angularDamping. */
  183. void applyDamping(float timeStep);
  184. /** Set the linear velocity. */
  185. void setLinearVelocity(const cocos2d::Vec3& lin_vel);
  186. /** Get the linear velocity. */
  187. cocos2d::Vec3 getLinearVelocity() const;
  188. /** Set the linear factor. */
  189. void setLinearFactor(const cocos2d::Vec3& linearFactor);
  190. /** Get the linear factor. */
  191. cocos2d::Vec3 getLinearFactor() const;
  192. /** Set the angular factor. */
  193. void setAngularFactor(const cocos2d::Vec3& angFac);
  194. /** Set the angular factor, use unified factor. */
  195. void setAngularFactor(float angFac);
  196. /** Get the angular factor. */
  197. cocos2d::Vec3 getAngularFactor() const;
  198. /** Set the angular velocity. */
  199. void setAngularVelocity(const cocos2d::Vec3& ang_vel);
  200. /** Get the angular velocity. */
  201. cocos2d::Vec3 getAngularVelocity() const;
  202. /** Set the center of mass. */
  203. void setCenterOfMassTransform(const cocos2d::Mat4& xform);
  204. /** Get the center of mass. */
  205. cocos2d::Mat4 getCenterOfMassTransform() const;
  206. /** Set linear damping and angular damping. */
  207. void setDamping(float lin_damping, float ang_damping);
  208. /** Get linear damping. */
  209. float getLinearDamping() const;
  210. /** Get angular damping. */
  211. float getAngularDamping() const;
  212. /** Set the acceleration. */
  213. void setGravity(const cocos2d::Vec3& acceleration);
  214. /** Get the acceleration. */
  215. cocos2d::Vec3 getGravity() const;
  216. /** Set the inverse of local inertia. */
  217. void setInvInertiaDiagLocal(const cocos2d::Vec3& diagInvInertia);
  218. /** Get the inverse of local inertia. */
  219. cocos2d::Vec3 getInvInertiaDiagLocal() const;
  220. /** Set mass and inertia. */
  221. void setMassProps(float mass, const cocos2d::Vec3& inertia);
  222. /** Get inverse of mass. */
  223. float getInvMass() const;
  224. /** Get total force. */
  225. cocos2d::Vec3 getTotalForce() const;
  226. /** Get total torque. */
  227. cocos2d::Vec3 getTotalTorque() const;
  228. /** Set restitution. */
  229. void setRestitution(float rest);
  230. /** Get restitution. */
  231. float getRestitution() const;
  232. /** Set friction. */
  233. void setFriction(float frict);
  234. /** Get friction. */
  235. float getFriction() const;
  236. /** Set rolling friction. */
  237. void setRollingFriction(float frict);
  238. /** Get rolling friction. */
  239. float getRollingFriction() const;
  240. /** Set hit friction. */
  241. void setHitFraction(float hitFraction);
  242. /** Get hit friction. */
  243. float getHitFraction() const;
  244. /** Set motion threshold, don't do continuous collision detection if the motion (in one step) is less then ccdMotionThreshold */
  245. void setCcdMotionThreshold(float ccdMotionThreshold);
  246. /** Get motion threshold. */
  247. float getCcdMotionThreshold() const;
  248. /** Set swept sphere radius. */
  249. void setCcdSweptSphereRadius(float radius);
  250. /** Get swept sphere radius. */
  251. float getCcdSweptSphereRadius() const;
  252. /** Set kinematic object. */
  253. void setKinematic(bool kinematic);
  254. /** Check rigid body is kinematic object. */
  255. bool isKinematic() const;
  256. /** override. */
  257. virtual cocos2d::Mat4 getWorldTransform() const override;
  258. /** Get constraint by index. */
  259. Physics3DConstraint* getConstraint(unsigned int idx) const;
  260. /** Get the total number of constraints. */
  261. unsigned int getConstraintCount() const;
  262. /** Active or inactive. */
  263. void setActive(bool active);
  264. CC_CONSTRUCTOR_ACCESS:
  265. Physics3DRigidBody();
  266. virtual ~Physics3DRigidBody();
  267. bool init(Physics3DRigidBodyDes* info);
  268. void addConstraint(Physics3DConstraint *constraint);
  269. void removeConstraint(Physics3DConstraint *constraint);
  270. void removeConstraint(unsigned int idx);
  271. protected:
  272. btRigidBody* _btRigidBody;
  273. Physics3DShape *_physics3DShape;
  274. std::vector<Physics3DConstraint *> _constraintList;
  275. };
  276. /**
  277. * @brief The description of Physics3DCollider.
  278. */
  279. struct CC_DLL Physics3DColliderDes
  280. {
  281. /**shape pointer*/
  282. Physics3DShape* shape;
  283. /**original world Transform*/
  284. cocos2d::Mat4 originalTransform;
  285. /**Is collider a trigger?*/
  286. bool isTrigger;
  287. /**the friction*/
  288. float friction;
  289. /**the rolling friction*/
  290. float rollingFriction;
  291. /**the restitution*/
  292. float restitution;
  293. /**the hit fraction*/
  294. float hitFraction;
  295. /**the swept sphere radius*/
  296. float ccdSweptSphereRadius;
  297. /**the motion threshold*/
  298. float ccdMotionThreshold;
  299. Physics3DColliderDes()
  300. : shape(nullptr)
  301. , isTrigger(false)
  302. , friction(0.5f)
  303. , rollingFriction(0.0f)
  304. , restitution(0.0f)
  305. , hitFraction(1.0f)
  306. , ccdSweptSphereRadius(0.0f)
  307. , ccdMotionThreshold(0.0f)
  308. {
  309. }
  310. };
  311. /**
  312. * @brief Inherit from Physics3DObject, the main class for Colliders.
  313. */
  314. class CC_DLL Physics3DCollider : public Physics3DObject
  315. {
  316. public:
  317. /**
  318. * Creates a Physics3DCollider with Physics3DColliderDes.
  319. *
  320. * @return An autoreleased Physics3DCollider object.
  321. */
  322. static Physics3DCollider* create(Physics3DColliderDes *info);
  323. /** Get the pointer of btGhostObject.
  324. * @return The pointer of btGhostObject.
  325. */
  326. btGhostObject* getGhostObject() const { return _btGhostObject; }
  327. /** Set trigger.
  328. * @param isTrigger Is a trigger.
  329. */
  330. void setTrigger(bool isTrigger);
  331. /** Check is a trigger.
  332. * @return Is a trigger.
  333. */
  334. bool isTrigger() const;
  335. /** Set restitution.
  336. * @param rest The restitution.
  337. */
  338. void setRestitution(float rest);
  339. /** Get restitution.
  340. * @return The restitution.
  341. */
  342. float getRestitution() const;
  343. /** Set friction.
  344. * @param frict The friction.
  345. */
  346. void setFriction(float frict);
  347. /** Get friction.
  348. * @return The friction.
  349. */
  350. float getFriction() const;
  351. /** Set rolling friction.
  352. * @param frict The rolling friction.
  353. */
  354. void setRollingFriction(float frict);
  355. /** Get rolling friction.
  356. * @return The rolling friction.
  357. */
  358. float getRollingFriction() const;
  359. /** Set hit friction.
  360. * @param hitFraction The hit friction.
  361. */
  362. void setHitFraction(float hitFraction);
  363. /** Get hit friction.
  364. * @return The hit friction.
  365. */
  366. float getHitFraction() const;
  367. /** Set motion threshold, don't do continuous collision detection if the motion (in one step) is less then ccdMotionThreshold.
  368. * @param ccdMotionThreshold The motion threshold.
  369. */
  370. void setCcdMotionThreshold(float ccdMotionThreshold);
  371. /** Get motion threshold.
  372. * @return The motion threshold.
  373. */
  374. float getCcdMotionThreshold() const;
  375. /** Set swept sphere radius.
  376. * @param radius The swept sphere radius.
  377. */
  378. void setCcdSweptSphereRadius(float radius);
  379. /** Get swept sphere radius.
  380. * @return The swept sphere radius.
  381. */
  382. float getCcdSweptSphereRadius() const;
  383. /** override. */
  384. virtual cocos2d::Mat4 getWorldTransform() const;
  385. /** Set a callback when trigger enter. */
  386. std::function<void(Physics3DObject *otherObject)> onTriggerEnter;
  387. /** Set a callback when trigger exit. */
  388. std::function<void(Physics3DObject *otherObject)> onTriggerExit;
  389. CC_CONSTRUCTOR_ACCESS :
  390. Physics3DCollider();
  391. virtual ~Physics3DCollider();
  392. bool init(Physics3DColliderDes *info);
  393. protected:
  394. btGhostObject *_btGhostObject;
  395. Physics3DShape *_physics3DShape;
  396. };
  397. // end of 3d group
  398. /// @}
  399. NS_CC_END
  400. #endif // CC_ENABLE_BULLET_INTEGRATION
  401. #endif //CC_USE_3D_PHYSICS
  402. #endif // __PHYSICS_3D_OBJECT_H__