EPATest.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Tests/ConvexCollision/EPATest.h>
  6. #include <Jolt/Geometry/Sphere.h>
  7. #include <Jolt/Geometry/AABox.h>
  8. #include <Jolt/Geometry/ConvexSupport.h>
  9. #include <Jolt/Geometry/EPAPenetrationDepth.h>
  10. #include <Utils/DebugRendererSP.h>
  11. JPH_IMPLEMENT_RTTI_VIRTUAL(EPATest)
  12. {
  13. JPH_ADD_BASE_CLASS(EPATest, Test)
  14. }
  15. void EPATest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  16. {
  17. AABox box(Vec3(1, 1, -2), Vec3(2, 2, 2));
  18. Sphere sphere(Vec3(4, 4, 0), sqrt(8.0f) + 0.01f);
  19. Mat44 matrix = Mat44::sRotationTranslation(Quat::sRotation(Vec3(1, 1, 1).Normalized(), 0.25f * JPH_PI), Vec3(1, 2, 3));
  20. bool intersecting = CollideBoxSphere(matrix, box, sphere);
  21. JPH_ASSERT(intersecting);
  22. (void)intersecting; // For when asserts are off
  23. }
  24. bool EPATest::CollideBoxSphere(Mat44Arg inMatrix, const AABox &inBox, const Sphere &inSphere) const
  25. {
  26. // Draw the box and shere
  27. DrawBoxSP(mDebugRenderer, inMatrix, inBox, Color::sGrey);
  28. DrawSphereSP(mDebugRenderer, inMatrix * inSphere.GetCenter(), inSphere.GetRadius(), Color::sGrey);
  29. // Transform the box and sphere according to inMatrix
  30. TransformedConvexObject<AABox> transformed_box(inMatrix, inBox);
  31. TransformedConvexObject<Sphere> transformed_sphere(inMatrix, inSphere);
  32. // Run the EPA algorithm
  33. EPAPenetrationDepth epa;
  34. Vec3 v1 = Vec3::sAxisX(), pa1, pb1;
  35. bool intersect1 = epa.GetPenetrationDepth(transformed_box, transformed_box, 0.0f, transformed_sphere, transformed_sphere, 0.0f, 1.0e-2f, FLT_EPSILON, v1, pa1, pb1);
  36. // Draw iterative solution
  37. if (intersect1)
  38. {
  39. DrawMarkerSP(mDebugRenderer, pa1, Color::sRed, 1.0f);
  40. DrawMarkerSP(mDebugRenderer, pb1, Color::sGreen, 1.0f);
  41. DrawArrowSP(mDebugRenderer, pb1 + Vec3(0, 1, 0), pb1 + Vec3(0, 1, 0) + v1, Color::sYellow, 0.1f);
  42. }
  43. // Calculate analytical solution
  44. Vec3 pa2 = inBox.GetClosestPoint(inSphere.GetCenter());
  45. Vec3 v2 = inSphere.GetCenter() - pa2;
  46. bool intersect2 = v2.LengthSq() <= Square(inSphere.GetRadius());
  47. JPH_ASSERT(intersect1 == intersect2);
  48. if (intersect1 && intersect2)
  49. {
  50. Vec3 pb2 = inSphere.GetCenter() - inSphere.GetRadius() * v2.NormalizedOr(Vec3::sZero());
  51. // Transform analytical solution
  52. v2 = inMatrix.Multiply3x3(v2);
  53. pa2 = inMatrix * pa2;
  54. pb2 = inMatrix * pb2;
  55. // Draw analytical solution
  56. DrawMarkerSP(mDebugRenderer, pa2, Color::sOrange, 1.0f);
  57. DrawMarkerSP(mDebugRenderer, pb2, Color::sYellow, 1.0f);
  58. // Check angle between v1 and v2
  59. float dot = v1.Dot(v2);
  60. float len = v1.Length() * v2.Length();
  61. float angle = RadiansToDegrees(ACos(dot / len));
  62. JPH_ASSERT(angle < 0.1f);
  63. Trace("Angle = %.9g", (double)angle);
  64. // Check delta between contact on A
  65. Vec3 dpa = pa2 - pa1;
  66. JPH_ASSERT(dpa.IsNearZero(Square(8.0e-4f)));
  67. Trace("Delta A = %.9g", (double)dpa.Length());
  68. // Check delta between contact on B
  69. Vec3 dpb = pb2 - pb1;
  70. JPH_ASSERT(dpb.IsNearZero(Square(8.0e-4f)));
  71. Trace("Delta B = %.9g", (double)dpb.Length());
  72. }
  73. return intersect1;
  74. }