b2PolyAndCircleContact.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (c) 2006-2007 Erin Catto http://www.gphysics.com
  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 "b2PolyAndCircleContact.h"
  19. #include "../b2Body.h"
  20. #include "../b2WorldCallbacks.h"
  21. #include "../../Common/b2BlockAllocator.h"
  22. #include <new>
  23. #include <string.h>
  24. b2Contact* b2PolyAndCircleContact::Create(b2Shape* shape1, b2Shape* shape2, b2BlockAllocator* allocator)
  25. {
  26. void* mem = allocator->Allocate(sizeof(b2PolyAndCircleContact));
  27. return new (mem) b2PolyAndCircleContact(shape1, shape2);
  28. }
  29. void b2PolyAndCircleContact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
  30. {
  31. ((b2PolyAndCircleContact*)contact)->~b2PolyAndCircleContact();
  32. allocator->Free(contact, sizeof(b2PolyAndCircleContact));
  33. }
  34. b2PolyAndCircleContact::b2PolyAndCircleContact(b2Shape* s1, b2Shape* s2)
  35. : b2Contact(s1, s2)
  36. {
  37. b2Assert(m_shape1->GetType() == e_polygonShape);
  38. b2Assert(m_shape2->GetType() == e_circleShape);
  39. m_manifold.pointCount = 0;
  40. m_manifold.points[0].normalImpulse = 0.0f;
  41. m_manifold.points[0].tangentImpulse = 0.0f;
  42. }
  43. void b2PolyAndCircleContact::Evaluate(b2ContactListener* listener)
  44. {
  45. b2Body* b1 = m_shape1->GetBody();
  46. b2Body* b2 = m_shape2->GetBody();
  47. b2Manifold m0;
  48. memcpy(&m0, &m_manifold, sizeof(b2Manifold));
  49. b2CollidePolygonAndCircle(&m_manifold, (b2PolygonShape*)m_shape1, b1->GetXForm(), (b2CircleShape*)m_shape2, b2->GetXForm());
  50. bool persisted[b2_maxManifoldPoints] = {false, false};
  51. b2ContactPoint cp;
  52. cp.shape1 = m_shape1;
  53. cp.shape2 = m_shape2;
  54. cp.friction = m_friction;
  55. cp.restitution = m_restitution;
  56. // Match contact ids to facilitate warm starting.
  57. if (m_manifold.pointCount > 0)
  58. {
  59. // Match old contact ids to new contact ids and copy the
  60. // stored impulses to warm start the solver.
  61. for (int32 i = 0; i < m_manifold.pointCount; ++i)
  62. {
  63. b2ManifoldPoint* mp = m_manifold.points + i;
  64. mp->normalImpulse = 0.0f;
  65. mp->tangentImpulse = 0.0f;
  66. bool found = false;
  67. b2ContactID id = mp->id;
  68. for (int32 j = 0; j < m0.pointCount; ++j)
  69. {
  70. if (persisted[j] == true)
  71. {
  72. continue;
  73. }
  74. b2ManifoldPoint* mp0 = m0.points + j;
  75. if (mp0->id.key == id.key)
  76. {
  77. persisted[j] = true;
  78. mp->normalImpulse = mp0->normalImpulse;
  79. mp->tangentImpulse = mp0->tangentImpulse;
  80. // A persistent point.
  81. found = true;
  82. // Report persistent point.
  83. if (listener != NULL)
  84. {
  85. cp.position = b1->GetWorldPoint(mp->localPoint1);
  86. b2Vec2 v1 = b1->GetLinearVelocityFromLocalPoint(mp->localPoint1);
  87. b2Vec2 v2 = b2->GetLinearVelocityFromLocalPoint(mp->localPoint2);
  88. cp.velocity = v2 - v1;
  89. cp.normal = m_manifold.normal;
  90. cp.separation = mp->separation;
  91. cp.id = id;
  92. listener->Persist(&cp);
  93. }
  94. break;
  95. }
  96. }
  97. // Report added point.
  98. if (found == false && listener != NULL)
  99. {
  100. cp.position = b1->GetWorldPoint(mp->localPoint1);
  101. b2Vec2 v1 = b1->GetLinearVelocityFromLocalPoint(mp->localPoint1);
  102. b2Vec2 v2 = b2->GetLinearVelocityFromLocalPoint(mp->localPoint2);
  103. cp.velocity = v2 - v1;
  104. cp.normal = m_manifold.normal;
  105. cp.separation = mp->separation;
  106. cp.id = id;
  107. listener->Add(&cp);
  108. }
  109. }
  110. m_manifoldCount = 1;
  111. }
  112. else
  113. {
  114. m_manifoldCount = 0;
  115. }
  116. if (listener == NULL)
  117. {
  118. return;
  119. }
  120. // Report removed points.
  121. for (int32 i = 0; i < m0.pointCount; ++i)
  122. {
  123. if (persisted[i])
  124. {
  125. continue;
  126. }
  127. b2ManifoldPoint* mp0 = m0.points + i;
  128. cp.position = b1->GetWorldPoint(mp0->localPoint1);
  129. b2Vec2 v1 = b1->GetLinearVelocityFromLocalPoint(mp0->localPoint1);
  130. b2Vec2 v2 = b2->GetLinearVelocityFromLocalPoint(mp0->localPoint2);
  131. cp.velocity = v2 - v1;
  132. cp.normal = m0.normal;
  133. cp.separation = mp0->separation;
  134. cp.id = mp0->id;
  135. listener->Remove(&cp);
  136. }
  137. }