b2ContactManager.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. * Copyright (c) 2014 Google, Inc.
  4. *
  5. * This software is provided 'as-is', without any express or implied
  6. * warranty. In no event will the authors be held liable for any damages
  7. * arising from the use of this software.
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. * 1. The origin of this software must not be misrepresented; you must not
  12. * claim that you wrote the original software. If you use this software
  13. * in a product, an acknowledgment in the product documentation would be
  14. * appreciated but is not required.
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. * 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. #include <Box2D/Dynamics/b2ContactManager.h>
  20. #include <Box2D/Dynamics/b2Body.h>
  21. #include <Box2D/Dynamics/b2Fixture.h>
  22. #include <Box2D/Dynamics/b2WorldCallbacks.h>
  23. #include <Box2D/Dynamics/Contacts/b2Contact.h>
  24. b2ContactFilter b2_defaultFilter;
  25. b2ContactListener b2_defaultListener;
  26. b2ContactManager::b2ContactManager()
  27. {
  28. m_contactList = NULL;
  29. m_contactCount = 0;
  30. m_contactFilter = &b2_defaultFilter;
  31. m_contactListener = &b2_defaultListener;
  32. m_allocator = NULL;
  33. }
  34. void b2ContactManager::Destroy(b2Contact* c)
  35. {
  36. b2Fixture* fixtureA = c->GetFixtureA();
  37. b2Fixture* fixtureB = c->GetFixtureB();
  38. b2Body* bodyA = fixtureA->GetBody();
  39. b2Body* bodyB = fixtureB->GetBody();
  40. if (m_contactListener && c->IsTouching())
  41. {
  42. m_contactListener->EndContact(c);
  43. }
  44. // Remove from the world.
  45. if (c->m_prev)
  46. {
  47. c->m_prev->m_next = c->m_next;
  48. }
  49. if (c->m_next)
  50. {
  51. c->m_next->m_prev = c->m_prev;
  52. }
  53. if (c == m_contactList)
  54. {
  55. m_contactList = c->m_next;
  56. }
  57. // Remove from body 1
  58. if (c->m_nodeA.prev)
  59. {
  60. c->m_nodeA.prev->next = c->m_nodeA.next;
  61. }
  62. if (c->m_nodeA.next)
  63. {
  64. c->m_nodeA.next->prev = c->m_nodeA.prev;
  65. }
  66. if (&c->m_nodeA == bodyA->m_contactList)
  67. {
  68. bodyA->m_contactList = c->m_nodeA.next;
  69. }
  70. // Remove from body 2
  71. if (c->m_nodeB.prev)
  72. {
  73. c->m_nodeB.prev->next = c->m_nodeB.next;
  74. }
  75. if (c->m_nodeB.next)
  76. {
  77. c->m_nodeB.next->prev = c->m_nodeB.prev;
  78. }
  79. if (&c->m_nodeB == bodyB->m_contactList)
  80. {
  81. bodyB->m_contactList = c->m_nodeB.next;
  82. }
  83. // Call the factory.
  84. b2Contact::Destroy(c, m_allocator);
  85. --m_contactCount;
  86. }
  87. // This is the top level collision call for the time step. Here
  88. // all the narrow phase collision is processed for the world
  89. // contact list.
  90. void b2ContactManager::Collide()
  91. {
  92. // Update awake contacts.
  93. b2Contact* c = m_contactList;
  94. while (c)
  95. {
  96. b2Fixture* fixtureA = c->GetFixtureA();
  97. b2Fixture* fixtureB = c->GetFixtureB();
  98. int32 indexA = c->GetChildIndexA();
  99. int32 indexB = c->GetChildIndexB();
  100. b2Body* bodyA = fixtureA->GetBody();
  101. b2Body* bodyB = fixtureB->GetBody();
  102. // Is this contact flagged for filtering?
  103. if (c->m_flags & b2Contact::e_filterFlag)
  104. {
  105. // Should these bodies collide?
  106. if (bodyB->ShouldCollide(bodyA) == false)
  107. {
  108. b2Contact* cNuke = c;
  109. c = cNuke->GetNext();
  110. Destroy(cNuke);
  111. continue;
  112. }
  113. // Check user filtering.
  114. if (m_contactFilter && m_contactFilter->ShouldCollide(fixtureA, fixtureB) == false)
  115. {
  116. b2Contact* cNuke = c;
  117. c = cNuke->GetNext();
  118. Destroy(cNuke);
  119. continue;
  120. }
  121. // Clear the filtering flag.
  122. c->m_flags &= ~b2Contact::e_filterFlag;
  123. }
  124. bool activeA = bodyA->IsAwake() && bodyA->m_type != b2_staticBody;
  125. bool activeB = bodyB->IsAwake() && bodyB->m_type != b2_staticBody;
  126. // At least one body must be awake and it must be dynamic or kinematic.
  127. if (activeA == false && activeB == false)
  128. {
  129. c = c->GetNext();
  130. continue;
  131. }
  132. int32 proxyIdA = fixtureA->m_proxies[indexA].proxyId;
  133. int32 proxyIdB = fixtureB->m_proxies[indexB].proxyId;
  134. bool overlap = m_broadPhase.TestOverlap(proxyIdA, proxyIdB);
  135. // Here we destroy contacts that cease to overlap in the broad-phase.
  136. if (overlap == false)
  137. {
  138. b2Contact* cNuke = c;
  139. c = cNuke->GetNext();
  140. Destroy(cNuke);
  141. continue;
  142. }
  143. // The contact persists.
  144. c->Update(m_contactListener);
  145. c = c->GetNext();
  146. }
  147. }
  148. void b2ContactManager::FindNewContacts()
  149. {
  150. m_broadPhase.UpdatePairs(this);
  151. }
  152. void b2ContactManager::AddPair(void* proxyUserDataA, void* proxyUserDataB)
  153. {
  154. b2FixtureProxy* proxyA = (b2FixtureProxy*)proxyUserDataA;
  155. b2FixtureProxy* proxyB = (b2FixtureProxy*)proxyUserDataB;
  156. b2Fixture* fixtureA = proxyA->fixture;
  157. b2Fixture* fixtureB = proxyB->fixture;
  158. int32 indexA = proxyA->childIndex;
  159. int32 indexB = proxyB->childIndex;
  160. b2Body* bodyA = fixtureA->GetBody();
  161. b2Body* bodyB = fixtureB->GetBody();
  162. // Are the fixtures on the same body?
  163. if (bodyA == bodyB)
  164. {
  165. return;
  166. }
  167. // TODO_ERIN use a hash table to remove a potential bottleneck when both
  168. // bodies have a lot of contacts.
  169. // Does a contact already exist?
  170. b2ContactEdge* edge = bodyB->GetContactList();
  171. while (edge)
  172. {
  173. if (edge->other == bodyA)
  174. {
  175. b2Fixture* fA = edge->contact->GetFixtureA();
  176. b2Fixture* fB = edge->contact->GetFixtureB();
  177. int32 iA = edge->contact->GetChildIndexA();
  178. int32 iB = edge->contact->GetChildIndexB();
  179. if (fA == fixtureA && fB == fixtureB && iA == indexA && iB == indexB)
  180. {
  181. // A contact already exists.
  182. return;
  183. }
  184. if (fA == fixtureB && fB == fixtureA && iA == indexB && iB == indexA)
  185. {
  186. // A contact already exists.
  187. return;
  188. }
  189. }
  190. edge = edge->next;
  191. }
  192. // Does a joint override collision? Is at least one body dynamic?
  193. if (bodyB->ShouldCollide(bodyA) == false)
  194. {
  195. return;
  196. }
  197. // Check user filtering.
  198. if (m_contactFilter && m_contactFilter->ShouldCollide(fixtureA, fixtureB) == false)
  199. {
  200. return;
  201. }
  202. // Call the factory.
  203. b2Contact* c = b2Contact::Create(fixtureA, indexA, fixtureB, indexB, m_allocator);
  204. if (c == NULL)
  205. {
  206. return;
  207. }
  208. // Contact creation may swap fixtures.
  209. fixtureA = c->GetFixtureA();
  210. fixtureB = c->GetFixtureB();
  211. bodyA = fixtureA->GetBody();
  212. bodyB = fixtureB->GetBody();
  213. // Insert into the world.
  214. c->m_prev = NULL;
  215. c->m_next = m_contactList;
  216. if (m_contactList != NULL)
  217. {
  218. m_contactList->m_prev = c;
  219. }
  220. m_contactList = c;
  221. // Connect to island graph.
  222. // Connect to body A
  223. c->m_nodeA.contact = c;
  224. c->m_nodeA.other = bodyB;
  225. c->m_nodeA.prev = NULL;
  226. c->m_nodeA.next = bodyA->m_contactList;
  227. if (bodyA->m_contactList != NULL)
  228. {
  229. bodyA->m_contactList->prev = &c->m_nodeA;
  230. }
  231. bodyA->m_contactList = &c->m_nodeA;
  232. // Connect to body B
  233. c->m_nodeB.contact = c;
  234. c->m_nodeB.other = bodyA;
  235. c->m_nodeB.prev = NULL;
  236. c->m_nodeB.next = bodyB->m_contactList;
  237. if (bodyB->m_contactList != NULL)
  238. {
  239. bodyB->m_contactList->prev = &c->m_nodeB;
  240. }
  241. bodyB->m_contactList = &c->m_nodeB;
  242. // Wake up the bodies
  243. if (fixtureA->IsSensor() == false && fixtureB->IsSensor() == false)
  244. {
  245. bodyA->SetAwake(true);
  246. bodyB->SetAwake(true);
  247. }
  248. ++m_contactCount;
  249. }