ManifoldBetweenTwoFaces.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. #include <Jolt/Physics/Collision/ManifoldBetweenTwoFaces.h>
  6. #include <Jolt/Physics/Constraints/ContactConstraintManager.h>
  7. #include <Jolt/Geometry/ClipPoly.h>
  8. #ifdef JPH_DEBUG_RENDERER
  9. #include <Jolt/Renderer/DebugRenderer.h>
  10. #endif // JPH_DEBUG_RENDERER
  11. JPH_NAMESPACE_BEGIN
  12. void PruneContactPoints(Vec3Arg inPenetrationAxis, ContactPoints &ioContactPointsOn1, ContactPoints &ioContactPointsOn2 JPH_IF_DEBUG_RENDERER(, RVec3Arg inCenterOfMass))
  13. {
  14. // Makes no sense to call this with 4 or less points
  15. JPH_ASSERT(ioContactPointsOn1.size() > 4);
  16. // Both arrays should have the same size
  17. JPH_ASSERT(ioContactPointsOn1.size() == ioContactPointsOn2.size());
  18. // Penetration axis must be normalized
  19. JPH_ASSERT(inPenetrationAxis.IsNormalized());
  20. // We use a heuristic of (distance to center of mass) * (penetration depth) to find the contact point that we should keep
  21. // Neither of those two terms should ever become zero, so we clamp against this minimum value
  22. constexpr float cMinDistanceSq = 1.0e-6f; // 1 mm
  23. ContactPoints projected;
  24. StaticArray<float, 64> penetration_depth_sq;
  25. for (ContactPoints::size_type i = 0; i < ioContactPointsOn1.size(); ++i)
  26. {
  27. // Project contact points on the plane through inCenterOfMass with normal inPenetrationAxis and center around the center of mass of body 1
  28. // (note that since all points are relative to inCenterOfMass we can project onto the plane through the origin)
  29. Vec3 v1 = ioContactPointsOn1[i];
  30. projected.push_back(v1 - v1.Dot(inPenetrationAxis) * inPenetrationAxis);
  31. // Calculate penetration depth^2 of each point and clamp against the minimal distance
  32. Vec3 v2 = ioContactPointsOn2[i];
  33. penetration_depth_sq.push_back(max(cMinDistanceSq, (v2 - v1).LengthSq()));
  34. }
  35. // Find the point that is furthest away from the center of mass (its torque will have the biggest influence)
  36. // and the point that has the deepest penetration depth. Use the heuristic (distance to center of mass) * (penetration depth) for this.
  37. uint point1 = 0;
  38. float val = max(cMinDistanceSq, projected[0].LengthSq()) * penetration_depth_sq[0];
  39. for (uint i = 0; i < projected.size(); ++i)
  40. {
  41. float v = max(cMinDistanceSq, projected[i].LengthSq()) * penetration_depth_sq[i];
  42. if (v > val)
  43. {
  44. val = v;
  45. point1 = i;
  46. }
  47. }
  48. Vec3 point1v = projected[point1];
  49. // Find point furthest from the first point forming a line segment with point1. Again combine this with the heuristic
  50. // for deepest point as per above.
  51. uint point2 = uint(-1);
  52. val = -FLT_MAX;
  53. for (uint i = 0; i < projected.size(); ++i)
  54. if (i != point1)
  55. {
  56. float v = max(cMinDistanceSq, (projected[i] - point1v).LengthSq()) * penetration_depth_sq[i];
  57. if (v > val)
  58. {
  59. val = v;
  60. point2 = i;
  61. }
  62. }
  63. JPH_ASSERT(point2 != uint(-1));
  64. Vec3 point2v = projected[point2];
  65. // Find furthest points on both sides of the line segment in order to maximize the area
  66. uint point3 = uint(-1);
  67. uint point4 = uint(-1);
  68. float min_val = 0.0f;
  69. float max_val = 0.0f;
  70. Vec3 perp = (point2v - point1v).Cross(inPenetrationAxis);
  71. for (uint i = 0; i < projected.size(); ++i)
  72. if (i != point1 && i != point2)
  73. {
  74. float v = perp.Dot(projected[i] - point1v);
  75. if (v < min_val)
  76. {
  77. min_val = v;
  78. point3 = i;
  79. }
  80. else if (v > max_val)
  81. {
  82. max_val = v;
  83. point4 = i;
  84. }
  85. }
  86. // Add points to array (in order so they form a polygon)
  87. StaticArray<Vec3, 4> points_to_keep_on_1, points_to_keep_on_2;
  88. points_to_keep_on_1.push_back(ioContactPointsOn1[point1]);
  89. points_to_keep_on_2.push_back(ioContactPointsOn2[point1]);
  90. if (point3 != uint(-1))
  91. {
  92. points_to_keep_on_1.push_back(ioContactPointsOn1[point3]);
  93. points_to_keep_on_2.push_back(ioContactPointsOn2[point3]);
  94. }
  95. points_to_keep_on_1.push_back(ioContactPointsOn1[point2]);
  96. points_to_keep_on_2.push_back(ioContactPointsOn2[point2]);
  97. if (point4 != uint(-1))
  98. {
  99. JPH_ASSERT(point3 != point4);
  100. points_to_keep_on_1.push_back(ioContactPointsOn1[point4]);
  101. points_to_keep_on_2.push_back(ioContactPointsOn2[point4]);
  102. }
  103. #ifdef JPH_DEBUG_RENDERER
  104. if (ContactConstraintManager::sDrawContactPointReduction)
  105. {
  106. // Draw input polygon
  107. DebugRenderer::sInstance->DrawWirePolygon(RMat44::sTranslation(inCenterOfMass), ioContactPointsOn1, Color::sOrange, 0.05f);
  108. // Draw primary axis
  109. DebugRenderer::sInstance->DrawArrow(inCenterOfMass + ioContactPointsOn1[point1], inCenterOfMass + ioContactPointsOn1[point2], Color::sRed, 0.05f);
  110. // Draw contact points we kept
  111. for (Vec3 p : points_to_keep_on_1)
  112. DebugRenderer::sInstance->DrawMarker(inCenterOfMass + p, Color::sGreen, 0.1f);
  113. }
  114. #endif // JPH_DEBUG_RENDERER
  115. // Copy the points back to the input buffer
  116. ioContactPointsOn1 = points_to_keep_on_1;
  117. ioContactPointsOn2 = points_to_keep_on_2;
  118. }
  119. void ManifoldBetweenTwoFaces(Vec3Arg inContactPoint1, Vec3Arg inContactPoint2, Vec3Arg inPenetrationAxis, float inMaxContactDistanceSq , const ConvexShape::SupportingFace &inShape1Face, const ConvexShape::SupportingFace &inShape2Face, ContactPoints &outContactPoints1, ContactPoints &outContactPoints2 JPH_IF_DEBUG_RENDERER(, RVec3Arg inCenterOfMass))
  120. {
  121. #ifdef JPH_DEBUG_RENDERER
  122. if (ContactConstraintManager::sDrawContactPoint)
  123. {
  124. RVec3 cp1 = inCenterOfMass + inContactPoint1;
  125. RVec3 cp2 = inCenterOfMass + inContactPoint2;
  126. // Draw contact points
  127. DebugRenderer::sInstance->DrawMarker(cp1, Color::sRed, 0.1f);
  128. DebugRenderer::sInstance->DrawMarker(cp2, Color::sGreen, 0.1f);
  129. // Draw contact normal
  130. DebugRenderer::sInstance->DrawArrow(cp1, cp1 + inPenetrationAxis.Normalized(), Color::sRed, 0.05f);
  131. }
  132. #endif // JPH_DEBUG_RENDERER
  133. // Remember size before adding new points, to check at the end if we added some
  134. ContactPoints::size_type old_size = outContactPoints1.size();
  135. // Check if both shapes have polygon faces
  136. if (inShape1Face.size() >= 2 // The dynamic shape needs to have at least 2 points or else there can never be more than 1 contact point
  137. && inShape2Face.size() >= 3) // The dynamic/static shape needs to have at least 3 points (in the case that it has 2 points only if the edges match exactly you can have 2 contact points, but this situation is unstable anyhow)
  138. {
  139. // Clip the polygon of face 2 against that of 1
  140. ConvexShape::SupportingFace clipped_face;
  141. if (inShape1Face.size() >= 3)
  142. ClipPolyVsPoly(inShape2Face, inShape1Face, inPenetrationAxis, clipped_face);
  143. else if (inShape1Face.size() == 2)
  144. ClipPolyVsEdge(inShape2Face, inShape1Face[0], inShape1Face[1], inPenetrationAxis, clipped_face);
  145. // Project the points back onto the plane of shape 1 face and only keep those that are behind the plane
  146. Vec3 plane_origin = inShape1Face[0];
  147. Vec3 plane_normal;
  148. Vec3 first_edge = inShape1Face[1] - plane_origin;
  149. if (inShape1Face.size() >= 3)
  150. {
  151. // Three vertices, can just calculate the normal
  152. plane_normal = first_edge.Cross(inShape1Face[2] - plane_origin);
  153. }
  154. else
  155. {
  156. // Two vertices, first find a perpendicular to the edge and penetration axis and then use the perpendicular together with the edge to form a normal
  157. plane_normal = first_edge.Cross(inPenetrationAxis).Cross(first_edge);
  158. }
  159. // Check if the plane normal has any length, if not the clipped shape is so small that we'll just use the contact points
  160. float plane_normal_len_sq = plane_normal.LengthSq();
  161. if (plane_normal_len_sq > 0.0f)
  162. {
  163. // Discard points of faces that are too far away to collide
  164. for (Vec3 p2 : clipped_face)
  165. {
  166. float distance = (p2 - plane_origin).Dot(plane_normal); // Note should divide by length of plane_normal (unnormalized here)
  167. if (distance <= 0.0f || Square(distance) < inMaxContactDistanceSq * plane_normal_len_sq) // Must be close enough to plane, note we correct for not dividing by plane normal length here
  168. {
  169. // Project point back on shape 1 using the normal, note we correct for not dividing by plane normal length here:
  170. // p1 = p2 - (distance / sqrt(plane_normal_len_sq)) * (plane_normal / sqrt(plane_normal_len_sq));
  171. Vec3 p1 = p2 - (distance / plane_normal_len_sq) * plane_normal;
  172. outContactPoints1.push_back(p1);
  173. outContactPoints2.push_back(p2);
  174. }
  175. }
  176. }
  177. #ifdef JPH_DEBUG_RENDERER
  178. if (ContactConstraintManager::sDrawSupportingFaces)
  179. {
  180. RMat44 com = RMat44::sTranslation(inCenterOfMass);
  181. // Draw clipped poly
  182. DebugRenderer::sInstance->DrawWirePolygon(com, clipped_face, Color::sOrange);
  183. // Draw supporting faces
  184. DebugRenderer::sInstance->DrawWirePolygon(com, inShape1Face, Color::sRed, 0.05f);
  185. DebugRenderer::sInstance->DrawWirePolygon(com, inShape2Face, Color::sGreen, 0.05f);
  186. // Draw normal
  187. if (plane_normal_len_sq > 0.0f)
  188. {
  189. RVec3 plane_origin_ws = inCenterOfMass + plane_origin;
  190. DebugRenderer::sInstance->DrawArrow(plane_origin_ws, plane_origin_ws + plane_normal / sqrt(plane_normal_len_sq), Color::sYellow, 0.05f);
  191. }
  192. // Draw contact points that remain after distance check
  193. for (ContactPoints::size_type p = old_size; p < outContactPoints1.size(); ++p)
  194. DebugRenderer::sInstance->DrawMarker(inCenterOfMass + outContactPoints1[p], Color::sYellow, 0.1f);
  195. }
  196. #endif // JPH_DEBUG_RENDERER
  197. }
  198. // If the clipping result is empty, use the contact point itself
  199. if (outContactPoints1.size() == old_size)
  200. {
  201. outContactPoints1.push_back(inContactPoint1);
  202. outContactPoints2.push_back(inContactPoint2);
  203. }
  204. }
  205. JPH_NAMESPACE_END