InteractivePairsTest.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/InteractivePairsTest.h>
  6. #include <Input/Keyboard.h>
  7. #include <Jolt/Geometry/Sphere.h>
  8. #include <Jolt/Geometry/AABox.h>
  9. #include <Jolt/Geometry/ConvexSupport.h>
  10. #include <Jolt/Geometry/EPAPenetrationDepth.h>
  11. #include <Utils/DebugRendererSP.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(InteractivePairsTest)
  13. {
  14. JPH_ADD_BASE_CLASS(InteractivePairsTest, Test)
  15. }
  16. void InteractivePairsTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  17. {
  18. // Keyboard controls
  19. if (inParams.mKeyboard->IsKeyPressed(DIK_Z))
  20. {
  21. mKeyboardMode = true;
  22. mDistance -= inParams.mDeltaTime;
  23. }
  24. else if (inParams.mKeyboard->IsKeyPressed(DIK_C))
  25. {
  26. mKeyboardMode = true;
  27. mDistance += inParams.mDeltaTime;
  28. }
  29. else if (inParams.mKeyboard->IsKeyPressed(DIK_X))
  30. {
  31. mKeyboardMode = false;
  32. }
  33. // Auto update
  34. if (!mKeyboardMode)
  35. mDistance -= inParams.mDeltaTime;
  36. // Clamp distance
  37. if (mDistance < -4.0f)
  38. mDistance = 4.0f;
  39. if (mDistance > 4.0f)
  40. mDistance = -4.0f;
  41. float z = 0.0f;
  42. const float r1 = 0.25f * JPH_PI;
  43. const float r2 = ATan(1.0f / sqrt(2.0f)); // When rotating cube by 45 degrees the one axis becomes sqrt(2) long while the other stays at length 1
  44. for (int i = 0; i < 2; ++i)
  45. {
  46. const float cvx_radius = i == 0? 0.0f : 0.1f; // First round without convex radius, second with
  47. const float edge_len = 1.0f - cvx_radius;
  48. AABox b(Vec3(-edge_len, -edge_len, -edge_len), Vec3(edge_len, edge_len, edge_len));
  49. // Face vs face
  50. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, 0, 0), cvx_radius, b);
  51. z += 4;
  52. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(r1, 0, 0), cvx_radius, b);
  53. z += 4;
  54. // Face vs edge
  55. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
  56. z += 4;
  57. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, 0, r1), cvx_radius, b);
  58. z += 4;
  59. // Face vs vertex
  60. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
  61. z += 4;
  62. // Edge vs edge
  63. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, r1, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
  64. z += 4;
  65. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, r1), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
  66. z += 4;
  67. // Edge vs vertex
  68. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, r2, r1), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
  69. z += 4;
  70. // Sphere vs face
  71. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, 0, 0), cvx_radius, b);
  72. z += 4;
  73. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(r1, 0, 0), cvx_radius, b);
  74. z += 4;
  75. // Sphere vs edge
  76. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
  77. z += 4;
  78. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, 0, r1), cvx_radius, b);
  79. z += 4;
  80. // Sphere vs vertex
  81. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
  82. z += 4;
  83. // Sphere vs sphere
  84. TestSphereVsSphere(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), 1.0f, i == 1);
  85. z += 4;
  86. }
  87. }
  88. void InteractivePairsTest::TestBoxVsBox(Vec3Arg inTranslationA, Vec3Arg inRotationA, float inConvexRadiusA, const AABox &inA, Vec3Arg inTranslationB, Vec3Arg inRotationB, float inConvexRadiusB, const AABox &inB)
  89. {
  90. Mat44 mat_a = Mat44::sTranslation(inTranslationA) * Mat44::sRotationX(inRotationA.GetX()) * Mat44::sRotationY(inRotationA.GetY()) * Mat44::sRotationZ(inRotationA.GetZ());
  91. TransformedConvexObject<AABox> a(mat_a, inA);
  92. Mat44 mat_b = Mat44::sTranslation(inTranslationB) * Mat44::sRotationX(inRotationB.GetX()) * Mat44::sRotationY(inRotationB.GetY()) * Mat44::sRotationZ(inRotationB.GetZ());
  93. TransformedConvexObject<AABox> b(mat_b, inB);
  94. EPAPenetrationDepth pen_depth;
  95. Vec3 v = Vec3::sAxisX(), pa, pb;
  96. DrawBoxSP(mDebugRenderer, mat_a, inA, Color::sWhite);
  97. AABox widened_a = inA;
  98. widened_a.ExpandBy(Vec3::sReplicate(inConvexRadiusA));
  99. AABox widened_b = inB;
  100. widened_b.ExpandBy(Vec3::sReplicate(inConvexRadiusB));
  101. DrawBoxSP(mDebugRenderer, mat_a, inA, Color::sWhite);
  102. if (inConvexRadiusA > 0.0f)
  103. DrawWireBoxSP(mDebugRenderer, mat_a, widened_a, Color::sWhite);
  104. AddConvexRadius<TransformedConvexObject<AABox>> a_inc(a, inConvexRadiusA);
  105. AddConvexRadius<TransformedConvexObject<AABox>> b_inc(b, inConvexRadiusB);
  106. if (pen_depth.GetPenetrationDepth(a, a_inc, inConvexRadiusA, b, b_inc, inConvexRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb))
  107. {
  108. DrawBoxSP(mDebugRenderer, mat_b, inB, Color::sRed);
  109. if (inConvexRadiusB > 0.0f)
  110. DrawWireBoxSP(mDebugRenderer, mat_b, widened_b, Color::sRed);
  111. DrawMarkerSP(mDebugRenderer, pa, Color::sYellow, 2.0f);
  112. DrawMarkerSP(mDebugRenderer, pb, Color::sCyan, 2.0f);
  113. }
  114. else
  115. {
  116. DrawBoxSP(mDebugRenderer, mat_b, inB, Color::sGreen);
  117. if (inConvexRadiusB > 0.0f)
  118. DrawWireBoxSP(mDebugRenderer, mat_b, widened_b, Color::sGreen);
  119. }
  120. DrawArrowSP(mDebugRenderer, inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
  121. }
  122. void InteractivePairsTest::TestSphereVsBox(Vec3Arg inTranslationA, float inRadiusA, Vec3Arg inTranslationB, Vec3Arg inRotationB, float inConvexRadiusB, const AABox &inB)
  123. {
  124. Sphere s(inTranslationA, inRadiusA);
  125. Mat44 mat_b = Mat44::sTranslation(inTranslationB) * Mat44::sRotationX(inRotationB.GetX()) * Mat44::sRotationY(inRotationB.GetY()) * Mat44::sRotationZ(inRotationB.GetZ());
  126. TransformedConvexObject<AABox> b(mat_b, inB);
  127. AABox widened_b = inB;
  128. widened_b.ExpandBy(Vec3::sReplicate(inConvexRadiusB));
  129. EPAPenetrationDepth pen_depth;
  130. Vec3 v = Vec3::sAxisX(), pa, pb;
  131. DrawSphereSP(mDebugRenderer, inTranslationA, inRadiusA, Color::sWhite);
  132. AddConvexRadius<TransformedConvexObject<AABox>> b_inc(b, inConvexRadiusB);
  133. if (pen_depth.GetPenetrationDepth(s, s, 0.0f, b, b_inc, inConvexRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb))
  134. {
  135. DrawBoxSP(mDebugRenderer, mat_b, inB, Color::sRed);
  136. if (inConvexRadiusB > 0.0f)
  137. DrawWireBoxSP(mDebugRenderer, mat_b, widened_b, Color::sRed);
  138. DrawMarkerSP(mDebugRenderer, pa, Color::sYellow, 2.0f);
  139. DrawMarkerSP(mDebugRenderer, pb, Color::sCyan, 2.0f);
  140. }
  141. else
  142. {
  143. DrawBoxSP(mDebugRenderer, mat_b, inB, Color::sGreen);
  144. if (inConvexRadiusB > 0.0f)
  145. DrawWireBoxSP(mDebugRenderer, mat_b, widened_b, Color::sGreen);
  146. }
  147. DrawArrowSP(mDebugRenderer, inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
  148. }
  149. void InteractivePairsTest::TestSphereVsSphere(Vec3Arg inTranslationA, float inRadiusA, Vec3Arg inTranslationB, float inRadiusB, bool inTreatSphereAsPointWithConvexRadius)
  150. {
  151. Sphere s1(inTranslationA, inRadiusA);
  152. Sphere s2(inTranslationB, inRadiusB);
  153. if (inTreatSphereAsPointWithConvexRadius)
  154. DrawWireSphereSP(mDebugRenderer, s1.GetCenter(), s1.GetRadius(), Color::sWhite);
  155. else
  156. DrawSphereSP(mDebugRenderer, s1.GetCenter(), s1.GetRadius(), Color::sWhite);
  157. bool intersects;
  158. EPAPenetrationDepth pen_depth;
  159. Vec3 v = Vec3::sAxisX(), pa, pb;
  160. if (inTreatSphereAsPointWithConvexRadius)
  161. intersects = pen_depth.GetPenetrationDepth(PointConvexSupport { inTranslationA }, s1, inRadiusA, PointConvexSupport { inTranslationB }, s2, inRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb);
  162. else
  163. intersects = pen_depth.GetPenetrationDepth(s1, s1, 0.0f, s2, s2, 0.0f, 1.0e-4f, FLT_EPSILON, v, pa, pb);
  164. if (intersects)
  165. {
  166. if (inTreatSphereAsPointWithConvexRadius)
  167. DrawWireSphereSP(mDebugRenderer, s2.GetCenter(), s2.GetRadius(), Color::sRed);
  168. else
  169. DrawSphereSP(mDebugRenderer, s2.GetCenter(), s2.GetRadius(), Color::sRed);
  170. DrawMarkerSP(mDebugRenderer, pa, Color::sYellow, 2.0f);
  171. DrawMarkerSP(mDebugRenderer, pb, Color::sCyan, 2.0f);
  172. }
  173. else
  174. {
  175. if (inTreatSphereAsPointWithConvexRadius)
  176. DrawWireSphereSP(mDebugRenderer, s2.GetCenter(), s2.GetRadius(), Color::sGreen);
  177. else
  178. DrawSphereSP(mDebugRenderer, s2.GetCenter(), s2.GetRadius(), Color::sGreen);
  179. }
  180. DrawArrowSP(mDebugRenderer, inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
  181. }