CCPhysicsContact.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_CONTACT_H__
  22. #define __CCPHYSICS_CONTACT_H__
  23. #include "base/ccConfig.h"
  24. #if CC_USE_PHYSICS
  25. #include "base/CCRef.h"
  26. #include "math/CCGeometry.h"
  27. #include "base/CCEventListenerCustom.h"
  28. #include "base/CCEvent.h"
  29. #include "base/CCEventCustom.h"
  30. NS_CC_BEGIN
  31. class PhysicsShape;
  32. class PhysicsBody;
  33. class PhysicsWorld;
  34. typedef struct CC_DLL PhysicsContactData
  35. {
  36. static const int POINT_MAX = 4;
  37. Vec2 points[POINT_MAX];
  38. int count;
  39. Vec2 normal;
  40. PhysicsContactData()
  41. : count(0)
  42. {}
  43. }PhysicsContactData;
  44. /**
  45. * @addtogroup physics
  46. * @{
  47. * @addtogroup physics_2d
  48. * @{
  49. */
  50. /**
  51. * @brief Contact information.
  52. * It will created automatically when two shape contact with each other. And it will destroyed automatically when two shape separated.
  53. */
  54. class CC_DLL PhysicsContact : public EventCustom
  55. {
  56. public:
  57. enum class EventCode
  58. {
  59. NONE,
  60. BEGIN,
  61. PRESOLVE,
  62. POSTSOLVE,
  63. SEPARATE
  64. };
  65. /** Get contact shape A. */
  66. PhysicsShape* getShapeA() const { return _shapeA; }
  67. /** Get contact shape B. */
  68. PhysicsShape* getShapeB() const { return _shapeB; }
  69. /** Get contact data. */
  70. const PhysicsContactData* getContactData() const { return _contactData; }
  71. /** Get previous contact data */
  72. const PhysicsContactData* getPreContactData() const { return _preContactData; }
  73. /**
  74. * Get data.
  75. * @lua NA
  76. */
  77. void* getData() const { return _data; }
  78. /**
  79. * @brief Set data to contact.
  80. * You must manage the memory yourself, Generally you can set data at contact begin, and destroy it at contact separate.
  81. *
  82. * @lua NA
  83. */
  84. void setData(void* data) { _data = data; }
  85. /** Get the event code */
  86. EventCode getEventCode() const { return _eventCode; };
  87. private:
  88. static PhysicsContact* construct(PhysicsShape* a, PhysicsShape* b);
  89. bool init(PhysicsShape* a, PhysicsShape* b);
  90. void setEventCode(EventCode eventCode) { _eventCode = eventCode; };
  91. bool isNotificationEnabled() const { return _notificationEnable; }
  92. void setNotificationEnable(bool enable) { _notificationEnable = enable; }
  93. PhysicsWorld* getWorld() const { return _world; }
  94. void setWorld(PhysicsWorld* world) { _world = world; }
  95. void setResult(bool result) { _result = result; }
  96. bool resetResult() { bool ret = _result; _result = true; return ret; }
  97. void generateContactData();
  98. private:
  99. PhysicsContact();
  100. ~PhysicsContact();
  101. private:
  102. PhysicsWorld* _world;
  103. PhysicsShape* _shapeA;
  104. PhysicsShape* _shapeB;
  105. EventCode _eventCode;
  106. bool _notificationEnable;
  107. bool _result;
  108. void* _data;
  109. void* _contactInfo;
  110. PhysicsContactData* _contactData;
  111. PhysicsContactData* _preContactData;
  112. friend class EventListenerPhysicsContact;
  113. friend class PhysicsWorldCallback;
  114. friend class PhysicsWorld;
  115. };
  116. /**
  117. * @brief Presolve value generated when onContactPreSolve called.
  118. */
  119. class CC_DLL PhysicsContactPreSolve
  120. {
  121. public:
  122. /** Get restitution between two bodies.*/
  123. float getRestitution() const;
  124. /** Get friction between two bodies.*/
  125. float getFriction() const;
  126. /** Get surface velocity between two bodies.*/
  127. Vec2 getSurfaceVelocity() const;
  128. /** Set the restitution.*/
  129. void setRestitution(float restitution);
  130. /** Set the friction.*/
  131. void setFriction(float friction);
  132. /** Set the surface velocity.*/
  133. void setSurfaceVelocity(const Vec2& velocity);
  134. /** Ignore the rest of the contact presolve and postsolve callbacks. */
  135. void ignore();
  136. private:
  137. PhysicsContactPreSolve(void* contactInfo);
  138. ~PhysicsContactPreSolve();
  139. private:
  140. void* _contactInfo;
  141. friend class EventListenerPhysicsContact;
  142. };
  143. /**
  144. * @brief Postsolve value generated when onContactPostSolve called.
  145. */
  146. class CC_DLL PhysicsContactPostSolve
  147. {
  148. public:
  149. /** Get restitution between two bodies.*/
  150. float getRestitution() const;
  151. /** Get friction between two bodies.*/
  152. float getFriction() const;
  153. /** Get surface velocity between two bodies.*/
  154. Vec2 getSurfaceVelocity() const;
  155. private:
  156. PhysicsContactPostSolve(void* contactInfo);
  157. ~PhysicsContactPostSolve();
  158. private:
  159. void* _contactInfo;
  160. friend class EventListenerPhysicsContact;
  161. };
  162. /** Contact listener. It will receive all the contact callbacks. */
  163. class CC_DLL EventListenerPhysicsContact : public EventListenerCustom
  164. {
  165. public:
  166. /** Create the listener. */
  167. static EventListenerPhysicsContact* create();
  168. /** Check the listener is available.
  169. * @return True if there's one available callback function at least, false if there's no one.
  170. */
  171. virtual bool checkAvailable() override;
  172. /** Clone an object from this listener.*/
  173. virtual EventListenerPhysicsContact* clone() override;
  174. protected:
  175. /**
  176. * It will be call when two body have contact.
  177. * if return false, it will not invoke callbacks.
  178. */
  179. virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB);
  180. public:
  181. /**
  182. * @brief It will called at two shapes start to contact, and only call it once.
  183. */
  184. std::function<bool(PhysicsContact& contact)> onContactBegin;
  185. /**
  186. * @brief Two shapes are touching during this step. Return false from the callback to make world ignore the collision this step or true to process it normally. Additionally, you may override collision values, restitution, or surface velocity values.
  187. */
  188. std::function<bool(PhysicsContact& contact, PhysicsContactPreSolve& solve)> onContactPreSolve;
  189. /**
  190. * @brief Two shapes are touching and their collision response has been processed. You can retrieve the collision impulse or kinetic energy at this time if you want to use it to calculate sound volumes or damage amounts. See cpArbiter for more info
  191. */
  192. std::function<void(PhysicsContact& contact, const PhysicsContactPostSolve& solve)> onContactPostSolve;
  193. /**
  194. * @brief It will called at two shapes separated, and only call it once.
  195. * onContactBegin and onContactSeparate will called in pairs.
  196. */
  197. std::function<void(PhysicsContact& contact)> onContactSeparate;
  198. protected:
  199. bool init();
  200. void onEvent(EventCustom* event);
  201. protected:
  202. EventListenerPhysicsContact();
  203. virtual ~EventListenerPhysicsContact();
  204. friend class PhysicsWorld;
  205. };
  206. /** This event listener only be called when bodyA and bodyB have contacts. */
  207. class CC_DLL EventListenerPhysicsContactWithBodies : public EventListenerPhysicsContact
  208. {
  209. public:
  210. /** Create the listener. */
  211. static EventListenerPhysicsContactWithBodies* create(PhysicsBody* bodyA, PhysicsBody* bodyB);
  212. virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
  213. virtual EventListenerPhysicsContactWithBodies* clone() override;
  214. protected:
  215. PhysicsBody* _a;
  216. PhysicsBody* _b;
  217. protected:
  218. EventListenerPhysicsContactWithBodies();
  219. virtual ~EventListenerPhysicsContactWithBodies();
  220. };
  221. /** This event listener only be called when shapeA and shapeB have contacts. */
  222. class CC_DLL EventListenerPhysicsContactWithShapes : public EventListenerPhysicsContact
  223. {
  224. public:
  225. /** Create the listener. */
  226. static EventListenerPhysicsContactWithShapes* create(PhysicsShape* shapeA, PhysicsShape* shapeB);
  227. virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
  228. virtual EventListenerPhysicsContactWithShapes* clone() override;
  229. protected:
  230. PhysicsShape* _a;
  231. PhysicsShape* _b;
  232. protected:
  233. EventListenerPhysicsContactWithShapes();
  234. virtual ~EventListenerPhysicsContactWithShapes();
  235. };
  236. /** This event listener only be called when shapeA or shapeB is in the group your specified */
  237. class CC_DLL EventListenerPhysicsContactWithGroup : public EventListenerPhysicsContact
  238. {
  239. public:
  240. /** Create the listener. */
  241. static EventListenerPhysicsContactWithGroup* create(int group);
  242. virtual bool hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB) override;
  243. virtual EventListenerPhysicsContactWithGroup* clone() override;
  244. protected:
  245. int _group;
  246. protected:
  247. EventListenerPhysicsContactWithGroup();
  248. virtual ~EventListenerPhysicsContactWithGroup();
  249. };
  250. /** @} */
  251. /** @} */
  252. NS_CC_END
  253. #endif // CC_USE_PHYSICS
  254. #endif //__CCPHYSICS_CONTACT_H__