b2Contact.cpp 7.6 KB

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