b2ContactManager.cpp 6.9 KB

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