The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

241 lines
7.1KB

  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 "b2Contact.h"
  19. #include "b2CircleContact.h"
  20. #include "b2PolygonAndCircleContact.h"
  21. #include "b2PolygonContact.h"
  22. #include "b2EdgeAndCircleContact.h"
  23. #include "b2EdgeAndPolygonContact.h"
  24. #include "b2ChainAndCircleContact.h"
  25. #include "b2ChainAndPolygonContact.h"
  26. #include "b2ContactSolver.h"
  27. #include "../../Collision/b2Collision.h"
  28. #include "../../Collision/b2TimeOfImpact.h"
  29. #include "../../Collision/Shapes/b2Shape.h"
  30. #include "../../Common/b2BlockAllocator.h"
  31. #include "../b2Body.h"
  32. #include "../b2Fixture.h"
  33. #include "../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. if (contact->m_manifold.pointCount > 0)
  93. {
  94. contact->GetFixtureA()->GetBody()->SetAwake(true);
  95. contact->GetFixtureB()->GetBody()->SetAwake(true);
  96. }
  97. b2Shape::Type typeA = contact->GetFixtureA()->GetType();
  98. b2Shape::Type typeB = contact->GetFixtureB()->GetType();
  99. b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
  100. b2Assert(0 <= typeA && typeB < b2Shape::e_typeCount);
  101. b2ContactDestroyFcn* destroyFcn = s_registers[typeA][typeB].destroyFcn;
  102. destroyFcn(contact, allocator);
  103. }
  104. b2Contact::b2Contact(b2Fixture* fA, int32 indexA, b2Fixture* fB, int32 indexB)
  105. {
  106. m_flags = e_enabledFlag;
  107. m_fixtureA = fA;
  108. m_fixtureB = fB;
  109. m_indexA = indexA;
  110. m_indexB = indexB;
  111. m_manifold.pointCount = 0;
  112. m_prev = NULL;
  113. m_next = NULL;
  114. m_nodeA.contact = NULL;
  115. m_nodeA.prev = NULL;
  116. m_nodeA.next = NULL;
  117. m_nodeA.other = NULL;
  118. m_nodeB.contact = NULL;
  119. m_nodeB.prev = NULL;
  120. m_nodeB.next = NULL;
  121. m_nodeB.other = NULL;
  122. m_toiCount = 0;
  123. m_friction = b2MixFriction(m_fixtureA->m_friction, m_fixtureB->m_friction);
  124. m_restitution = b2MixRestitution(m_fixtureA->m_restitution, m_fixtureB->m_restitution);
  125. }
  126. // Update the contact manifold and touching status.
  127. // Note: do not assume the fixture AABBs are overlapping or are valid.
  128. void b2Contact::Update(b2ContactListener* listener)
  129. {
  130. b2Manifold oldManifold = m_manifold;
  131. // Re-enable this contact.
  132. m_flags |= e_enabledFlag;
  133. bool touching = false;
  134. bool wasTouching = (m_flags & e_touchingFlag) == e_touchingFlag;
  135. bool sensorA = m_fixtureA->IsSensor();
  136. bool sensorB = m_fixtureB->IsSensor();
  137. bool sensor = sensorA || sensorB;
  138. b2Body* bodyA = m_fixtureA->GetBody();
  139. b2Body* bodyB = m_fixtureB->GetBody();
  140. const b2Transform& xfA = bodyA->GetTransform();
  141. const b2Transform& xfB = bodyB->GetTransform();
  142. // Is this contact a sensor?
  143. if (sensor)
  144. {
  145. const b2Shape* shapeA = m_fixtureA->GetShape();
  146. const b2Shape* shapeB = m_fixtureB->GetShape();
  147. touching = b2TestOverlap(shapeA, m_indexA, shapeB, m_indexB, xfA, xfB);
  148. // Sensors don't generate manifolds.
  149. m_manifold.pointCount = 0;
  150. }
  151. else
  152. {
  153. Evaluate(&m_manifold, xfA, xfB);
  154. touching = m_manifold.pointCount > 0;
  155. // Match old contact ids to new contact ids and copy the
  156. // stored impulses to warm start the solver.
  157. for (int32 i = 0; i < m_manifold.pointCount; ++i)
  158. {
  159. b2ManifoldPoint* mp2 = m_manifold.points + i;
  160. mp2->normalImpulse = 0.0f;
  161. mp2->tangentImpulse = 0.0f;
  162. b2ContactID id2 = mp2->id;
  163. for (int32 j = 0; j < oldManifold.pointCount; ++j)
  164. {
  165. b2ManifoldPoint* mp1 = oldManifold.points + j;
  166. if (mp1->id.key == id2.key)
  167. {
  168. mp2->normalImpulse = mp1->normalImpulse;
  169. mp2->tangentImpulse = mp1->tangentImpulse;
  170. break;
  171. }
  172. }
  173. }
  174. if (touching != wasTouching)
  175. {
  176. bodyA->SetAwake(true);
  177. bodyB->SetAwake(true);
  178. }
  179. }
  180. if (touching)
  181. {
  182. m_flags |= e_touchingFlag;
  183. }
  184. else
  185. {
  186. m_flags &= ~e_touchingFlag;
  187. }
  188. if (wasTouching == false && touching == true && listener)
  189. {
  190. listener->BeginContact(this);
  191. }
  192. if (wasTouching == true && touching == false && listener)
  193. {
  194. listener->EndContact(this);
  195. }
  196. if (sensor == false && touching && listener)
  197. {
  198. listener->PreSolve(this, &oldManifold);
  199. }
  200. }