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.

318 lines
8.8KB

  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 "b2Collision.h"
  19. #include "Shapes/b2PolygonShape.h"
  20. // Find the separation between poly1 and poly2 for a give edge normal on poly1.
  21. static float32 b2EdgeSeparation(const b2PolygonShape* poly1, const b2Transform& xf1, int32 edge1,
  22. const b2PolygonShape* poly2, const b2Transform& xf2)
  23. {
  24. const b2Vec2* vertices1 = poly1->m_vertices;
  25. const b2Vec2* normals1 = poly1->m_normals;
  26. int32 count2 = poly2->m_vertexCount;
  27. const b2Vec2* vertices2 = poly2->m_vertices;
  28. b2Assert(0 <= edge1 && edge1 < poly1->m_vertexCount);
  29. // Convert normal from poly1's frame into poly2's frame.
  30. b2Vec2 normal1World = b2Mul(xf1.q, normals1[edge1]);
  31. b2Vec2 normal1 = b2MulT(xf2.q, normal1World);
  32. // Find support vertex on poly2 for -normal.
  33. int32 index = 0;
  34. float32 minDot = b2_maxFloat;
  35. for (int32 i = 0; i < count2; ++i)
  36. {
  37. float32 dot = b2Dot(vertices2[i], normal1);
  38. if (dot < minDot)
  39. {
  40. minDot = dot;
  41. index = i;
  42. }
  43. }
  44. b2Vec2 v1 = b2Mul(xf1, vertices1[edge1]);
  45. b2Vec2 v2 = b2Mul(xf2, vertices2[index]);
  46. float32 separation = b2Dot(v2 - v1, normal1World);
  47. return separation;
  48. }
  49. // Find the max separation between poly1 and poly2 using edge normals from poly1.
  50. static float32 b2FindMaxSeparation(int32* edgeIndex,
  51. const b2PolygonShape* poly1, const b2Transform& xf1,
  52. const b2PolygonShape* poly2, const b2Transform& xf2)
  53. {
  54. int32 count1 = poly1->m_vertexCount;
  55. const b2Vec2* normals1 = poly1->m_normals;
  56. // Vector pointing from the centroid of poly1 to the centroid of poly2.
  57. b2Vec2 d = b2Mul(xf2, poly2->m_centroid) - b2Mul(xf1, poly1->m_centroid);
  58. b2Vec2 dLocal1 = b2MulT(xf1.q, d);
  59. // Find edge normal on poly1 that has the largest projection onto d.
  60. int32 edge = 0;
  61. float32 maxDot = -b2_maxFloat;
  62. for (int32 i = 0; i < count1; ++i)
  63. {
  64. float32 dot = b2Dot(normals1[i], dLocal1);
  65. if (dot > maxDot)
  66. {
  67. maxDot = dot;
  68. edge = i;
  69. }
  70. }
  71. // Get the separation for the edge normal.
  72. float32 s = b2EdgeSeparation(poly1, xf1, edge, poly2, xf2);
  73. // Check the separation for the previous edge normal.
  74. int32 prevEdge = edge - 1 >= 0 ? edge - 1 : count1 - 1;
  75. float32 sPrev = b2EdgeSeparation(poly1, xf1, prevEdge, poly2, xf2);
  76. // Check the separation for the next edge normal.
  77. int32 nextEdge = edge + 1 < count1 ? edge + 1 : 0;
  78. float32 sNext = b2EdgeSeparation(poly1, xf1, nextEdge, poly2, xf2);
  79. // Find the best edge and the search direction.
  80. int32 bestEdge;
  81. float32 bestSeparation;
  82. int32 increment;
  83. if (sPrev > s && sPrev > sNext)
  84. {
  85. increment = -1;
  86. bestEdge = prevEdge;
  87. bestSeparation = sPrev;
  88. }
  89. else if (sNext > s)
  90. {
  91. increment = 1;
  92. bestEdge = nextEdge;
  93. bestSeparation = sNext;
  94. }
  95. else
  96. {
  97. *edgeIndex = edge;
  98. return s;
  99. }
  100. // Perform a local search for the best edge normal.
  101. for ( ; ; )
  102. {
  103. if (increment == -1)
  104. edge = bestEdge - 1 >= 0 ? bestEdge - 1 : count1 - 1;
  105. else
  106. edge = bestEdge + 1 < count1 ? bestEdge + 1 : 0;
  107. s = b2EdgeSeparation(poly1, xf1, edge, poly2, xf2);
  108. if (s > bestSeparation)
  109. {
  110. bestEdge = edge;
  111. bestSeparation = s;
  112. }
  113. else
  114. {
  115. break;
  116. }
  117. }
  118. *edgeIndex = bestEdge;
  119. return bestSeparation;
  120. }
  121. static void b2FindIncidentEdge(b2ClipVertex c[2],
  122. const b2PolygonShape* poly1, const b2Transform& xf1, int32 edge1,
  123. const b2PolygonShape* poly2, const b2Transform& xf2)
  124. {
  125. const b2Vec2* normals1 = poly1->m_normals;
  126. int32 count2 = poly2->m_vertexCount;
  127. const b2Vec2* vertices2 = poly2->m_vertices;
  128. const b2Vec2* normals2 = poly2->m_normals;
  129. b2Assert(0 <= edge1 && edge1 < poly1->m_vertexCount);
  130. // Get the normal of the reference edge in poly2's frame.
  131. b2Vec2 normal1 = b2MulT(xf2.q, b2Mul(xf1.q, normals1[edge1]));
  132. // Find the incident edge on poly2.
  133. int32 index = 0;
  134. float32 minDot = b2_maxFloat;
  135. for (int32 i = 0; i < count2; ++i)
  136. {
  137. float32 dot = b2Dot(normal1, normals2[i]);
  138. if (dot < minDot)
  139. {
  140. minDot = dot;
  141. index = i;
  142. }
  143. }
  144. // Build the clip vertices for the incident edge.
  145. int32 i1 = index;
  146. int32 i2 = i1 + 1 < count2 ? i1 + 1 : 0;
  147. c[0].v = b2Mul(xf2, vertices2[i1]);
  148. c[0].id.cf.indexA = (uint8)edge1;
  149. c[0].id.cf.indexB = (uint8)i1;
  150. c[0].id.cf.typeA = b2ContactFeature::e_face;
  151. c[0].id.cf.typeB = b2ContactFeature::e_vertex;
  152. c[1].v = b2Mul(xf2, vertices2[i2]);
  153. c[1].id.cf.indexA = (uint8)edge1;
  154. c[1].id.cf.indexB = (uint8)i2;
  155. c[1].id.cf.typeA = b2ContactFeature::e_face;
  156. c[1].id.cf.typeB = b2ContactFeature::e_vertex;
  157. }
  158. // Find edge normal of max separation on A - return if separating axis is found
  159. // Find edge normal of max separation on B - return if separation axis is found
  160. // Choose reference edge as min(minA, minB)
  161. // Find incident edge
  162. // Clip
  163. // The normal points from 1 to 2
  164. void b2CollidePolygons(b2Manifold* manifold,
  165. const b2PolygonShape* polyA, const b2Transform& xfA,
  166. const b2PolygonShape* polyB, const b2Transform& xfB)
  167. {
  168. manifold->pointCount = 0;
  169. float32 totalRadius = polyA->m_radius + polyB->m_radius;
  170. int32 edgeA = 0;
  171. float32 separationA = b2FindMaxSeparation(&edgeA, polyA, xfA, polyB, xfB);
  172. if (separationA > totalRadius)
  173. return;
  174. int32 edgeB = 0;
  175. float32 separationB = b2FindMaxSeparation(&edgeB, polyB, xfB, polyA, xfA);
  176. if (separationB > totalRadius)
  177. return;
  178. const b2PolygonShape* poly1; // reference polygon
  179. const b2PolygonShape* poly2; // incident polygon
  180. b2Transform xf1, xf2;
  181. int32 edge1; // reference edge
  182. uint8 flip;
  183. const float32 k_relativeTol = 0.98f;
  184. const float32 k_absoluteTol = 0.001f;
  185. if (separationB > k_relativeTol * separationA + k_absoluteTol)
  186. {
  187. poly1 = polyB;
  188. poly2 = polyA;
  189. xf1 = xfB;
  190. xf2 = xfA;
  191. edge1 = edgeB;
  192. manifold->type = b2Manifold::e_faceB;
  193. flip = 1;
  194. }
  195. else
  196. {
  197. poly1 = polyA;
  198. poly2 = polyB;
  199. xf1 = xfA;
  200. xf2 = xfB;
  201. edge1 = edgeA;
  202. manifold->type = b2Manifold::e_faceA;
  203. flip = 0;
  204. }
  205. b2ClipVertex incidentEdge[2];
  206. b2FindIncidentEdge(incidentEdge, poly1, xf1, edge1, poly2, xf2);
  207. int32 count1 = poly1->m_vertexCount;
  208. const b2Vec2* vertices1 = poly1->m_vertices;
  209. int32 iv1 = edge1;
  210. int32 iv2 = edge1 + 1 < count1 ? edge1 + 1 : 0;
  211. b2Vec2 v11 = vertices1[iv1];
  212. b2Vec2 v12 = vertices1[iv2];
  213. b2Vec2 localTangent = v12 - v11;
  214. localTangent.Normalize();
  215. b2Vec2 localNormal = b2Cross(localTangent, 1.0f);
  216. b2Vec2 planePoint = 0.5f * (v11 + v12);
  217. b2Vec2 tangent = b2Mul(xf1.q, localTangent);
  218. b2Vec2 normal = b2Cross(tangent, 1.0f);
  219. v11 = b2Mul(xf1, v11);
  220. v12 = b2Mul(xf1, v12);
  221. // Face offset.
  222. float32 frontOffset = b2Dot(normal, v11);
  223. // Side offsets, extended by polytope skin thickness.
  224. float32 sideOffset1 = -b2Dot(tangent, v11) + totalRadius;
  225. float32 sideOffset2 = b2Dot(tangent, v12) + totalRadius;
  226. // Clip incident edge against extruded edge1 side edges.
  227. b2ClipVertex clipPoints1[2];
  228. b2ClipVertex clipPoints2[2];
  229. int np;
  230. // Clip to box side 1
  231. np = b2ClipSegmentToLine(clipPoints1, incidentEdge, -tangent, sideOffset1, iv1);
  232. if (np < 2)
  233. return;
  234. // Clip to negative box side 1
  235. np = b2ClipSegmentToLine(clipPoints2, clipPoints1, tangent, sideOffset2, iv2);
  236. if (np < 2)
  237. {
  238. return;
  239. }
  240. // Now clipPoints2 contains the clipped points.
  241. manifold->localNormal = localNormal;
  242. manifold->localPoint = planePoint;
  243. int32 pointCount = 0;
  244. for (int32 i = 0; i < b2_maxManifoldPoints; ++i)
  245. {
  246. float32 separation = b2Dot(normal, clipPoints2[i].v) - frontOffset;
  247. if (separation <= totalRadius)
  248. {
  249. b2ManifoldPoint* cp = manifold->points + pointCount;
  250. cp->localPoint = b2MulT(xf2, clipPoints2[i].v);
  251. cp->id = clipPoints2[i].id;
  252. if (flip)
  253. {
  254. // Swap features
  255. b2ContactFeature cf = cp->id.cf;
  256. cp->id.cf.indexA = cf.indexB;
  257. cp->id.cf.indexB = cf.indexA;
  258. cp->id.cf.typeA = cf.typeB;
  259. cp->id.cf.typeB = cf.typeA;
  260. }
  261. ++pointCount;
  262. }
  263. }
  264. manifold->pointCount = pointCount;
  265. }