CCPhysicsJoint.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CCPHYSICS_JOINT_H__
  22. #define __CCPHYSICS_JOINT_H__
  23. #include "base/ccConfig.h"
  24. #if CC_USE_PHYSICS
  25. #include "base/CCRef.h"
  26. #include "math/CCGeometry.h"
  27. struct cpConstraint;
  28. NS_CC_BEGIN
  29. class Node;
  30. class PhysicsBody;
  31. class PhysicsWorld;
  32. /**
  33. * @addtogroup physics
  34. * @{
  35. * @addtogroup physics_2d
  36. * @{
  37. */
  38. /**
  39. * @brief An PhysicsJoint object connects two physics bodies together.
  40. */
  41. class CC_DLL PhysicsJoint
  42. {
  43. protected:
  44. PhysicsJoint();
  45. virtual ~PhysicsJoint() = 0;
  46. public:
  47. /**Get physics body a connected to this joint.*/
  48. PhysicsBody* getBodyA() const { return _bodyA; }
  49. /**Get physics body b connected to this joint.*/
  50. PhysicsBody* getBodyB() const { return _bodyB; }
  51. /**Get the physics world.*/
  52. PhysicsWorld* getWorld() const { return _world; }
  53. /**
  54. * Get this joint's tag.
  55. *
  56. * @return An integer number.
  57. */
  58. int getTag() const { return _tag; }
  59. /**
  60. * Set this joint's tag.
  61. *
  62. * @param tag An integer number that identifies a PhysicsJoint.
  63. */
  64. void setTag(int tag) { _tag = tag; }
  65. /** Determines if the joint is enable. */
  66. bool isEnabled() const { return _enable; }
  67. /** Enable/Disable the joint. */
  68. void setEnable(bool enable);
  69. /** Determines if the collision is enable. */
  70. bool isCollisionEnabled() const { return _collisionEnable; }
  71. /** Enable/disable the collision between two bodies. */
  72. void setCollisionEnable(bool enable);
  73. /** Remove the joint from the world. */
  74. void removeFormWorld();
  75. /** Set the max force between two bodies. */
  76. void setMaxForce(float force);
  77. /** Get the max force setting. */
  78. float getMaxForce() const { return _maxForce; }
  79. protected:
  80. bool init(PhysicsBody* a, PhysicsBody* b);
  81. bool initJoint();
  82. /** Create constraints for this type joint */
  83. virtual bool createConstraints() { return false; }
  84. std::vector<cpConstraint*> _cpConstraints;
  85. PhysicsBody* _bodyA;
  86. PhysicsBody* _bodyB;
  87. PhysicsWorld* _world;
  88. bool _enable;
  89. bool _collisionEnable;
  90. bool _destroyMark;
  91. int _tag;
  92. float _maxForce;
  93. bool _initDirty;
  94. friend class PhysicsBody;
  95. friend class PhysicsWorld;
  96. friend class PhysicsDebugDraw;
  97. };
  98. /**
  99. * @brief A fixed joint fuses the two bodies together at a reference point. Fixed joints are useful for creating complex shapes that can be broken apart later.
  100. */
  101. class CC_DLL PhysicsJointFixed : public PhysicsJoint
  102. {
  103. public:
  104. /** Create a fixed joint.
  105. @param a A is the body to connect.
  106. @param b B is the body to connect.
  107. @param anchr It's the pivot position.
  108. @return A object pointer.
  109. */
  110. static PhysicsJointFixed* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr);
  111. virtual bool createConstraints() override;
  112. protected:
  113. PhysicsJointFixed() {}
  114. virtual ~PhysicsJointFixed() {}
  115. Vec2 _anchr;
  116. };
  117. /**
  118. * @brief A limit joint imposes a maximum distance between the two bodies, as if they were connected by a rope.
  119. */
  120. class CC_DLL PhysicsJointLimit : public PhysicsJoint
  121. {
  122. public:
  123. /** Create a limit joint.
  124. @param a A is the body to connect.
  125. @param b B is the body to connect.
  126. @param anchr1 Anchr1 is the anchor point on body a.
  127. @param anchr2 Anchr2 is the anchor point on body b.
  128. @return A object pointer.
  129. */
  130. static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2);
  131. /** Create a limit joint.
  132. @param a A is the body to connect.
  133. @param b B is the body to connect.
  134. @param anchr1 Anchr1 is the anchor point on body a.
  135. @param anchr2 Anchr2 is the anchor point on body b.
  136. @param min Define the allowed min distance of the anchor points.
  137. @param max Define the allowed max distance of the anchor points.
  138. @return A object pointer.
  139. */
  140. static PhysicsJointLimit* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float min, float max);
  141. /** Get the anchor point on body a.*/
  142. Vec2 getAnchr1() const;
  143. /** Set the anchor point on body a.*/
  144. void setAnchr1(const Vec2& anchr1);
  145. /** Get the anchor point on body b.*/
  146. Vec2 getAnchr2() const;
  147. /** Set the anchor point on body b.*/
  148. void setAnchr2(const Vec2& anchr2);
  149. /** Get the allowed min distance of the anchor points.*/
  150. float getMin() const;
  151. /** Set the min distance of the anchor points.*/
  152. void setMin(float min);
  153. /** Get the allowed max distance of the anchor points.*/
  154. float getMax() const;
  155. /** Set the max distance of the anchor points.*/
  156. void setMax(float max);
  157. virtual bool createConstraints() override;
  158. protected:
  159. PhysicsJointLimit() {}
  160. virtual ~PhysicsJointLimit() {}
  161. Vec2 _anchr1;
  162. Vec2 _anchr2;
  163. float _min;
  164. float _max;
  165. };
  166. /**
  167. * @brief A pin joint allows the two bodies to independently rotate around the anchor point as if pinned together.
  168. */
  169. class CC_DLL PhysicsJointPin : public PhysicsJoint
  170. {
  171. public:
  172. /** Create a pin joint.
  173. @param a A is the body to connect.
  174. @param b B is the body to connect.
  175. @param pivot It's the pivot position.
  176. @return A object pointer.
  177. */
  178. static PhysicsJointPin* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& pivot);
  179. /** Create a pin joint.
  180. @param a A is the body to connect.
  181. @param b B is the body to connect.
  182. @param anchr1 Anchr1 is the anchor point on body a.
  183. @param anchr2 Anchr2 is the anchor point on body b.
  184. @return A object pointer.
  185. */
  186. static PhysicsJointPin* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2);
  187. virtual bool createConstraints() override;
  188. protected:
  189. PhysicsJointPin() {}
  190. virtual ~PhysicsJointPin() {}
  191. bool _useSpecificAnchr;
  192. Vec2 _anchr1;
  193. Vec2 _anchr2;
  194. };
  195. /** Set the fixed distance with two bodies */
  196. class CC_DLL PhysicsJointDistance : public PhysicsJoint
  197. {
  198. public:
  199. /** Create a fixed distance joint.
  200. @param a A is the body to connect.
  201. @param b B is the body to connect.
  202. @param anchr1 Anchr1 is the anchor point on body a.
  203. @param anchr2 Anchr2 is the anchor point on body b.
  204. @return A object pointer.
  205. */
  206. static PhysicsJointDistance* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2);
  207. /** Get the distance of the anchor points.*/
  208. float getDistance() const;
  209. /** Set the distance of the anchor points.*/
  210. void setDistance(float distance);
  211. virtual bool createConstraints() override;
  212. protected:
  213. PhysicsJointDistance() {}
  214. virtual ~PhysicsJointDistance() {}
  215. Vec2 _anchr1;
  216. Vec2 _anchr2;
  217. };
  218. /** Connecting two physics bodies together with a spring. */
  219. class CC_DLL PhysicsJointSpring : public PhysicsJoint
  220. {
  221. public:
  222. /** Create a fixed distance joint.
  223. @param a A is the body to connect.
  224. @param b B is the body to connect.
  225. @param anchr1 Anchr1 is the anchor point on body a.
  226. @param anchr2 Anchr2 is the anchor point on body b.
  227. @param stiffness It's the spring constant.
  228. @param damping It's how soft to make the damping of the spring.
  229. @return A object pointer.
  230. */
  231. static PhysicsJointSpring* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& anchr1, const Vec2& anchr2, float stiffness, float damping);
  232. /** Get the anchor point on body a.*/
  233. Vec2 getAnchr1() const;
  234. /** Set the anchor point on body a.*/
  235. void setAnchr1(const Vec2& anchr1);
  236. /** Get the anchor point on body b.*/
  237. Vec2 getAnchr2() const;
  238. /** Set the anchor point on body b.*/
  239. void setAnchr2(const Vec2& anchr2);
  240. /** Get the distance of the anchor points.*/
  241. float getRestLength() const;
  242. /** Set the distance of the anchor points.*/
  243. void setRestLength(float restLength);
  244. /** Get the spring constant.*/
  245. float getStiffness() const;
  246. /** Set the spring constant.*/
  247. void setStiffness(float stiffness);
  248. /** Get the spring soft constant.*/
  249. float getDamping() const;
  250. /** Set the spring soft constant.*/
  251. void setDamping(float damping);
  252. virtual bool createConstraints() override;
  253. protected:
  254. PhysicsJointSpring() {}
  255. virtual ~PhysicsJointSpring() {}
  256. Vec2 _anchr1;
  257. Vec2 _anchr2;
  258. float _stiffness;
  259. float _damping;
  260. };
  261. /** Attach body a to a line, and attach body b to a dot. */
  262. class CC_DLL PhysicsJointGroove : public PhysicsJoint
  263. {
  264. public:
  265. /** Create a groove joint.
  266. @param a A is the body to connect.
  267. @param b B is the body to connect.
  268. @param grooveA The line begin position.
  269. @param grooveB The line end position.
  270. @param anchr2 Anchr2 is the anchor point on body b.
  271. @return A object pointer.
  272. */
  273. static PhysicsJointGroove* construct(PhysicsBody* a, PhysicsBody* b, const Vec2& grooveA, const Vec2& grooveB, const Vec2& anchr2);
  274. /** Get the line begin position*/
  275. Vec2 getGrooveA() const;
  276. /** Set the line begin position*/
  277. void setGrooveA(const Vec2& grooveA);
  278. /** Get the line end position*/
  279. Vec2 getGrooveB() const;
  280. /** Set the line end position*/
  281. void setGrooveB(const Vec2& grooveB);
  282. /** Get the anchor point on body b.*/
  283. Vec2 getAnchr2() const;
  284. /** Set the anchor point on body b.*/
  285. void setAnchr2(const Vec2& anchr2);
  286. virtual bool createConstraints() override;
  287. protected:
  288. PhysicsJointGroove() {}
  289. virtual ~PhysicsJointGroove() {}
  290. Vec2 _grooveA;
  291. Vec2 _grooveB;
  292. Vec2 _anchr2;
  293. };
  294. /** Likes a spring joint, but works with rotary. */
  295. class CC_DLL PhysicsJointRotarySpring : public PhysicsJoint
  296. {
  297. public:
  298. /** Create a damped rotary spring joint.
  299. @param a A is the body to connect.
  300. @param b B is the body to connect.
  301. @param stiffness It's the spring constant.
  302. @param damping It's how soft to make the damping of the spring.
  303. @return A object pointer.
  304. */
  305. static PhysicsJointRotarySpring* construct(PhysicsBody* a, PhysicsBody* b, float stiffness, float damping);
  306. /** Get the relative angle in radians from the body a to b.*/
  307. float getRestAngle() const;
  308. /** Set the relative angle in radians from the body a to b.*/
  309. void setRestAngle(float restAngle);
  310. /** Get the spring constant.*/
  311. float getStiffness() const;
  312. /** Set the spring constant.*/
  313. void setStiffness(float stiffness);
  314. /** Get the spring soft constant.*/
  315. float getDamping() const;
  316. /** Set the spring soft constant.*/
  317. void setDamping(float damping);
  318. virtual bool createConstraints() override;
  319. protected:
  320. PhysicsJointRotarySpring() {}
  321. virtual ~PhysicsJointRotarySpring() {}
  322. float _stiffness;
  323. float _damping;
  324. };
  325. /** Likes a limit joint, but works with rotary. */
  326. class CC_DLL PhysicsJointRotaryLimit : public PhysicsJoint
  327. {
  328. public:
  329. /** Create a limit rotary joint.
  330. @param a A is the body to connect.
  331. @param b B is the body to connect.
  332. @param min It's min rotation limit in radians.
  333. @param max It's max rotation limit in radians.
  334. @return A object pointer.
  335. */
  336. static PhysicsJointRotaryLimit* construct(PhysicsBody* a, PhysicsBody* b, float min, float max);
  337. /** Create a limit rotary joint.
  338. @param a A is the body to connect.
  339. @param b B is the body to connect.
  340. @return A object pointer.
  341. */
  342. static PhysicsJointRotaryLimit* construct(PhysicsBody* a, PhysicsBody* b);
  343. /** Get the min rotation limit.*/
  344. float getMin() const;
  345. /** Set the min rotation limit.*/
  346. void setMin(float min);
  347. /** Get the max rotation limit.*/
  348. float getMax() const;
  349. /** Set the max rotation limit.*/
  350. void setMax(float max);
  351. virtual bool createConstraints() override;
  352. protected:
  353. PhysicsJointRotaryLimit() {}
  354. virtual ~PhysicsJointRotaryLimit() {}
  355. float _min;
  356. float _max;
  357. };
  358. /** Works like a socket wrench. */
  359. class CC_DLL PhysicsJointRatchet : public PhysicsJoint
  360. {
  361. public:
  362. /** Create a ratchet joint.
  363. @param a A is the body to connect.
  364. @param b B is the body to connect.
  365. @param phase Phase is the initial offset to use when deciding where the ratchet angles are.
  366. @param ratchet Ratchet is the distance between "clicks".
  367. @return A object pointer.
  368. */
  369. static PhysicsJointRatchet* construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratchet);
  370. /** Get the ratchet angle.*/
  371. float getAngle() const;
  372. /** Set the ratchet angle.*/
  373. void setAngle(float angle);
  374. /** Get the initial offset.*/
  375. float getPhase() const;
  376. /** Set the initial offset.*/
  377. void setPhase(float phase);
  378. /** Get the distance between "clicks".*/
  379. float getRatchet() const;
  380. /** Set the distance between "clicks".*/
  381. void setRatchet(float ratchet);
  382. virtual bool createConstraints() override;
  383. protected:
  384. PhysicsJointRatchet() {}
  385. virtual ~PhysicsJointRatchet() {}
  386. float _phase;
  387. float _ratchet;
  388. };
  389. /** Keeps the angular velocity ratio of a pair of bodies constant. */
  390. class CC_DLL PhysicsJointGear : public PhysicsJoint
  391. {
  392. public:
  393. /** Create a gear joint.
  394. @param a A is the body to connect.
  395. @param b B is the body to connect.
  396. @param phase Phase is the initial angular offset of the two bodies.
  397. @param ratio Ratio is always measured in absolute terms.
  398. @return A object pointer.
  399. */
  400. static PhysicsJointGear* construct(PhysicsBody* a, PhysicsBody* b, float phase, float ratio);
  401. /** Get the angular offset of the two bodies.*/
  402. float getPhase() const;
  403. /** Set the angular offset of the two bodies.*/
  404. void setPhase(float phase);
  405. /** Get the ratio.*/
  406. float getRatio() const;
  407. /** Set the ratio.*/
  408. void setRatio(float ratchet);
  409. virtual bool createConstraints() override;
  410. protected:
  411. PhysicsJointGear() {}
  412. virtual ~PhysicsJointGear() {}
  413. float _phase;
  414. float _ratio;
  415. };
  416. /** Keeps the relative angular velocity of a pair of bodies constant. */
  417. class CC_DLL PhysicsJointMotor : public PhysicsJoint
  418. {
  419. public:
  420. /** Create a motor joint.
  421. @param a A is the body to connect.
  422. @param b B is the body to connect.
  423. @param rate Rate is the desired relative angular velocity.
  424. @return A object pointer.
  425. */
  426. static PhysicsJointMotor* construct(PhysicsBody* a, PhysicsBody* b, float rate);
  427. /** Get the relative angular velocity.*/
  428. float getRate() const;
  429. /** Set the relative angular velocity.*/
  430. void setRate(float rate);
  431. virtual bool createConstraints() override;
  432. protected:
  433. PhysicsJointMotor() {}
  434. virtual ~PhysicsJointMotor() {}
  435. float _rate;
  436. };
  437. /** @} */
  438. /** @} */
  439. NS_CC_END
  440. #endif // CC_USE_PHYSICS
  441. #endif // __CCPHYSICS_JOINT_H__