b2Contact.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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/Contacts/b2Contact.h>
  19. #include <Box2D/Dynamics/Contacts/b2CircleContact.h>
  20. #include <Box2D/Dynamics/Contacts/b2PolygonAndCircleContact.h>
  21. #include <Box2D/Dynamics/Contacts/b2PolygonContact.h>
  22. #include <Box2D/Dynamics/Contacts/b2EdgeAndCircleContact.h>
  23. #include <Box2D/Dynamics/Contacts/b2EdgeAndPolygonContact.h>
  24. #include <Box2D/Dynamics/Contacts/b2ChainAndCircleContact.h>
  25. #include <Box2D/Dynamics/Contacts/b2ChainAndPolygonContact.h>
  26. #include <Box2D/Dynamics/Contacts/b2ContactSolver.h>
  27. #include <Box2D/Collision/b2Collision.h>
  28. #include <Box2D/Collision/b2TimeOfImpact.h>
  29. #include <Box2D/Collision/Shapes/b2Shape.h>
  30. #include <Box2D/Common/b2BlockAllocator.h>
  31. #include <Box2D/Dynamics/b2Body.h>
  32. #include <Box2D/Dynamics/b2Fixture.h>
  33. #include <Box2D/Dynamics/b2World.h>
  34. b2ContactRegister b2Contact::s_registers[b2Shape::e_typeCount][b2Shape::e_typeCount];
  35. bool b2Contact::s_initialized = false;
  36. void b2Contact::InitializeRegisters()
  37. {
  38. AddType(b2CircleContact::Create, b2CircleContact::Destroy, b2Shape::e_circle, b2Shape::e_circle);
  39. AddType(b2PolygonAndCircleContact::Create, b2PolygonAndCircleContact::Destroy, b2Shape::e_polygon, b2Shape::e_circle);
  40. AddType(b2PolygonContact::Create, b2PolygonContact::Destroy, b2Shape::e_polygon, b2Shape::e_polygon);
  41. AddType(b2EdgeAndCircleContact::Create, b2EdgeAndCircleContact::Destroy, b2Shape::e_edge, b2Shape::e_circle);
  42. AddType(b2EdgeAndPolygonContact::Create, b2EdgeAndPolygonContact::Destroy, b2Shape::e_edge, b2Shape::e_polygon);
  43. AddType(b2ChainAndCircleContact::Create, b2ChainAndCircleContact::Destroy, b2Shape::e_chain, b2Shape::e_circle);
  44. AddType(b2ChainAndPolygonContact::Create, b2ChainAndPolygonContact::Destroy, b2Shape::e_chain, b2Shape::e_polygon);
  45. }
  46. void b2Contact::AddType(b2ContactCreateFcn* createFcn, b2ContactDestroyFcn* destoryFcn,
  47. b2Shape::Type type1, b2Shape::Type type2)
  48. {
  49. b2Assert(0 <= type1 && type1 < b2Shape::e_typeCount);
  50. b2Assert(0 <= type2 && type2 < b2Shape::e_typeCount);
  51. s_registers[type1][type2].createFcn = createFcn;
  52. s_registers[type1][type2].destroyFcn = destoryFcn;
  53. s_registers[type1][type2].primary = true;
  54. if (type1 != type2)
  55. {
  56. s_registers[type2][type1].createFcn = createFcn;
  57. s_registers[type2][type1].destroyFcn = destoryFcn;
  58. s_registers[type2][type1].primary = false;
  59. }
  60. }
  61. b2Contact* b2Contact::Create(b2Fixture* fixtureA, int32 indexA, b2Fixture* fixtureB, int32 indexB, b2BlockAllocator* allocator)
  62. {
  63. if (s_initialized == false)
  64. {
  65. InitializeRegisters();
  66. s_initialized = true;
  67. }
  68. b2Shape::Type type1 = fixtureA->GetType();
  69. b2Shape::Type type2 = fixtureB->GetType();
  70. b2Assert(0 <= type1 && type1 < b2Shape::e_typeCount);
  71. b2Assert(0 <= type2 && type2 < b2Shape::e_typeCount);
  72. b2ContactCreateFcn* createFcn = s_registers[type1][type2].createFcn;
  73. if (createFcn)
  74. {
  75. if (s_registers[type1][type2].primary)
  76. {
  77. return createFcn(fixtureA, indexA, fixtureB, indexB, allocator);
  78. }
  79. else
  80. {
  81. return createFcn(fixtureB, indexB, fixtureA, indexA, allocator);
  82. }
  83. }
  84. else
  85. {
  86. return NULL;
  87. }
  88. }
  89. void b2Contact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
  90. {
  91. b2Assert(s_initialized == true);
  92. b2Fixture* fixtureA = contact->m_fixtureA;
  93. b2Fixture* fixtureB = contact->m_fixtureB;
  94. if (contact->m_manifold.pointCount > 0 &&
  95. fixtureA->IsSensor() == false &&
  96. fixtureB->IsSensor() == false)
  97. {
  98. fixtureA->GetBody()->SetAwake(true);
  99. fixtureB->GetBody()->SetAwake(true);
  100. }
  101. b2Shape::Type typeA = fixtureA->GetType();
  102. b2Shape::Type typeB = fixtureB->GetType();
  103. b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
  104. b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
  105. b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn;
  106. destroyFcn(contact, allocator);
  107. }
  108. b2Contact::b2Contact(b2Fixture* fA, int32 indexA, b2Fixture* fB, int32 indexB)
  109. {
  110. m_flags = e_enabledFlag;
  111. m_fixtureA = fA;
  112. m_fixtureB = fB;
  113. m_indexA = indexA;
  114. m_indexB = indexB;
  115. m_manifold.pointCount = 0;
  116. m_prev = NULL;
  117. m_next = NULL;
  118. m_nodeA.contact = NULL;
  119. m_nodeA.prev = NULL;
  120. m_nodeA.next = NULL;
  121. m_nodeA.other = NULL;
  122. m_nodeB.contact = NULL;
  123. m_nodeB.prev = NULL;
  124. m_nodeB.next = NULL;
  125. m_nodeB.other = NULL;
  126. m_toiCount = 0;
  127. m_friction = b2MixFriction(m_fixtureA->m_friction, m_fixtureB->m_friction);
  128. m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution);
  129. m_tangentSpeed = 0.0f;
  130. }
  131. // Update the contact manifold and touching status.
  132. // Note: do not assume the fixture AABBs are overlapping or are valid.
  133. void b2Contact::Update(b2ContactListener* listener)
  134. {
  135. b2Manifold oldManifold = m_manifold;
  136. // Re-enable this contact.
  137. m_flags |= e_enabledFlag;
  138. bool touching = false;
  139. bool wasTouching = (m_flags & e_touchingFlag) == e_touchingFlag;
  140. bool sensorA = m_fixtureA->IsSensor();
  141. bool sensorB = m_fixtureB->IsSensor();
  142. bool sensor = sensorA || sensorB;
  143. b2Body* bodyA = m_fixtureA->GetBody();
  144. b2Body* bodyB = m_fixtureB->GetBody();
  145. const b2Transform& xfA = bodyA->GetTransform();
  146. const b2Transform& xfB = bodyB->GetTransform();
  147. // Is this contact a sensor?
  148. if (sensor)
  149. {
  150. const b2Shape* shapeA = m_fixtureA->GetShape();
  151. const b2Shape* shapeB = m_fixtureB->GetShape();
  152. touching = b2TestOverlap(shapeA, m_indexA, shapeB, m_indexB, xfA, xfB);
  153. // Sensors don't generate manifolds.
  154. m_manifold.pointCount = 0;
  155. }
  156. else
  157. {
  158. Evaluate(&m_manifold, xfA, xfB);
  159. touching = m_manifold.pointCount > 0;
  160. // Match old contact ids to new contact ids and copy the
  161. // stored impulses to warm start the solver.
  162. for (int32 i = 0; i < m_manifold.pointCount; ++i)
  163. {
  164. b2ManifoldPoint* mp2 = m_manifold.points + i;
  165. mp2->normalImpulse = 0.0f;
  166. mp2->tangentImpulse = 0.0f;
  167. b2ContactID id2 = mp2->id;
  168. for (int32 j = 0; j < oldManifold.pointCount; ++j)
  169. {
  170. b2ManifoldPoint* mp1 = oldManifold.points + j;
  171. if (mp1->id.key == id2.key)
  172. {
  173. mp2->normalImpulse = mp1->normalImpulse;
  174. mp2->tangentImpulse = mp1->tangentImpulse;
  175. break;
  176. }
  177. }
  178. }
  179. if (touching != wasTouching)
  180. {
  181. bodyA->SetAwake(true);
  182. bodyB->SetAwake(true);
  183. }
  184. }
  185. if (touching)
  186. {
  187. m_flags |= e_touchingFlag;
  188. }
  189. else
  190. {
  191. m_flags &= ~e_touchingFlag;
  192. }
  193. if (wasTouching == false && touching == true && listener)
  194. {
  195. listener->BeginContact(this);
  196. }
  197. if (wasTouching == true && touching == false && listener)
  198. {
  199. listener->EndContact(this);
  200. }
  201. if (sensor == false && touching && listener)
  202. {
  203. listener->PreSolve(this, &oldManifold);
  204. }
  205. }