b2PolyAndEdgeContact.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 "b2PolyAndEdgeContact.h"
  19. #include "../b2Body.h"
  20. #include "../b2WorldCallbacks.h"
  21. #include "../../Common/b2BlockAllocator.h"
  22. #include "../../Collision/Shapes/b2EdgeShape.h"
  23. #include "../../Collision/Shapes/b2PolygonShape.h"
  24. #include <new>
  25. #include <cstring>
  26. b2Contact* b2PolyAndEdgeContact::Create(b2Shape* shape1, b2Shape* shape2, b2BlockAllocator* allocator)
  27. {
  28. void* mem = allocator->Allocate(sizeof(b2PolyAndEdgeContact));
  29. return new (mem) b2PolyAndEdgeContact(shape1, shape2);
  30. }
  31. void b2PolyAndEdgeContact::Destroy(b2Contact* contact, b2BlockAllocator* allocator)
  32. {
  33. ((b2PolyAndEdgeContact*)contact)->~b2PolyAndEdgeContact();
  34. allocator->Free(contact, sizeof(b2PolyAndEdgeContact));
  35. }
  36. b2PolyAndEdgeContact::b2PolyAndEdgeContact(b2Shape* s1, b2Shape* s2)
  37. : b2Contact(s1, s2)
  38. {
  39. b2Assert(m_shape1->GetType() == e_polygonShape);
  40. b2Assert(m_shape2->GetType() == e_edgeShape);
  41. m_manifold.pointCount = 0;
  42. }
  43. void b2PolyAndEdgeContact::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. b2CollidePolyAndEdge(&m_manifold, (b2PolygonShape*)m_shape1, b1->GetXForm(), (b2EdgeShape*)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 = b2MixFriction(m_shape1->GetFriction(), m_shape2->GetFriction());
  55. cp.restitution = b2MixRestitution(m_shape1->GetRestitution(), m_shape2->GetRestitution());
  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. }
  138. void b2PolyAndEdgeContact::b2CollidePolyAndEdge(b2Manifold* manifold,
  139. const b2PolygonShape* polygon,
  140. const b2XForm& xf1,
  141. const b2EdgeShape* edge,
  142. const b2XForm& xf2)
  143. {
  144. manifold->pointCount = 0;
  145. b2Vec2 v1 = b2Mul(xf2, edge->GetVertex1());
  146. b2Vec2 v2 = b2Mul(xf2, edge->GetVertex2());
  147. b2Vec2 n = b2Mul(xf2.R, edge->GetNormalVector());
  148. b2Vec2 v1Local = b2MulT(xf1, v1);
  149. b2Vec2 v2Local = b2MulT(xf1, v2);
  150. b2Vec2 nLocal = b2MulT(xf1.R, n);
  151. float32 separation1;
  152. int32 separationIndex1 = -1; // which normal on the poly found the shallowest depth?
  153. float32 separationMax1 = -B2_FLT_MAX; // the shallowest depth of edge in poly
  154. float32 separation2;
  155. int32 separationIndex2 = -1; // which normal on the poly found the shallowest depth?
  156. float32 separationMax2 = -B2_FLT_MAX; // the shallowest depth of edge in poly
  157. float32 separationMax = -B2_FLT_MAX; // the shallowest depth of edge in poly
  158. bool separationV1 = false; // is the shallowest depth from edge's v1 or v2 vertex?
  159. int32 separationIndex = -1; // which normal on the poly found the shallowest depth?
  160. int32 vertexCount = polygon->GetVertexCount();
  161. const b2Vec2* vertices = polygon->GetVertices();
  162. const b2Vec2* normals = polygon->GetNormals();
  163. int32 enterStartIndex = -1; // the last poly vertex above the edge
  164. int32 enterEndIndex = -1; // the first poly vertex below the edge
  165. int32 exitStartIndex = -1; // the last poly vertex below the edge
  166. int32 exitEndIndex = -1; // the first poly vertex above the edge
  167. //int32 deepestIndex;
  168. // the "N" in the following variables refers to the edge's normal.
  169. // these are projections of poly vertices along the edge's normal,
  170. // a.k.a. they are the separation of the poly from the edge.
  171. float32 prevSepN = 0.0f;
  172. float32 nextSepN = 0.0f;
  173. float32 enterSepN = 0.0f; // the depth of enterEndIndex under the edge (stored as a separation, so it's negative)
  174. float32 exitSepN = 0.0f; // the depth of exitStartIndex under the edge (stored as a separation, so it's negative)
  175. float32 deepestSepN = B2_FLT_MAX; // the depth of the deepest poly vertex under the end (stored as a separation, so it's negative)
  176. // for each poly normal, get the edge's depth into the poly.
  177. // for each poly vertex, get the vertex's depth into the edge.
  178. // use these calculations to define the remaining variables declared above.
  179. prevSepN = b2Dot(vertices[vertexCount-1] - v1Local, nLocal);
  180. for (int32 i = 0; i < vertexCount; i++)
  181. {
  182. separation1 = b2Dot(v1Local - vertices[i], normals[i]);
  183. separation2 = b2Dot(v2Local - vertices[i], normals[i]);
  184. if (separation2 < separation1) {
  185. if (separation2 > separationMax) {
  186. separationMax = separation2;
  187. separationV1 = false;
  188. separationIndex = i;
  189. }
  190. } else {
  191. if (separation1 > separationMax) {
  192. separationMax = separation1;
  193. separationV1 = true;
  194. separationIndex = i;
  195. }
  196. }
  197. if (separation1 > separationMax1) {
  198. separationMax1 = separation1;
  199. separationIndex1 = i;
  200. }
  201. if (separation2 > separationMax2) {
  202. separationMax2 = separation2;
  203. separationIndex2 = i;
  204. }
  205. nextSepN = b2Dot(vertices[i] - v1Local, nLocal);
  206. if (nextSepN >= 0.0f && prevSepN < 0.0f) {
  207. exitStartIndex = (i == 0) ? vertexCount-1 : i-1;
  208. exitEndIndex = i;
  209. exitSepN = prevSepN;
  210. } else if (nextSepN < 0.0f && prevSepN >= 0.0f) {
  211. enterStartIndex = (i == 0) ? vertexCount-1 : i-1;
  212. enterEndIndex = i;
  213. enterSepN = nextSepN;
  214. }
  215. if (nextSepN < deepestSepN) {
  216. deepestSepN = nextSepN;
  217. //deepestIndex = i;
  218. }
  219. prevSepN = nextSepN;
  220. }
  221. if (enterStartIndex == -1) {
  222. // poly is entirely below or entirely above edge, return with no contact:
  223. return;
  224. }
  225. if (separationMax > 0.0f) {
  226. // poly is laterally disjoint with edge, return with no contact:
  227. return;
  228. }
  229. // if the poly is near a convex corner on the edge
  230. if ((separationV1 && edge->Corner1IsConvex()) || (!separationV1 && edge->Corner2IsConvex())) {
  231. // if shallowest depth was from edge into poly,
  232. // use the edge's vertex as the contact point:
  233. if (separationMax > deepestSepN + b2_linearSlop) {
  234. // if -normal angle is closer to adjacent edge than this edge,
  235. // let the adjacent edge handle it and return with no contact:
  236. if (separationV1) {
  237. if (b2Dot(normals[separationIndex1], b2MulT(xf1.R, b2Mul(xf2.R, edge->GetCorner1Vector()))) >= 0.0f) {
  238. return;
  239. }
  240. } else {
  241. if (b2Dot(normals[separationIndex2], b2MulT(xf1.R, b2Mul(xf2.R, edge->GetCorner2Vector()))) <= 0.0f) {
  242. return;
  243. }
  244. }
  245. manifold->pointCount = 1;
  246. manifold->normal = b2Mul(xf1.R, normals[separationIndex]);
  247. manifold->points[0].separation = separationMax;
  248. manifold->points[0].id.features.incidentEdge = (uint8)separationIndex;
  249. manifold->points[0].id.features.incidentVertex = b2_nullFeature;
  250. manifold->points[0].id.features.referenceEdge = 0;
  251. manifold->points[0].id.features.flip = 0;
  252. if (separationV1) {
  253. manifold->points[0].localPoint1 = v1Local;
  254. manifold->points[0].localPoint2 = edge->GetVertex1();
  255. } else {
  256. manifold->points[0].localPoint1 = v2Local;
  257. manifold->points[0].localPoint2 = edge->GetVertex2();
  258. }
  259. return;
  260. }
  261. }
  262. // We're going to use the edge's normal now.
  263. manifold->normal = (-1.0f) * n;
  264. // Check whether we only need one contact point.
  265. if (enterEndIndex == exitStartIndex) {
  266. manifold->pointCount = 1;
  267. manifold->points[0].id.features.incidentEdge = (uint8)enterEndIndex;
  268. manifold->points[0].id.features.incidentVertex = b2_nullFeature;
  269. manifold->points[0].id.features.referenceEdge = 0;
  270. manifold->points[0].id.features.flip = 0;
  271. manifold->points[0].localPoint1 = vertices[enterEndIndex];
  272. manifold->points[0].localPoint2 = b2MulT(xf2, b2Mul(xf1, vertices[enterEndIndex]));
  273. manifold->points[0].separation = enterSepN;
  274. return;
  275. }
  276. manifold->pointCount = 2;
  277. // dirLocal should be the edge's direction vector, but in the frame of the polygon.
  278. b2Vec2 dirLocal = b2Cross(nLocal, -1.0f); // TODO: figure out why this optimization didn't work
  279. //b2Vec2 dirLocal = b2MulT(xf1.R, b2Mul(xf2.R, edge->GetDirectionVector()));
  280. float32 dirProj1 = b2Dot(dirLocal, vertices[enterEndIndex] - v1Local);
  281. float32 dirProj2;
  282. // The contact resolution is more robust if the two manifold points are
  283. // adjacent to each other on the polygon. So pick the first two poly
  284. // vertices that are under the edge:
  285. exitEndIndex = (enterEndIndex == vertexCount - 1) ? 0 : enterEndIndex + 1;
  286. if (exitEndIndex != exitStartIndex) {
  287. exitStartIndex = exitEndIndex;
  288. exitSepN = b2Dot(nLocal, vertices[exitStartIndex] - v1Local);
  289. }
  290. dirProj2 = b2Dot(dirLocal, vertices[exitStartIndex] - v1Local);
  291. manifold->points[0].id.features.incidentEdge = (uint8)enterEndIndex;
  292. manifold->points[0].id.features.incidentVertex = b2_nullFeature;
  293. manifold->points[0].id.features.referenceEdge = 0;
  294. manifold->points[0].id.features.flip = 0;
  295. if (dirProj1 > edge->GetLength()) {
  296. manifold->points[0].localPoint1 = v2Local;
  297. manifold->points[0].localPoint2 = edge->GetVertex2();
  298. float32 ratio = (edge->GetLength() - dirProj2) / (dirProj1 - dirProj2);
  299. if (ratio > 100.0f * B2_FLT_EPSILON && ratio < 1.0f) {
  300. manifold->points[0].separation = exitSepN * (1.0f - ratio) + enterSepN * ratio;
  301. } else {
  302. manifold->points[0].separation = enterSepN;
  303. }
  304. } else {
  305. manifold->points[0].localPoint1 = vertices[enterEndIndex];
  306. manifold->points[0].localPoint2 = b2MulT(xf2, b2Mul(xf1, vertices[enterEndIndex]));
  307. manifold->points[0].separation = enterSepN;
  308. }
  309. manifold->points[1].id.features.incidentEdge = (uint8)exitStartIndex;
  310. manifold->points[1].id.features.incidentVertex = b2_nullFeature;
  311. manifold->points[1].id.features.referenceEdge = 0;
  312. manifold->points[1].id.features.flip = 0;
  313. if (dirProj2 < 0.0f) {
  314. manifold->points[1].localPoint1 = v1Local;
  315. manifold->points[1].localPoint2 = edge->GetVertex1();
  316. float32 ratio = (-dirProj1) / (dirProj2 - dirProj1);
  317. if (ratio > 100.0f * B2_FLT_EPSILON && ratio < 1.0f) {
  318. manifold->points[1].separation = enterSepN * (1.0f - ratio) + exitSepN * ratio;
  319. } else {
  320. manifold->points[1].separation = exitSepN;
  321. }
  322. } else {
  323. manifold->points[1].localPoint1 = vertices[exitStartIndex];
  324. manifold->points[1].localPoint2 = b2MulT(xf2, b2Mul(xf1, vertices[exitStartIndex]));
  325. manifold->points[1].separation = exitSepN;
  326. }
  327. }