InteractivePairsTest.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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::ProcessInput(const ProcessInputParams &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. }
  42. void InteractivePairsTest::PrePhysicsUpdate(const PreUpdateParams &inParams)
  43. {
  44. float z = 0.0f;
  45. const float r1 = 0.25f * JPH_PI;
  46. 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
  47. for (int i = 0; i < 2; ++i)
  48. {
  49. const float cvx_radius = i == 0? 0.0f : 0.1f; // First round without convex radius, second with
  50. const float edge_len = 1.0f - cvx_radius;
  51. AABox b(Vec3(-edge_len, -edge_len, -edge_len), Vec3(edge_len, edge_len, edge_len));
  52. // Face vs face
  53. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, 0, 0), cvx_radius, b);
  54. z += 4;
  55. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(r1, 0, 0), cvx_radius, b);
  56. z += 4;
  57. // Face vs edge
  58. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
  59. z += 4;
  60. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, 0, r1), cvx_radius, b);
  61. z += 4;
  62. // Face vs vertex
  63. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
  64. z += 4;
  65. // Edge vs edge
  66. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, r1, 0), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
  67. z += 4;
  68. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, 0, r1), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
  69. z += 4;
  70. // Edge vs vertex
  71. TestBoxVsBox(Vec3(0, 0, z), Vec3(0, r2, r1), cvx_radius, b, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
  72. z += 4;
  73. // Sphere vs face
  74. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, 0, 0), cvx_radius, b);
  75. z += 4;
  76. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(r1, 0, 0), cvx_radius, b);
  77. z += 4;
  78. // Sphere vs edge
  79. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, r1, 0), cvx_radius, b);
  80. z += 4;
  81. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, 0, r1), cvx_radius, b);
  82. z += 4;
  83. // Sphere vs vertex
  84. TestSphereVsBox(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), Vec3(0, r2, r1), cvx_radius, b);
  85. z += 4;
  86. // Sphere vs sphere
  87. TestSphereVsSphere(Vec3(0, 0, z), 1.0f, Vec3(mDistance, 0, z), 1.0f, i == 1);
  88. z += 4;
  89. }
  90. }
  91. void InteractivePairsTest::TestBoxVsBox(Vec3Arg inTranslationA, Vec3Arg inRotationA, float inConvexRadiusA, const AABox &inA, Vec3Arg inTranslationB, Vec3Arg inRotationB, float inConvexRadiusB, const AABox &inB)
  92. {
  93. Mat44 mat_a = Mat44::sTranslation(inTranslationA) * Mat44::sRotationX(inRotationA.GetX()) * Mat44::sRotationY(inRotationA.GetY()) * Mat44::sRotationZ(inRotationA.GetZ());
  94. TransformedConvexObject<AABox> a(mat_a, inA);
  95. Mat44 mat_b = Mat44::sTranslation(inTranslationB) * Mat44::sRotationX(inRotationB.GetX()) * Mat44::sRotationY(inRotationB.GetY()) * Mat44::sRotationZ(inRotationB.GetZ());
  96. TransformedConvexObject<AABox> b(mat_b, inB);
  97. EPAPenetrationDepth pen_depth;
  98. Vec3 v = Vec3::sAxisX(), pa, pb;
  99. DrawBoxSP(mDebugRenderer, mat_a, inA, Color::sWhite);
  100. AABox widened_a = inA;
  101. widened_a.ExpandBy(Vec3::sReplicate(inConvexRadiusA));
  102. AABox widened_b = inB;
  103. widened_b.ExpandBy(Vec3::sReplicate(inConvexRadiusB));
  104. DrawBoxSP(mDebugRenderer, mat_a, inA, Color::sWhite);
  105. if (inConvexRadiusA > 0.0f)
  106. DrawWireBoxSP(mDebugRenderer, mat_a, widened_a, Color::sWhite);
  107. AddConvexRadius<TransformedConvexObject<AABox>> a_inc(a, inConvexRadiusA);
  108. AddConvexRadius<TransformedConvexObject<AABox>> b_inc(b, inConvexRadiusB);
  109. if (pen_depth.GetPenetrationDepth(a, a_inc, inConvexRadiusA, b, b_inc, inConvexRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb))
  110. {
  111. DrawBoxSP(mDebugRenderer, mat_b, inB, Color::sRed);
  112. if (inConvexRadiusB > 0.0f)
  113. DrawWireBoxSP(mDebugRenderer, mat_b, widened_b, Color::sRed);
  114. DrawMarkerSP(mDebugRenderer, pa, Color::sYellow, 2.0f);
  115. DrawMarkerSP(mDebugRenderer, pb, Color::sCyan, 2.0f);
  116. }
  117. else
  118. {
  119. DrawBoxSP(mDebugRenderer, mat_b, inB, Color::sGreen);
  120. if (inConvexRadiusB > 0.0f)
  121. DrawWireBoxSP(mDebugRenderer, mat_b, widened_b, Color::sGreen);
  122. }
  123. DrawArrowSP(mDebugRenderer, inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
  124. }
  125. void InteractivePairsTest::TestSphereVsBox(Vec3Arg inTranslationA, float inRadiusA, Vec3Arg inTranslationB, Vec3Arg inRotationB, float inConvexRadiusB, const AABox &inB)
  126. {
  127. Sphere s(inTranslationA, inRadiusA);
  128. Mat44 mat_b = Mat44::sTranslation(inTranslationB) * Mat44::sRotationX(inRotationB.GetX()) * Mat44::sRotationY(inRotationB.GetY()) * Mat44::sRotationZ(inRotationB.GetZ());
  129. TransformedConvexObject<AABox> b(mat_b, inB);
  130. AABox widened_b = inB;
  131. widened_b.ExpandBy(Vec3::sReplicate(inConvexRadiusB));
  132. EPAPenetrationDepth pen_depth;
  133. Vec3 v = Vec3::sAxisX(), pa, pb;
  134. DrawSphereSP(mDebugRenderer, inTranslationA, inRadiusA, Color::sWhite);
  135. AddConvexRadius<TransformedConvexObject<AABox>> b_inc(b, inConvexRadiusB);
  136. if (pen_depth.GetPenetrationDepth(s, s, 0.0f, b, b_inc, inConvexRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb))
  137. {
  138. DrawBoxSP(mDebugRenderer, mat_b, inB, Color::sRed);
  139. if (inConvexRadiusB > 0.0f)
  140. DrawWireBoxSP(mDebugRenderer, mat_b, widened_b, Color::sRed);
  141. DrawMarkerSP(mDebugRenderer, pa, Color::sYellow, 2.0f);
  142. DrawMarkerSP(mDebugRenderer, pb, Color::sCyan, 2.0f);
  143. }
  144. else
  145. {
  146. DrawBoxSP(mDebugRenderer, mat_b, inB, Color::sGreen);
  147. if (inConvexRadiusB > 0.0f)
  148. DrawWireBoxSP(mDebugRenderer, mat_b, widened_b, Color::sGreen);
  149. }
  150. DrawArrowSP(mDebugRenderer, inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
  151. }
  152. void InteractivePairsTest::TestSphereVsSphere(Vec3Arg inTranslationA, float inRadiusA, Vec3Arg inTranslationB, float inRadiusB, bool inTreatSphereAsPointWithConvexRadius)
  153. {
  154. Sphere s1(inTranslationA, inRadiusA);
  155. Sphere s2(inTranslationB, inRadiusB);
  156. if (inTreatSphereAsPointWithConvexRadius)
  157. DrawWireSphereSP(mDebugRenderer, s1.GetCenter(), s1.GetRadius(), Color::sWhite);
  158. else
  159. DrawSphereSP(mDebugRenderer, s1.GetCenter(), s1.GetRadius(), Color::sWhite);
  160. bool intersects;
  161. EPAPenetrationDepth pen_depth;
  162. Vec3 v = Vec3::sAxisX(), pa, pb;
  163. if (inTreatSphereAsPointWithConvexRadius)
  164. intersects = pen_depth.GetPenetrationDepth(PointConvexSupport { inTranslationA }, s1, inRadiusA, PointConvexSupport { inTranslationB }, s2, inRadiusB, 1.0e-4f, FLT_EPSILON, v, pa, pb);
  165. else
  166. intersects = pen_depth.GetPenetrationDepth(s1, s1, 0.0f, s2, s2, 0.0f, 1.0e-4f, FLT_EPSILON, v, pa, pb);
  167. if (intersects)
  168. {
  169. if (inTreatSphereAsPointWithConvexRadius)
  170. DrawWireSphereSP(mDebugRenderer, s2.GetCenter(), s2.GetRadius(), Color::sRed);
  171. else
  172. DrawSphereSP(mDebugRenderer, s2.GetCenter(), s2.GetRadius(), Color::sRed);
  173. DrawMarkerSP(mDebugRenderer, pa, Color::sYellow, 2.0f);
  174. DrawMarkerSP(mDebugRenderer, pb, Color::sCyan, 2.0f);
  175. }
  176. else
  177. {
  178. if (inTreatSphereAsPointWithConvexRadius)
  179. DrawWireSphereSP(mDebugRenderer, s2.GetCenter(), s2.GetRadius(), Color::sGreen);
  180. else
  181. DrawSphereSP(mDebugRenderer, s2.GetCenter(), s2.GetRadius(), Color::sGreen);
  182. }
  183. DrawArrowSP(mDebugRenderer, inTranslationB + Vec3(0, 2, 0), inTranslationB + v + Vec3(0, 2, 0), Color::sOrange, 0.05f);
  184. }