b2ContactManager.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. b2Assert(bodyA->m_type == b2_dynamicBody || bodyB->m_type == b2_dynamicBody);
  102. bool activeA = bodyA->IsAwake() && bodyA->m_type != b2_staticBody;
  103. bool activeB = bodyB->IsAwake() && bodyB->m_type != b2_staticBody;
  104. // At least one body must be awake and it must be dynamic or kinematic.
  105. if (activeA == false && activeB == false)
  106. {
  107. c = c->GetNext();
  108. continue;
  109. }
  110. // Is this contact flagged for filtering?
  111. if (c->m_flags & b2Contact::e_filterFlag)
  112. {
  113. // Should these bodies collide?
  114. if (bodyB->ShouldCollide(bodyA) == false)
  115. {
  116. b2Contact* cNuke = c;
  117. c = cNuke->GetNext();
  118. Destroy(cNuke);
  119. continue;
  120. }
  121. // Check user filtering.
  122. if (m_contactFilter && m_contactFilter->ShouldCollide(fixtureA, fixtureB) == false)
  123. {
  124. b2Contact* cNuke = c;
  125. c = cNuke->GetNext();
  126. Destroy(cNuke);
  127. continue;
  128. }
  129. // Clear the filtering flag.
  130. c->m_flags &= ~b2Contact::e_filterFlag;
  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. // Does a contact already exist?
  168. b2ContactEdge* edge = bodyB->GetContactList();
  169. while (edge)
  170. {
  171. if (edge->other == bodyA)
  172. {
  173. b2Fixture* fA = edge->contact->GetFixtureA();
  174. b2Fixture* fB = edge->contact->GetFixtureB();
  175. int32 iA = edge->contact->GetChildIndexA();
  176. int32 iB = edge->contact->GetChildIndexB();
  177. if (fA == fixtureA && fB == fixtureB && iA == indexA && iB == indexB)
  178. {
  179. // A contact already exists.
  180. return;
  181. }
  182. if (fA == fixtureB && fB == fixtureA && iA == indexB && iB == indexA)
  183. {
  184. // A contact already exists.
  185. return;
  186. }
  187. }
  188. edge = edge->next;
  189. }
  190. // Does a joint override collision? Is at least one body dynamic?
  191. if (bodyB->ShouldCollide(bodyA) == false)
  192. {
  193. return;
  194. }
  195. // Check user filtering.
  196. if (m_contactFilter && m_contactFilter->ShouldCollide(fixtureA, fixtureB) == false)
  197. {
  198. return;
  199. }
  200. // Call the factory.
  201. b2Contact* c = b2Contact::Create(fixtureA, indexA, fixtureB, indexB, m_allocator);
  202. // Contact creation may swap fixtures.
  203. fixtureA = c->GetFixtureA();
  204. fixtureB = c->GetFixtureB();
  205. indexA = c->GetChildIndexA();
  206. indexB = c->GetChildIndexB();
  207. bodyA = fixtureA->GetBody();
  208. bodyB = fixtureB->GetBody();
  209. // Insert into the world.
  210. c->m_prev = NULL;
  211. c->m_next = m_contactList;
  212. if (m_contactList != NULL)
  213. {
  214. m_contactList->m_prev = c;
  215. }
  216. m_contactList = c;
  217. // Connect to island graph.
  218. // Connect to body A
  219. c->m_nodeA.contact = c;
  220. c->m_nodeA.other = bodyB;
  221. c->m_nodeA.prev = NULL;
  222. c->m_nodeA.next = bodyA->m_contactList;
  223. if (bodyA->m_contactList != NULL)
  224. {
  225. bodyA->m_contactList->prev = &c->m_nodeA;
  226. }
  227. bodyA->m_contactList = &c->m_nodeA;
  228. // Connect to body B
  229. c->m_nodeB.contact = c;
  230. c->m_nodeB.other = bodyA;
  231. c->m_nodeB.prev = NULL;
  232. c->m_nodeB.next = bodyB->m_contactList;
  233. if (bodyB->m_contactList != NULL)
  234. {
  235. bodyB->m_contactList->prev = &c->m_nodeB;
  236. }
  237. bodyB->m_contactList = &c->m_nodeB;
  238. ++m_contactCount;
  239. }