1
0

CCPhysicsContact.cpp 12 KB

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