2
0

EPATests.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include <Jolt/Geometry/ConvexSupport.h>
  5. #include <Jolt/Geometry/EPAPenetrationDepth.h>
  6. #include <Jolt/Geometry/AABox.h>
  7. #include <Jolt/Geometry/Sphere.h>
  8. #include <random>
  9. // Enable to trace accuracy of EPA algorithm
  10. #define EPA_TESTS_TRACE(...)
  11. //#define EPA_TESTS_TRACE(...) printf(__VA_ARGS__)
  12. TEST_SUITE("EPATests")
  13. {
  14. /// Helper function to return the angle between two vectors in degrees
  15. static float AngleBetweenVectors(Vec3Arg inV1, Vec3Arg inV2)
  16. {
  17. float dot = inV1.Dot(inV2);
  18. float len = inV1.Length() * inV2.Length();
  19. return RadiansToDegrees(ACos(dot / len));
  20. }
  21. /// Test box versus sphere and compare analytical solution with that of the EPA algorithm
  22. /// @return If a collision was detected
  23. static bool CollideBoxSphere(Mat44Arg inMatrix, const AABox &inBox, const Sphere &inSphere)
  24. {
  25. TransformedConvexObject<AABox> transformed_box(inMatrix, inBox);
  26. TransformedConvexObject<Sphere> transformed_sphere(inMatrix, inSphere);
  27. // Use EPA algorithm. Don't use convex radius to avoid EPA being skipped because the inner hulls are not touching.
  28. EPAPenetrationDepth epa;
  29. Vec3 v1 = Vec3::sAxisX(), pa1, pb1;
  30. bool intersect1 = epa.GetPenetrationDepth(transformed_box, transformed_box, 0.0f, transformed_sphere, transformed_sphere, 0.0f, 1.0e-2f, FLT_EPSILON, v1, pa1, pb1);
  31. // Analytical solution
  32. Vec3 pa2 = inBox.GetClosestPoint(inSphere.GetCenter());
  33. Vec3 v2 = inSphere.GetCenter() - pa2;
  34. bool intersect2 = v2.LengthSq() <= Square(inSphere.GetRadius());
  35. CHECK(intersect1 == intersect2);
  36. if (intersect1 && intersect2)
  37. {
  38. // Analytical solution of contact on B
  39. Vec3 pb2 = inSphere.GetCenter() - inSphere.GetRadius() * v2.NormalizedOr(Vec3::sZero());
  40. // Transform analytical solution
  41. v2 = inMatrix.Multiply3x3(v2);
  42. pa2 = inMatrix * pa2;
  43. pb2 = inMatrix * pb2;
  44. // Check angle between v1 and v2
  45. float angle = AngleBetweenVectors(v1, v2);
  46. CHECK(angle < 0.1f);
  47. EPA_TESTS_TRACE("Angle = %.9g\n", (double)angle);
  48. // Check delta between contact on A
  49. Vec3 dpa = pa2 - pa1;
  50. CHECK(dpa.Length() < 8.0e-4f);
  51. EPA_TESTS_TRACE("Delta A = %.9g\n", (double)dpa.Length());
  52. // Check delta between contact on B
  53. Vec3 dpb = pb2 - pb1;
  54. CHECK(dpb.Length() < 8.0e-4f);
  55. EPA_TESTS_TRACE("Delta B = %.9g\n", (double)dpb.Length());
  56. }
  57. return intersect1;
  58. }
  59. /// Test multiple boxes against spheres and transform both with inMatrix
  60. static void CollideBoxesWithSpheres(Mat44Arg inMatrix)
  61. {
  62. {
  63. // Sphere just missing face of box
  64. AABox box(Vec3(-2, -3, -4), Vec3(2, 3, 4));
  65. Sphere sphere(Vec3(4, 0, 0), 1.99f);
  66. CHECK(!CollideBoxSphere(inMatrix, box, sphere));
  67. }
  68. {
  69. // Sphere just touching face of box
  70. AABox box(Vec3(-2, -3, -4), Vec3(2, 3, 4));
  71. Sphere sphere(Vec3(4, 0, 0), 2.01f);
  72. CHECK(CollideBoxSphere(inMatrix, box, sphere));
  73. }
  74. {
  75. // Sphere deeply penetrating box on face
  76. AABox box(Vec3(-2, -3, -4), Vec3(2, 3, 4));
  77. Sphere sphere(Vec3(3, 0, 0), 2);
  78. CHECK(CollideBoxSphere(inMatrix, box, sphere));
  79. }
  80. {
  81. // Sphere just missing box on edge
  82. AABox box(Vec3(1, 1, -2), Vec3(2, 2, 2));
  83. Sphere sphere(Vec3(4, 4, 0), sqrt(8.0f) - 0.01f);
  84. CHECK(!CollideBoxSphere(inMatrix, box, sphere));
  85. }
  86. {
  87. // Sphere just penetrating box on edge
  88. AABox box(Vec3(1, 1, -2), Vec3(2, 2, 2));
  89. Sphere sphere(Vec3(4, 4, 0), sqrt(8.0f) + 0.01f);
  90. CHECK(CollideBoxSphere(inMatrix, box, sphere));
  91. }
  92. {
  93. // Sphere just missing box on vertex
  94. AABox box(Vec3(1, 1, 1), Vec3(2, 2, 2));
  95. Sphere sphere(Vec3(4, 4, 4), sqrt(12.0f) - 0.01f);
  96. CHECK(!CollideBoxSphere(inMatrix, box, sphere));
  97. }
  98. {
  99. // Sphere just penetrating box on vertex
  100. AABox box(Vec3(1, 1, 1), Vec3(2, 2, 2));
  101. Sphere sphere(Vec3(4, 4, 4), sqrt(12.0f) + 0.01f);
  102. CHECK(CollideBoxSphere(inMatrix, box, sphere));
  103. }
  104. }
  105. TEST_CASE("TestEPASphereBox")
  106. {
  107. // Test identity transform
  108. CollideBoxesWithSpheres(Mat44::sIdentity());
  109. // Test some random rotations/translations
  110. UnitTestRandom random;
  111. for (int i = 0; i < 10; ++i)
  112. CollideBoxesWithSpheres(Mat44::sRotationTranslation(Quat::sRandom(random), Vec3::sRandom(random)));
  113. }
  114. TEST_CASE("TestEPASphereSphereOverlapping")
  115. {
  116. // Worst case: Two spheres exactly overlapping
  117. // In this case the Minkowski sum is a sphere which means the EPA algorithm will be building a convex hull of a full sphere and run out of triangles resulting in a pretty bad approximation
  118. Sphere sphere(Vec3(1, 2, 3), 2.0f);
  119. EPAPenetrationDepth epa;
  120. Vec3 v = Vec3::sAxisX(), pa, pb;
  121. CHECK(epa.GetPenetrationDepth(sphere, sphere, 0.0f, sphere, sphere, 0.0f, 1.0e-4f, FLT_EPSILON, v, pa, pb));
  122. float delta_a = (pa - sphere.GetCenter()).Length() - sphere.GetRadius();
  123. CHECK(abs(delta_a) < 0.07f);
  124. float delta_b = (pb - sphere.GetCenter()).Length() - sphere.GetRadius();
  125. CHECK(abs(delta_b) < 0.07f);
  126. float delta_penetration = (pa - pb).Length() - 2.0f * sphere.GetRadius();
  127. CHECK(abs(delta_penetration) < 0.14f);
  128. float angle = AngleBetweenVectors(v, pa - pb);
  129. CHECK(angle < 1.0e-3f);
  130. }
  131. TEST_CASE("TestEPASphereSphereNearOverlapping")
  132. {
  133. // Near worst case: Two spheres almost exactly overlapping
  134. // Still limited by amount of triangles in the hull but more precise
  135. Sphere sphere1(Vec3(1, 2, 3), 2.0f);
  136. Sphere sphere2(Vec3(1.1f, 2, 3), 1.8f);
  137. EPAPenetrationDepth epa;
  138. Vec3 v = Vec3::sAxisX(), pa, pb;
  139. CHECK(epa.GetPenetrationDepth(sphere1, sphere1, 0.0f, sphere2, sphere2, 0.0f, 1.0e-4f, FLT_EPSILON, v, pa, pb));
  140. float delta_a = (pa - sphere1.GetCenter()).Length() - sphere1.GetRadius();
  141. CHECK(abs(delta_a) < 0.05f);
  142. float delta_b = (pb - sphere2.GetCenter()).Length() - sphere2.GetRadius();
  143. CHECK(abs(delta_b) < 0.05f);
  144. float delta_penetration = (pa - pb).Length() - (sphere1.GetRadius() + sphere2.GetRadius() - (sphere1.GetCenter() - sphere2.GetCenter()).Length());
  145. CHECK(abs(delta_penetration) < 0.06f);
  146. float angle = AngleBetweenVectors(v, pa - pb);
  147. CHECK(angle < 1.0e-3f);
  148. }
  149. TEST_CASE("TestEPACastSphereSphereMiss")
  150. {
  151. Sphere sphere(Vec3(0, 0, 0), 1.0f);
  152. EPAPenetrationDepth epa;
  153. float lambda = 1.0f + FLT_EPSILON;
  154. const Vec3 invalid(-999, -999, -999);
  155. Vec3 pa = invalid, pb = invalid, normal = invalid;
  156. CHECK(!epa.CastShape(Mat44::sTranslation(Vec3(-10, 2.1f, 0)), Vec3(20, 0, 0), 1.0e-4f, 1.0e-4f, sphere, sphere, 0.0f, 0.0f, true, lambda, pa, pb, normal));
  157. CHECK(lambda == 1.0f + FLT_EPSILON); // Check input values didn't change
  158. CHECK(pa == invalid);
  159. CHECK(pb == invalid);
  160. CHECK(normal == invalid);
  161. }
  162. TEST_CASE("TestEPACastSphereSphereInitialOverlap")
  163. {
  164. Sphere sphere(Vec3(0, 0, 0), 1.0f);
  165. EPAPenetrationDepth epa;
  166. float lambda = 1.0f + FLT_EPSILON;
  167. Vec3 pa, pb, normal;
  168. CHECK(epa.CastShape(Mat44::sTranslation(Vec3(-1, 0, 0)), Vec3(10, 0, 0), 1.0e-4f, 1.0e-4f, sphere, sphere, 0.0f, 0.0f, true, lambda, pa, pb, normal));
  169. CHECK(lambda == 0.0f);
  170. CHECK_APPROX_EQUAL(pa, Vec3::sZero(), 5.0e-3f);
  171. CHECK_APPROX_EQUAL(pb, Vec3(-1, 0, 0), 5.0e-3f);
  172. CHECK_APPROX_EQUAL(normal.NormalizedOr(Vec3::sZero()), Vec3(1, 0, 0), 1.0e-2f);
  173. }
  174. TEST_CASE("TestEPACastSphereSphereHit")
  175. {
  176. Sphere sphere(Vec3(0, 0, 0), 1.0f);
  177. EPAPenetrationDepth epa;
  178. float lambda = 1.0f + FLT_EPSILON;
  179. Vec3 pa, pb, normal;
  180. CHECK(epa.CastShape(Mat44::sTranslation(Vec3(-10, 0, 0)), Vec3(20, 0, 0), 1.0e-4f, 1.0e-4f, sphere, sphere, 0.0f, 0.0f, true, lambda, pa, pb, normal));
  181. CHECK_APPROX_EQUAL(lambda, 8.0f / 20.0f);
  182. CHECK_APPROX_EQUAL(pa, Vec3(-1, 0, 0));
  183. CHECK_APPROX_EQUAL(pb, Vec3(-1, 0, 0));
  184. CHECK_APPROX_EQUAL(normal.NormalizedOr(Vec3::sZero()), Vec3(1, 0, 0));
  185. }
  186. }