CCPhysicsContact.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. #include "physics/CCPhysicsContact.h"
  22. #if CC_USE_PHYSICS
  23. #include "chipmunk/chipmunk.h"
  24. #include "physics/CCPhysicsBody.h"
  25. #include "physics/CCPhysicsHelper.h"
  26. #include "base/CCEventCustom.h"
  27. NS_CC_BEGIN
  28. const char* PHYSICSCONTACT_EVENT_NAME = "PhysicsContactEvent";
  29. PhysicsContact::PhysicsContact()
  30. : EventCustom(PHYSICSCONTACT_EVENT_NAME)
  31. , _world(nullptr)
  32. , _shapeA(nullptr)
  33. , _shapeB(nullptr)
  34. , _eventCode(EventCode::NONE)
  35. , _notificationEnable(true)
  36. , _result(true)
  37. , _data(nullptr)
  38. , _contactInfo(nullptr)
  39. , _contactData(nullptr)
  40. , _preContactData(nullptr)
  41. {
  42. }
  43. PhysicsContact::~PhysicsContact()
  44. {
  45. CC_SAFE_DELETE(_contactData);
  46. CC_SAFE_DELETE(_preContactData);
  47. }
  48. PhysicsContact* PhysicsContact::construct(PhysicsShape* a, PhysicsShape* b)
  49. {
  50. PhysicsContact * contact = new (std::nothrow) PhysicsContact();
  51. if(contact && contact->init(a, b))
  52. {
  53. return contact;
  54. }
  55. CC_SAFE_DELETE(contact);
  56. return nullptr;
  57. }
  58. bool PhysicsContact::init(PhysicsShape* a, PhysicsShape* b)
  59. {
  60. do
  61. {
  62. CC_BREAK_IF(a == nullptr || b == nullptr);
  63. _shapeA = a;
  64. _shapeB = b;
  65. return true;
  66. } while(false);
  67. return false;
  68. }
  69. void PhysicsContact::generateContactData()
  70. {
  71. if (_contactInfo == nullptr)
  72. {
  73. return;
  74. }
  75. cpArbiter* arb = static_cast<cpArbiter*>(_contactInfo);
  76. CC_SAFE_DELETE(_preContactData);
  77. _preContactData = _contactData;
  78. _contactData = new (std::nothrow) PhysicsContactData();
  79. _contactData->count = cpArbiterGetCount(arb);
  80. for (int i=0; i<_contactData->count && i<PhysicsContactData::POINT_MAX; ++i)
  81. {
  82. _contactData->points[i] = PhysicsHelper::cpv2point(cpArbiterGetPointA(arb, i));
  83. }
  84. _contactData->normal = _contactData->count > 0 ? PhysicsHelper::cpv2point(cpArbiterGetNormal(arb)) : Vec2::ZERO;
  85. }
  86. // PhysicsContactPreSolve implementation
  87. PhysicsContactPreSolve::PhysicsContactPreSolve(void* contactInfo)
  88. : _contactInfo(contactInfo)
  89. {
  90. }
  91. PhysicsContactPreSolve::~PhysicsContactPreSolve()
  92. {
  93. }
  94. float PhysicsContactPreSolve::getRestitution() const
  95. {
  96. return cpArbiterGetRestitution(static_cast<cpArbiter*>(_contactInfo));
  97. }
  98. float PhysicsContactPreSolve::getFriction() const
  99. {
  100. return cpArbiterGetFriction(static_cast<cpArbiter*>(_contactInfo));
  101. }
  102. Vec2 PhysicsContactPreSolve::getSurfaceVelocity() const
  103. {
  104. return PhysicsHelper::cpv2point(cpArbiterGetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo)));
  105. }
  106. void PhysicsContactPreSolve::setRestitution(float restitution)
  107. {
  108. cpArbiterSetRestitution(static_cast<cpArbiter*>(_contactInfo), restitution);
  109. }
  110. void PhysicsContactPreSolve::setFriction(float friction)
  111. {
  112. cpArbiterSetFriction(static_cast<cpArbiter*>(_contactInfo), friction);
  113. }
  114. void PhysicsContactPreSolve::setSurfaceVelocity(const Vec2& velocity)
  115. {
  116. cpArbiterSetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo), PhysicsHelper::point2cpv(velocity));
  117. }
  118. void PhysicsContactPreSolve::ignore()
  119. {
  120. cpArbiterIgnore(static_cast<cpArbiter*>(_contactInfo));
  121. }
  122. // PhysicsContactPostSolve implementation
  123. PhysicsContactPostSolve::PhysicsContactPostSolve(void* contactInfo)
  124. : _contactInfo(contactInfo)
  125. {
  126. }
  127. PhysicsContactPostSolve::~PhysicsContactPostSolve()
  128. {
  129. }
  130. float PhysicsContactPostSolve::getRestitution() const
  131. {
  132. return cpArbiterGetRestitution(static_cast<cpArbiter*>(_contactInfo));
  133. }
  134. float PhysicsContactPostSolve::getFriction() const
  135. {
  136. return cpArbiterGetFriction(static_cast<cpArbiter*>(_contactInfo));
  137. }
  138. Vec2 PhysicsContactPostSolve::getSurfaceVelocity() const
  139. {
  140. return PhysicsHelper::cpv2point(cpArbiterGetSurfaceVelocity(static_cast<cpArbiter*>(_contactInfo)));
  141. }
  142. EventListenerPhysicsContact::EventListenerPhysicsContact()
  143. : onContactBegin(nullptr)
  144. , onContactPreSolve(nullptr)
  145. , onContactPostSolve(nullptr)
  146. , onContactSeparate(nullptr)
  147. {
  148. }
  149. bool EventListenerPhysicsContact::init()
  150. {
  151. auto func = [this](EventCustom* event) -> void
  152. {
  153. onEvent(event);
  154. };
  155. return EventListenerCustom::init(PHYSICSCONTACT_EVENT_NAME, func);
  156. }
  157. void EventListenerPhysicsContact::onEvent(EventCustom* event)
  158. {
  159. PhysicsContact* contact = dynamic_cast<PhysicsContact*>(event);
  160. if (contact == nullptr)
  161. {
  162. return;
  163. }
  164. switch (contact->getEventCode())
  165. {
  166. case PhysicsContact::EventCode::BEGIN:
  167. {
  168. bool ret = true;
  169. if (onContactBegin != nullptr
  170. && hitTest(contact->getShapeA(), contact->getShapeB()))
  171. {
  172. contact->generateContactData();
  173. ret = onContactBegin(*contact);
  174. }
  175. contact->setResult(ret);
  176. break;
  177. }
  178. case PhysicsContact::EventCode::PRESOLVE:
  179. {
  180. bool ret = true;
  181. if (onContactPreSolve != nullptr
  182. && hitTest(contact->getShapeA(), contact->getShapeB()))
  183. {
  184. PhysicsContactPreSolve solve(contact->_contactInfo);
  185. contact->generateContactData();
  186. ret = onContactPreSolve(*contact, solve);
  187. }
  188. contact->setResult(ret);
  189. break;
  190. }
  191. case PhysicsContact::EventCode::POSTSOLVE:
  192. {
  193. if (onContactPostSolve != nullptr
  194. && hitTest(contact->getShapeA(), contact->getShapeB()))
  195. {
  196. PhysicsContactPostSolve solve(contact->_contactInfo);
  197. onContactPostSolve(*contact, solve);
  198. }
  199. break;
  200. }
  201. case PhysicsContact::EventCode::SEPARATE:
  202. {
  203. if (onContactSeparate != nullptr
  204. && hitTest(contact->getShapeA(), contact->getShapeB()))
  205. {
  206. onContactSeparate(*contact);
  207. }
  208. break;
  209. }
  210. default:
  211. break;
  212. }
  213. }
  214. EventListenerPhysicsContact::~EventListenerPhysicsContact()
  215. {
  216. }
  217. EventListenerPhysicsContact* EventListenerPhysicsContact::create()
  218. {
  219. EventListenerPhysicsContact* obj = new (std::nothrow) EventListenerPhysicsContact();
  220. if (obj != nullptr && obj->init())
  221. {
  222. obj->autorelease();
  223. return obj;
  224. }
  225. CC_SAFE_DELETE(obj);
  226. return nullptr;
  227. }
  228. bool EventListenerPhysicsContact::hitTest(PhysicsShape* /*shapeA*/, PhysicsShape* /*shapeB*/)
  229. {
  230. return true;
  231. }
  232. bool EventListenerPhysicsContact::checkAvailable()
  233. {
  234. if (onContactBegin == nullptr && onContactPreSolve == nullptr
  235. && onContactPostSolve == nullptr && onContactSeparate == nullptr)
  236. {
  237. CCASSERT(false, "Invalid PhysicsContactListener.");
  238. return false;
  239. }
  240. return true;
  241. }
  242. EventListenerPhysicsContact* EventListenerPhysicsContact::clone()
  243. {
  244. EventListenerPhysicsContact* obj = EventListenerPhysicsContact::create();
  245. if (obj != nullptr)
  246. {
  247. obj->onContactBegin = onContactBegin;
  248. obj->onContactPreSolve = onContactPreSolve;
  249. obj->onContactPostSolve = onContactPostSolve;
  250. obj->onContactSeparate = onContactSeparate;
  251. return obj;
  252. }
  253. CC_SAFE_DELETE(obj);
  254. return nullptr;
  255. }
  256. EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::create(PhysicsBody* bodyA, PhysicsBody* bodyB)
  257. {
  258. EventListenerPhysicsContactWithBodies* obj = new (std::nothrow) EventListenerPhysicsContactWithBodies();
  259. if (obj != nullptr && obj->init())
  260. {
  261. obj->_a = bodyA;
  262. obj->_b = bodyB;
  263. obj->autorelease();
  264. return obj;
  265. }
  266. CC_SAFE_DELETE(obj);
  267. return nullptr;
  268. }
  269. EventListenerPhysicsContactWithBodies::EventListenerPhysicsContactWithBodies()
  270. : _a(nullptr)
  271. , _b(nullptr)
  272. {
  273. }
  274. EventListenerPhysicsContactWithBodies::~EventListenerPhysicsContactWithBodies()
  275. {
  276. }
  277. bool EventListenerPhysicsContactWithBodies::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
  278. {
  279. if ((shapeA->getBody() == _a && shapeB->getBody() == _b)
  280. || (shapeA->getBody() == _b && shapeB->getBody() == _a))
  281. {
  282. return true;
  283. }
  284. return false;
  285. }
  286. EventListenerPhysicsContactWithBodies* EventListenerPhysicsContactWithBodies::clone()
  287. {
  288. EventListenerPhysicsContactWithBodies* obj = EventListenerPhysicsContactWithBodies::create(_a, _b);
  289. if (obj != nullptr)
  290. {
  291. obj->onContactBegin = onContactBegin;
  292. obj->onContactPreSolve = onContactPreSolve;
  293. obj->onContactPostSolve = onContactPostSolve;
  294. obj->onContactSeparate = onContactSeparate;
  295. return obj;
  296. }
  297. CC_SAFE_DELETE(obj);
  298. return nullptr;
  299. }
  300. EventListenerPhysicsContactWithShapes::EventListenerPhysicsContactWithShapes()
  301. : _a(nullptr)
  302. , _b(nullptr)
  303. {
  304. }
  305. EventListenerPhysicsContactWithShapes::~EventListenerPhysicsContactWithShapes()
  306. {
  307. }
  308. EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::create(PhysicsShape* shapeA, PhysicsShape* shapeB)
  309. {
  310. EventListenerPhysicsContactWithShapes* obj = new (std::nothrow) EventListenerPhysicsContactWithShapes();
  311. if (obj != nullptr && obj->init())
  312. {
  313. obj->_a = shapeA;
  314. obj->_b = shapeB;
  315. obj->autorelease();
  316. return obj;
  317. }
  318. CC_SAFE_DELETE(obj);
  319. return nullptr;
  320. }
  321. bool EventListenerPhysicsContactWithShapes::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
  322. {
  323. if ((shapeA == _a && shapeB == _b)
  324. || (shapeA == _b && shapeB == _a))
  325. {
  326. return true;
  327. }
  328. return false;
  329. }
  330. EventListenerPhysicsContactWithShapes* EventListenerPhysicsContactWithShapes::clone()
  331. {
  332. EventListenerPhysicsContactWithShapes* obj = EventListenerPhysicsContactWithShapes::create(_a, _b);
  333. if (obj != nullptr)
  334. {
  335. obj->onContactBegin = onContactBegin;
  336. obj->onContactPreSolve = onContactPreSolve;
  337. obj->onContactPostSolve = onContactPostSolve;
  338. obj->onContactSeparate = onContactSeparate;
  339. return obj;
  340. }
  341. CC_SAFE_DELETE(obj);
  342. return nullptr;
  343. }
  344. EventListenerPhysicsContactWithGroup::EventListenerPhysicsContactWithGroup()
  345. : _group(CP_NO_GROUP)
  346. {
  347. }
  348. EventListenerPhysicsContactWithGroup::~EventListenerPhysicsContactWithGroup()
  349. {
  350. }
  351. EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::create(int group)
  352. {
  353. EventListenerPhysicsContactWithGroup* obj = new (std::nothrow) EventListenerPhysicsContactWithGroup();
  354. if (obj != nullptr && obj->init())
  355. {
  356. obj->_group = group;
  357. obj->autorelease();
  358. return obj;
  359. }
  360. CC_SAFE_DELETE(obj);
  361. return nullptr;
  362. }
  363. bool EventListenerPhysicsContactWithGroup::hitTest(PhysicsShape* shapeA, PhysicsShape* shapeB)
  364. {
  365. if (shapeA->getGroup() == _group || shapeB->getGroup() == _group)
  366. {
  367. return true;
  368. }
  369. return false;
  370. }
  371. EventListenerPhysicsContactWithGroup* EventListenerPhysicsContactWithGroup::clone()
  372. {
  373. EventListenerPhysicsContactWithGroup* obj = EventListenerPhysicsContactWithGroup::create(_group);
  374. if (obj != nullptr)
  375. {
  376. obj->onContactBegin = onContactBegin;
  377. obj->onContactPreSolve = onContactPreSolve;
  378. obj->onContactPostSolve = onContactPostSolve;
  379. obj->onContactSeparate = onContactSeparate;
  380. return obj;
  381. }
  382. CC_SAFE_DELETE(obj);
  383. return nullptr;
  384. }
  385. NS_CC_END
  386. #endif // CC_USE_PHYSICS