InteractivePairsTest.cpp 8.0 KB

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