EPAPenetrationDepth.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/StaticArray.h>
  6. #include <Jolt/Core/Profiler.h>
  7. #include <Jolt/Geometry/GJKClosestPoint.h>
  8. #include <Jolt/Geometry/EPAConvexHullBuilder.h>
  9. //#define JPH_EPA_PENETRATION_DEPTH_DEBUG
  10. JPH_NAMESPACE_BEGIN
  11. /// Implementation of Expanding Polytope Algorithm as described in:
  12. ///
  13. /// Proximity Queries and Penetration Depth Computation on 3D Game Objects - Gino van den Bergen
  14. ///
  15. /// The implementation of this algorithm does not completely follow the article, instead of splitting
  16. /// triangles at each edge as in fig. 7 in the article, we build a convex hull (removing any triangles that
  17. /// are facing the new point, thereby avoiding the problem of getting really oblong triangles as mentioned in
  18. /// the article).
  19. ///
  20. /// The algorithm roughly works like:
  21. ///
  22. /// - Start with a simplex of the Minkowski sum (difference) of two objects that was calculated by GJK
  23. /// - This simplex should contain the origin (or else GJK would have reported: no collision)
  24. /// - In cases where the simplex consists of 1 - 3 points, find some extra support points (of the Minkowski sum) to get to at least 4 points
  25. /// - Convert this into a convex hull with non-zero volume (which includes the origin)
  26. /// - A: Calculate the closest point to the origin for all triangles of the hull and take the closest one
  27. /// - Calculate a new support point (of the Minkowski sum) in this direction and add this point to the convex hull
  28. /// - This will remove all faces that are facing the new point and will create new triangles to fill up the hole
  29. /// - Loop to A until no closer point found
  30. /// - The closest point indicates the position / direction of least penetration
  31. class EPAPenetrationDepth
  32. {
  33. private:
  34. // Typedefs
  35. static constexpr int cMaxPoints = EPAConvexHullBuilder::cMaxPoints;
  36. static constexpr int cMaxPointsToIncludeOriginInHull = 32;
  37. static_assert(cMaxPointsToIncludeOriginInHull < cMaxPoints);
  38. using Triangle = EPAConvexHullBuilder::Triangle;
  39. using Points = EPAConvexHullBuilder::Points;
  40. /// The GJK algorithm, used to start the EPA algorithm
  41. GJKClosestPoint mGJK;
  42. /// A list of support points for the EPA algorithm
  43. class SupportPoints
  44. {
  45. public:
  46. /// List of support points
  47. Points mY;
  48. Vec3 mP[cMaxPoints];
  49. Vec3 mQ[cMaxPoints];
  50. /// Calculate and add new support point to the list of points
  51. template <typename A, typename B>
  52. Vec3 Add(const A &inA, const B &inB, Vec3Arg inDirection, int &outIndex)
  53. {
  54. // Get support point of the minkowski sum A - B
  55. Vec3 p = inA.GetSupport(inDirection);
  56. Vec3 q = inB.GetSupport(-inDirection);
  57. Vec3 w = p - q;
  58. // Store new point
  59. outIndex = mY.size();
  60. mY.push_back(w);
  61. mP[outIndex] = p;
  62. mQ[outIndex] = q;
  63. return w;
  64. }
  65. };
  66. public:
  67. /// Return code for GetPenetrationDepthStepGJK
  68. enum class EStatus
  69. {
  70. NotColliding, ///< Returned if the objects don't collide, in this case outPointA/outPointB are invalid
  71. Colliding, ///< Returned if the objects penetrate
  72. Indeterminate ///< Returned if the objects penetrate further than the convex radius. In this case you need to call GetPenetrationDepthStepEPA to get the actual penetration depth.
  73. };
  74. /// Calculates penetration depth between two objects, first step of two (the GJK step)
  75. ///
  76. /// @param inAExcludingConvexRadius Object A without convex radius.
  77. /// @param inBExcludingConvexRadius Object B without convex radius.
  78. /// @param inConvexRadiusA Convex radius for A.
  79. /// @param inConvexRadiusB Convex radius for B.
  80. /// @param ioV Pass in previously returned value or (1, 0, 0). On return this value is changed to direction to move B out of collision along the shortest path (magnitude is meaningless).
  81. /// @param inTolerance Minimal distance before A and B are considered colliding.
  82. /// @param outPointA Position on A that has the least amount of penetration.
  83. /// @param outPointB Position on B that has the least amount of penetration.
  84. /// Use |outPointB - outPointA| to get the distance of penetration.
  85. template <typename AE, typename BE>
  86. EStatus GetPenetrationDepthStepGJK(const AE &inAExcludingConvexRadius, float inConvexRadiusA, const BE &inBExcludingConvexRadius, float inConvexRadiusB, float inTolerance, Vec3 &ioV, Vec3 &outPointA, Vec3 &outPointB)
  87. {
  88. JPH_PROFILE_FUNCTION();
  89. // Don't supply a zero ioV, we only want to get points on the hull of the Minkowsky sum and not internal points
  90. JPH_ASSERT(!ioV.IsNearZero());
  91. // Get closest points
  92. float combined_radius = inConvexRadiusA + inConvexRadiusB;
  93. float combined_radius_sq = combined_radius * combined_radius;
  94. float closest_points_dist_sq = mGJK.GetClosestPoints(inAExcludingConvexRadius, inBExcludingConvexRadius, inTolerance, combined_radius_sq, ioV, outPointA, outPointB);
  95. if (closest_points_dist_sq > combined_radius_sq)
  96. {
  97. // No collision
  98. return EStatus::NotColliding;
  99. }
  100. if (closest_points_dist_sq > 0.0f)
  101. {
  102. // Collision within convex radius, adjust points for convex radius
  103. float v_len = sqrt(closest_points_dist_sq); // GetClosestPoints function returns |ioV|^2 when return value < FLT_MAX
  104. outPointA += ioV * (inConvexRadiusA / v_len);
  105. outPointB -= ioV * (inConvexRadiusB / v_len);
  106. return EStatus::Colliding;
  107. }
  108. return EStatus::Indeterminate;
  109. }
  110. /// Calculates penetration depth between two objects, second step (the EPA step)
  111. ///
  112. /// @param inAIncludingConvexRadius Object A with convex radius
  113. /// @param inBIncludingConvexRadius Object B with convex radius
  114. /// @param inTolerance A factor that determines the accuracy of the result. If the change of the squared distance is less than inTolerance * current_penetration_depth^2 the algorithm will terminate. Should be bigger or equal to FLT_EPSILON.
  115. /// @param outV Direction to move B out of collision along the shortest path (magnitude is meaningless)
  116. /// @param outPointA Position on A that has the least amount of penetration
  117. /// @param outPointB Position on B that has the least amount of penetration
  118. /// Use |outPointB - outPointA| to get the distance of penetration
  119. ///
  120. /// @return False if the objects don't collide, in this case outPointA/outPointB are invalid.
  121. /// True if the objects penetrate
  122. template <typename AI, typename BI>
  123. bool GetPenetrationDepthStepEPA(const AI &inAIncludingConvexRadius, const BI &inBIncludingConvexRadius, float inTolerance, Vec3 &outV, Vec3 &outPointA, Vec3 &outPointB)
  124. {
  125. JPH_PROFILE_FUNCTION();
  126. // Check that the tolerance makes sense (smaller value than this will just result in needless iterations)
  127. JPH_ASSERT(inTolerance >= FLT_EPSILON);
  128. // Fetch the simplex from GJK algorithm
  129. SupportPoints support_points;
  130. mGJK.GetClosestPointsSimplex(support_points.mY.data(), support_points.mP, support_points.mQ, support_points.mY.GetSizeRef());
  131. // Fill up the amount of support points to 4
  132. switch (support_points.mY.size())
  133. {
  134. case 1:
  135. {
  136. // 1 vertex, which must be at the origin, which is useless for our purpose
  137. JPH_ASSERT(support_points.mY[0].IsNearZero(1.0e-8f));
  138. support_points.mY.pop_back();
  139. // Add support points in 4 directions to form a tetrahedron around the origin
  140. int p1, p2, p3, p4;
  141. (void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, Vec3(0, 1, 0), p1);
  142. (void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, Vec3(-1, -1, -1), p2);
  143. (void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, Vec3(1, -1, -1), p3);
  144. (void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, Vec3(0, -1, 1), p4);
  145. JPH_ASSERT(p1 == 0);
  146. JPH_ASSERT(p2 == 1);
  147. JPH_ASSERT(p3 == 2);
  148. JPH_ASSERT(p4 == 3);
  149. break;
  150. }
  151. case 2:
  152. {
  153. // Two vertices, create 3 extra by taking perpendicular axis and rotating it around in 120 degree increments
  154. Vec3 axis = (support_points.mY[1] - support_points.mY[0]).Normalized();
  155. Mat44 rotation = Mat44::sRotation(axis, DegreesToRadians(120.0f));
  156. Vec3 dir1 = axis.GetNormalizedPerpendicular();
  157. Vec3 dir2 = rotation * dir1;
  158. Vec3 dir3 = rotation * dir2;
  159. int p1, p2, p3;
  160. (void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, dir1, p1);
  161. (void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, dir2, p2);
  162. (void)support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, dir3, p3);
  163. JPH_ASSERT(p1 == 2);
  164. JPH_ASSERT(p2 == 3);
  165. JPH_ASSERT(p3 == 4);
  166. break;
  167. }
  168. case 3:
  169. case 4:
  170. // We already have enough points
  171. break;
  172. }
  173. // Create hull out of the initial points
  174. JPH_ASSERT(support_points.mY.size() >= 3);
  175. EPAConvexHullBuilder hull(support_points.mY);
  176. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  177. hull.DrawLabel("Build initial hull");
  178. #endif
  179. #ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG
  180. Trace("Init: num_points = %u", (uint)support_points.mY.size());
  181. #endif
  182. hull.Initialize(0, 1, 2);
  183. for (typename Points::size_type i = 3; i < support_points.mY.size(); ++i)
  184. {
  185. float dist_sq;
  186. Triangle *t = hull.FindFacingTriangle(support_points.mY[i], dist_sq);
  187. if (t != nullptr)
  188. {
  189. EPAConvexHullBuilder::NewTriangles new_triangles;
  190. if (!hull.AddPoint(t, i, FLT_MAX, new_triangles))
  191. {
  192. // We can't recover from a failure to add a point to the hull because the old triangles have been unlinked already.
  193. // Assume no collision. This can happen if the shapes touch in 1 point (or plane) in which case the hull is degenerate.
  194. return false;
  195. }
  196. }
  197. }
  198. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  199. hull.DrawLabel("Complete hull");
  200. // Generate the hull of the Minkowski difference for visualization
  201. MinkowskiDifference diff(inAIncludingConvexRadius, inBIncludingConvexRadius);
  202. DebugRenderer::GeometryRef geometry = DebugRenderer::sInstance->CreateTriangleGeometryForConvex([&diff](Vec3Arg inDirection) { return diff.GetSupport(inDirection); });
  203. hull.DrawGeometry(geometry, Color::sYellow);
  204. hull.DrawLabel("Ensure origin in hull");
  205. #endif
  206. // Loop until we are sure that the origin is inside the hull
  207. for (;;)
  208. {
  209. // Get the next closest triangle
  210. Triangle *t = hull.PeekClosestTriangleInQueue();
  211. // Don't process removed triangles, just free them (because they're in a heap we don't remove them earlier since we would have to rebuild the sorted heap)
  212. if (t->mRemoved)
  213. {
  214. hull.PopClosestTriangleFromQueue();
  215. // If we run out of triangles, we couldn't include the origin in the hull so there must be very little penetration and we report no collision.
  216. if (!hull.HasNextTriangle())
  217. return false;
  218. hull.FreeTriangle(t);
  219. continue;
  220. }
  221. // If the closest to the triangle is zero or positive, the origin is in the hull and we can proceed to the main algorithm
  222. if (t->mClosestLenSq >= 0.0f)
  223. break;
  224. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  225. hull.DrawLabel("Next iteration");
  226. #endif
  227. #ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG
  228. Trace("EncapsulateOrigin: verts = (%d, %d, %d), closest_dist_sq = %g, centroid = (%g, %g, %g), normal = (%g, %g, %g)",
  229. t->mEdge[0].mStartIdx, t->mEdge[1].mStartIdx, t->mEdge[2].mStartIdx,
  230. t->mClosestLenSq,
  231. t->mCentroid.GetX(), t->mCentroid.GetY(), t->mCentroid.GetZ(),
  232. t->mNormal.GetX(), t->mNormal.GetY(), t->mNormal.GetZ());
  233. #endif
  234. // Remove the triangle from the queue before we start adding new ones (which may result in a new closest triangle at the front of the queue)
  235. hull.PopClosestTriangleFromQueue();
  236. // Add a support point to get the origin inside the hull
  237. int new_index;
  238. Vec3 w = support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, t->mNormal, new_index);
  239. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  240. // Draw the point that we're adding
  241. hull.DrawMarker(w, Color::sRed, 1.0f);
  242. hull.DrawWireTriangle(*t, Color::sRed);
  243. hull.DrawState();
  244. #endif
  245. // Add the point to the hull, if we fail we terminate and report no collision
  246. EPAConvexHullBuilder::NewTriangles new_triangles;
  247. if (!t->IsFacing(w) || !hull.AddPoint(t, new_index, FLT_MAX, new_triangles))
  248. return false;
  249. // If the triangle was removed we can free it now
  250. if (t->mRemoved)
  251. hull.FreeTriangle(t);
  252. // If we run out of triangles or points, we couldn't include the origin in the hull so there must be very little penetration and we report no collision.
  253. if (!hull.HasNextTriangle() || support_points.mY.size() >= cMaxPointsToIncludeOriginInHull)
  254. return false;
  255. }
  256. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  257. hull.DrawLabel("Main algorithm");
  258. #endif
  259. // Current closest distance to origin
  260. float closest_dist_sq = FLT_MAX;
  261. // Remember last good triangle
  262. Triangle *last = nullptr;
  263. // If we want to flip the penetration depth
  264. bool flip_v_sign = false;
  265. // Loop until closest point found
  266. do
  267. {
  268. // Get closest triangle to the origin
  269. Triangle *t = hull.PopClosestTriangleFromQueue();
  270. // Don't process removed triangles, just free them (because they're in a heap we don't remove them earlier since we would have to rebuild the sorted heap)
  271. if (t->mRemoved)
  272. {
  273. hull.FreeTriangle(t);
  274. continue;
  275. }
  276. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  277. hull.DrawLabel("Next iteration");
  278. #endif
  279. #ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG
  280. Trace("FindClosest: verts = (%d, %d, %d), closest_len_sq = %g, centroid = (%g, %g, %g), normal = (%g, %g, %g)",
  281. t->mEdge[0].mStartIdx, t->mEdge[1].mStartIdx, t->mEdge[2].mStartIdx,
  282. t->mClosestLenSq,
  283. t->mCentroid.GetX(), t->mCentroid.GetY(), t->mCentroid.GetZ(),
  284. t->mNormal.GetX(), t->mNormal.GetY(), t->mNormal.GetZ());
  285. #endif
  286. // Check if next triangle is further away than closest point, we've found the closest point
  287. if (t->mClosestLenSq >= closest_dist_sq)
  288. break;
  289. // Replace last good with this triangle
  290. if (last != nullptr)
  291. hull.FreeTriangle(last);
  292. last = t;
  293. // Add support point in direction of normal of the plane
  294. // Note that the article uses the closest point between the origin and plane, but this always has the exact same direction as the normal (if the origin is behind the plane)
  295. // and this way we do less calculations and lose less precision
  296. int new_index;
  297. Vec3 w = support_points.Add(inAIncludingConvexRadius, inBIncludingConvexRadius, t->mNormal, new_index);
  298. // Project w onto the triangle normal
  299. float dot = t->mNormal.Dot(w);
  300. // Check if we just found a separating axis. This can happen if the shape shrunk by convex radius and then expanded by
  301. // convex radius is bigger then the original shape due to inaccuracies in the shrinking process.
  302. if (dot < 0.0f)
  303. return false;
  304. // Get the distance squared (along normal) to the support point
  305. float dist_sq = Square(dot) / t->mNormal.LengthSq();
  306. #ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG
  307. Trace("FindClosest: w = (%g, %g, %g), dot = %g, dist_sq = %g",
  308. w.GetX(), w.GetY(), w.GetZ(),
  309. dot, dist_sq);
  310. #endif
  311. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  312. // Draw the point that we're adding
  313. hull.DrawMarker(w, Color::sPurple, 1.0f);
  314. hull.DrawWireTriangle(*t, Color::sPurple);
  315. hull.DrawState();
  316. #endif
  317. // If the error became small enough, we've converged
  318. if (dist_sq - t->mClosestLenSq < t->mClosestLenSq * inTolerance)
  319. {
  320. #ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG
  321. Trace("Converged");
  322. #endif // JPH_EPA_PENETRATION_DEPTH_DEBUG
  323. break;
  324. }
  325. // Keep track of the minimum distance
  326. closest_dist_sq = min(closest_dist_sq, dist_sq);
  327. // If the triangle thinks this point is not front facing, we've reached numerical precision and we're done
  328. if (!t->IsFacing(w))
  329. {
  330. #ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG
  331. Trace("Not facing triangle");
  332. #endif // JPH_EPA_PENETRATION_DEPTH_DEBUG
  333. break;
  334. }
  335. // Add point to hull
  336. EPAConvexHullBuilder::NewTriangles new_triangles;
  337. if (!hull.AddPoint(t, new_index, closest_dist_sq, new_triangles))
  338. {
  339. #ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG
  340. Trace("Could not add point");
  341. #endif // JPH_EPA_PENETRATION_DEPTH_DEBUG
  342. break;
  343. }
  344. // If the hull is starting to form defects then we're reaching numerical precision and we have to stop
  345. bool has_defect = false;
  346. for (const Triangle *nt : new_triangles)
  347. if (nt->IsFacingOrigin())
  348. {
  349. has_defect = true;
  350. break;
  351. }
  352. if (has_defect)
  353. {
  354. #ifdef JPH_EPA_PENETRATION_DEPTH_DEBUG
  355. Trace("Has defect");
  356. #endif // JPH_EPA_PENETRATION_DEPTH_DEBUG
  357. // When the hull has defects it is possible that the origin has been classified on the wrong side of the triangle
  358. // so we do an additional check to see if the penetration in the -triangle normal direction is smaller than
  359. // the penetration in the triangle normal direction. If so we must flip the sign of the penetration depth.
  360. Vec3 w2 = inAIncludingConvexRadius.GetSupport(-t->mNormal) - inBIncludingConvexRadius.GetSupport(t->mNormal);
  361. float dot2 = -t->mNormal.Dot(w2);
  362. if (dot2 < dot)
  363. flip_v_sign = true;
  364. break;
  365. }
  366. }
  367. while (hull.HasNextTriangle() && support_points.mY.size() < cMaxPoints);
  368. // Determine closest points, if last == null it means the hull was a plane so there's no penetration
  369. if (last == nullptr)
  370. return false;
  371. #ifdef JPH_EPA_CONVEX_BUILDER_DRAW
  372. hull.DrawLabel("Closest found");
  373. hull.DrawWireTriangle(*last, Color::sWhite);
  374. hull.DrawArrow(last->mCentroid, last->mCentroid + last->mNormal.NormalizedOr(Vec3::sZero()), Color::sWhite, 0.1f);
  375. hull.DrawState();
  376. #endif
  377. // Calculate penetration by getting the vector from the origin to the closest point on the triangle:
  378. // distance = (centroid - origin) . normal / |normal|, closest = origin + distance * normal / |normal|
  379. outV = (last->mCentroid.Dot(last->mNormal) / last->mNormal.LengthSq()) * last->mNormal;
  380. // If penetration is near zero, treat this as a non collision since we cannot find a good normal
  381. if (outV.IsNearZero())
  382. return false;
  383. // Check if we have to flip the sign of the penetration depth
  384. if (flip_v_sign)
  385. outV = -outV;
  386. // Use the barycentric coordinates for the closest point to the origin to find the contact points on A and B
  387. Vec3 p0 = support_points.mP[last->mEdge[0].mStartIdx];
  388. Vec3 p1 = support_points.mP[last->mEdge[1].mStartIdx];
  389. Vec3 p2 = support_points.mP[last->mEdge[2].mStartIdx];
  390. Vec3 q0 = support_points.mQ[last->mEdge[0].mStartIdx];
  391. Vec3 q1 = support_points.mQ[last->mEdge[1].mStartIdx];
  392. Vec3 q2 = support_points.mQ[last->mEdge[2].mStartIdx];
  393. if (last->mLambdaRelativeTo0)
  394. {
  395. // y0 was the reference vertex
  396. outPointA = p0 + last->mLambda[0] * (p1 - p0) + last->mLambda[1] * (p2 - p0);
  397. outPointB = q0 + last->mLambda[0] * (q1 - q0) + last->mLambda[1] * (q2 - q0);
  398. }
  399. else
  400. {
  401. // y1 was the reference vertex
  402. outPointA = p1 + last->mLambda[0] * (p0 - p1) + last->mLambda[1] * (p2 - p1);
  403. outPointB = q1 + last->mLambda[0] * (q0 - q1) + last->mLambda[1] * (q2 - q1);
  404. }
  405. return true;
  406. }
  407. /// This function combines the GJK and EPA steps and is provided as a convenience function.
  408. /// Note: less performant since you're providing all support functions in one go
  409. /// Note 2: You need to initialize ioV, see documentation at GetPenetrationDepthStepGJK!
  410. template <typename AE, typename AI, typename BE, typename BI>
  411. bool GetPenetrationDepth(const AE &inAExcludingConvexRadius, const AI &inAIncludingConvexRadius, float inConvexRadiusA, const BE &inBExcludingConvexRadius, const BI &inBIncludingConvexRadius, float inConvexRadiusB, float inCollisionToleranceSq, float inPenetrationTolerance, Vec3 &ioV, Vec3 &outPointA, Vec3 &outPointB)
  412. {
  413. // Check result of collision detection
  414. switch (GetPenetrationDepthStepGJK(inAExcludingConvexRadius, inConvexRadiusA, inBExcludingConvexRadius, inConvexRadiusB, inCollisionToleranceSq, ioV, outPointA, outPointB))
  415. {
  416. case EPAPenetrationDepth::EStatus::Colliding:
  417. return true;
  418. case EPAPenetrationDepth::EStatus::NotColliding:
  419. return false;
  420. case EPAPenetrationDepth::EStatus::Indeterminate:
  421. return GetPenetrationDepthStepEPA(inAIncludingConvexRadius, inBIncludingConvexRadius, inPenetrationTolerance, ioV, outPointA, outPointB);
  422. }
  423. JPH_ASSERT(false);
  424. return false;
  425. }
  426. /// Test if a cast shape inA moving from inStart to lambda * inStart.GetTranslation() + inDirection where lambda e [0, ioLambda> instersects inB
  427. ///
  428. /// @param inStart Start position and orientation of the convex object
  429. /// @param inDirection Direction of the sweep (ioLambda * inDirection determines length)
  430. /// @param inCollisionTolerance The minimal distance between A and B before they are considered colliding
  431. /// @param inPenetrationTolerance A factor that determines the accuracy of the result. If the change of the squared distance is less than inTolerance * current_penetration_depth^2 the algorithm will terminate. Should be bigger or equal to FLT_EPSILON.
  432. /// @param inA The convex object A, must support the GetSupport(Vec3) function.
  433. /// @param inB The convex object B, must support the GetSupport(Vec3) function.
  434. /// @param inConvexRadiusA The convex radius of A, this will be added on all sides to pad A.
  435. /// @param inConvexRadiusB The convex radius of B, this will be added on all sides to pad B.
  436. /// @param inReturnDeepestPoint If the shapes are initially interesecting this determines if the EPA algorithm will run to find the deepest point
  437. /// @param ioLambda The max fraction along the sweep, on output updated with the actual collision fraction.
  438. /// @param outPointA is the contact point on A
  439. /// @param outPointB is the contact point on B
  440. /// @param outContactNormal is either the contact normal when the objects are touching or the penetration axis when the objects are penetrating at the start of the sweep (pointing from A to B, length will not be 1)
  441. ///
  442. /// @return true if the a hit was found, in which case ioLambda, outPointA, outPointB and outSurfaceNormal are updated.
  443. template <typename A, typename B>
  444. bool CastShape(Mat44Arg inStart, Vec3Arg inDirection, float inCollisionTolerance, float inPenetrationTolerance, const A &inA, const B &inB, float inConvexRadiusA, float inConvexRadiusB, bool inReturnDeepestPoint, float &ioLambda, Vec3 &outPointA, Vec3 &outPointB, Vec3 &outContactNormal)
  445. {
  446. // First determine if there's a collision at all
  447. if (!mGJK.CastShape(inStart, inDirection, inCollisionTolerance, inA, inB, inConvexRadiusA, inConvexRadiusB, ioLambda, outPointA, outPointB, outContactNormal))
  448. return false;
  449. // When our contact normal is too small, we don't have an accurate result
  450. bool contact_normal_invalid = outContactNormal.IsNearZero(Square(inCollisionTolerance));
  451. if (inReturnDeepestPoint
  452. && ioLambda == 0.0f // Only when lambda = 0 we can have the bodies overlap
  453. && (inConvexRadiusA + inConvexRadiusB == 0.0f // When no convex radius was provided we can never trust contact points at lambda = 0
  454. || contact_normal_invalid))
  455. {
  456. // If we're initially intersecting, we need to run the EPA algorithm in order to find the deepest contact point
  457. AddConvexRadius<A> add_convex_a(inA, inConvexRadiusA);
  458. AddConvexRadius<B> add_convex_b(inB, inConvexRadiusB);
  459. TransformedConvexObject<AddConvexRadius<A>> transformed_a(inStart, add_convex_a);
  460. if (!GetPenetrationDepthStepEPA(transformed_a, add_convex_b, inPenetrationTolerance, outContactNormal, outPointA, outPointB))
  461. return false;
  462. }
  463. else if (contact_normal_invalid)
  464. {
  465. // If we weren't able to calculate a contact normal, use the cast direction instead
  466. outContactNormal = inDirection;
  467. }
  468. return true;
  469. }
  470. };
  471. JPH_NAMESPACE_END