CastShapeTests.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include <Physics/Collision/ShapeCast.h>
  5. #include <Physics/Collision/CastResult.h>
  6. #include <Physics/Collision/CollisionCollectorImpl.h>
  7. #include <Physics/Collision/Shape/SphereShape.h>
  8. #include <Physics/Collision/Shape/TriangleShape.h>
  9. #include <Physics/Collision/Shape/MeshShape.h>
  10. #include <Physics/Collision/Shape/ScaledShape.h>
  11. #include <Physics/Collision/Shape/BoxShape.h>
  12. #include <Physics/Collision/ShapeFilter.h>
  13. #include "PhysicsTestContext.h"
  14. #include "Layers.h"
  15. TEST_SUITE("CastShapeTests")
  16. {
  17. /// Helper function that tests a shere against a triangle centered on the origin with normal Z
  18. static void sTestCastSphereTriangle(const Shape *inTriangle)
  19. {
  20. // Create sphere
  21. Ref<Shape> sphere = SphereShapeSettings(0.2f).Create().Get();
  22. {
  23. // Hit front face
  24. ShapeCast shape_cast(sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(0, 0, 15)), Vec3(0, 0, -30));
  25. ShapeCastSettings cast_settings;
  26. cast_settings.mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  27. cast_settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  28. cast_settings.mReturnDeepestPoint = false;
  29. AllHitCollisionCollector<CastShapeCollector> collector;
  30. inTriangle->CastShape(shape_cast, cast_settings, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  31. CHECK(collector.mHits.size() == 1);
  32. const ShapeCastResult &result = collector.mHits.back();
  33. CHECK_APPROX_EQUAL(result.mFraction, (15.0f - 0.2f) / 30.0f, 1.0e-4f);
  34. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, 0, -1), 1.0e-3f);
  35. CHECK(result.mPenetrationDepth == 0.0f);
  36. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3::sZero(), 1.0e-3f);
  37. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3::sZero(), 1.0e-3f);
  38. CHECK(!result.mIsBackFaceHit);
  39. }
  40. {
  41. // Hit back face -> ignored
  42. ShapeCast shape_cast(sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(0, 0, -15)), Vec3(0, 0, 30));
  43. ShapeCastSettings cast_settings;
  44. cast_settings.mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  45. cast_settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  46. cast_settings.mReturnDeepestPoint = false;
  47. AllHitCollisionCollector<CastShapeCollector> collector;
  48. inTriangle->CastShape(shape_cast, cast_settings, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  49. CHECK(collector.mHits.empty());
  50. // Hit back face -> collision
  51. cast_settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  52. cast_settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  53. inTriangle->CastShape(shape_cast, cast_settings, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  54. CHECK(collector.mHits.size() == 1);
  55. const ShapeCastResult &result = collector.mHits.back();
  56. CHECK_APPROX_EQUAL(result.mFraction, (15.0f - 0.2f) / 30.0f, 1.0e-4f);
  57. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, 0, 1), 1.0e-3f);
  58. CHECK(result.mPenetrationDepth == 0.0f);
  59. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3::sZero(), 1.0e-3f);
  60. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3::sZero(), 1.0e-3f);
  61. CHECK(result.mIsBackFaceHit);
  62. }
  63. {
  64. // Hit back face while starting in collision -> ignored
  65. ShapeCast shape_cast(sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(0, 0, -0.1f)), Vec3(0, 0, 15));
  66. ShapeCastSettings cast_settings;
  67. cast_settings.mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  68. cast_settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  69. cast_settings.mReturnDeepestPoint = true;
  70. AllHitCollisionCollector<CastShapeCollector> collector;
  71. inTriangle->CastShape(shape_cast, cast_settings, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  72. CHECK(collector.mHits.empty());
  73. // Hit back face while starting in collision -> collision
  74. cast_settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  75. cast_settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  76. inTriangle->CastShape(shape_cast, cast_settings, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  77. CHECK(collector.mHits.size() == 1);
  78. const ShapeCastResult &result = collector.mHits.back();
  79. CHECK_APPROX_EQUAL(result.mFraction, 0.0f);
  80. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, 0, 1), 1.0e-3f);
  81. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 0.1f, 1.0e-3f);
  82. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(0, 0, 0.1f), 1.0e-3f);
  83. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3::sZero(), 1.0e-3f);
  84. CHECK(result.mIsBackFaceHit);
  85. }
  86. }
  87. TEST_CASE("TestCastSphereTriangle")
  88. {
  89. // Create triangle
  90. Ref<Shape> triangle = TriangleShapeSettings(Vec3(50, 25, 0), Vec3(-50, 25, 0), Vec3(0,-25, 0)).Create().Get();
  91. sTestCastSphereTriangle(triangle);
  92. // Create a triangle mesh shape
  93. Ref<Shape> triangle_mesh = MeshShapeSettings({ Triangle(Float3(50, 25, 0), Float3(-50, 25, 0), Float3(0,-25, 0)) }).Create().Get();
  94. sTestCastSphereTriangle(triangle_mesh);
  95. }
  96. // Test CastShape for a (scaled) sphere vs box
  97. TEST_CASE("TestCastShapeSphereVsBox")
  98. {
  99. PhysicsTestContext c;
  100. // Create box to collide against (shape 2)
  101. // The box is scaled up by a factor 10 in the X axis and then rotated so that the X axis is up
  102. BoxShapeSettings box(Vec3::sReplicate(1.0f));
  103. box.SetEmbedded();
  104. ScaledShapeSettings scaled_box(&box, Vec3(10, 1, 1));
  105. scaled_box.SetEmbedded();
  106. Body &body2 = c.CreateBody(&scaled_box, Vec3(0, 1, 0), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  107. // Set settings
  108. ShapeCastSettings settings;
  109. settings.mReturnDeepestPoint = true;
  110. settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  111. settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  112. {
  113. // Create shape cast
  114. Ref<Shape> normal_sphere = new SphereShape(1.0f);
  115. ShapeCast shape_cast { normal_sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(0, 11, 0)), Vec3(0, 1, 0) };
  116. // Shape is intersecting at the start
  117. AllHitCollisionCollector<CastShapeCollector> collector;
  118. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, collector);
  119. CHECK(collector.mHits.size() == 1);
  120. const ShapeCastResult &result = collector.mHits.front();
  121. CHECK(result.mBodyID2 == body2.GetID());
  122. CHECK_APPROX_EQUAL(result.mFraction, 0.0f);
  123. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, -1, 0), 1.0e-3f);
  124. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 1.0f, 1.0e-5f);
  125. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(0, 10, 0), 1.0e-3f);
  126. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3(0, 11, 0), 1.0e-3f);
  127. CHECK(!result.mIsBackFaceHit);
  128. }
  129. {
  130. // This repeats the same test as above but uses scaling at all levels and validate that the penetration depth is still correct
  131. Ref<Shape> scaled_sphere = new ScaledShape(new SphereShape(0.1f), Vec3::sReplicate(5.0f));
  132. ShapeCast shape_cast { scaled_sphere, Vec3::sReplicate(2.0f), Mat44::sTranslation(Vec3(0, 11, 0)), Vec3(0, 1, 0) };
  133. // Shape is intersecting at the start
  134. AllHitCollisionCollector<CastShapeCollector> collector;
  135. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, collector);
  136. CHECK(collector.mHits.size() == 1);
  137. const ShapeCastResult &result = collector.mHits.front();
  138. CHECK(result.mBodyID2 == body2.GetID());
  139. CHECK_APPROX_EQUAL(result.mFraction, 0.0f);
  140. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, -1, 0), 1.0e-3f);
  141. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 1.0f, 1.0e-5f);
  142. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(0, 10, 0), 1.0e-3f);
  143. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3(0, 11, 0), 1.0e-3f);
  144. CHECK(!result.mIsBackFaceHit);
  145. }
  146. }
  147. // Test CastShape ordering according to penetration depth
  148. TEST_CASE("TestCastShapePenetrationDepthOrdering")
  149. {
  150. PhysicsTestContext c;
  151. // Create box to collide against (shape 2)
  152. BoxShapeSettings box(Vec3(0.1f, 2.0f, 2.0f));
  153. box.SetEmbedded();
  154. // Create 10 boxes that are 0.2 thick in the X axis and 4 in Y and Z, put them all next to each other on the X axis starting from X = 0 going to X = 2
  155. vector<Body *> bodies;
  156. for (int i = 0; i < 10; ++i)
  157. bodies.push_back(&c.CreateBody(&box, Vec3(0.1f + 0.2f * i, 0, 0), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate));
  158. // Set settings
  159. ShapeCastSettings settings;
  160. settings.mReturnDeepestPoint = true;
  161. settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  162. settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  163. settings.mCollisionTolerance = 1.0e-5f; // Increased precision
  164. settings.mPenetrationTolerance = 1.0e-5f;
  165. {
  166. // Create shape cast in X from -5 to 5
  167. RefConst<Shape> sphere = new SphereShape(1.0f);
  168. ShapeCast shape_cast { sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(-5, 0, 0)), Vec3(10, 0, 0) };
  169. // We should hit the first body
  170. ClosestHitCollisionCollector<CastShapeCollector> collector;
  171. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, collector);
  172. CHECK(collector.HadHit());
  173. CHECK(collector.mHit.mBodyID2 == bodies.front()->GetID());
  174. CHECK_APPROX_EQUAL(collector.mHit.mFraction, 4.0f / 10.0f);
  175. CHECK_APPROX_EQUAL(collector.mHit.mPenetrationAxis.Normalized(), Vec3(1, 0, 0), 2.0e-2f);
  176. CHECK_APPROX_EQUAL(collector.mHit.mPenetrationDepth, 0.0f);
  177. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn1, Vec3(0, 0, 0));
  178. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn2, Vec3(0, 0, 0));
  179. CHECK(!collector.mHit.mIsBackFaceHit);
  180. }
  181. {
  182. // Create shape cast in X from 5 to -5
  183. RefConst<Shape> sphere = new SphereShape(1.0f);
  184. ShapeCast shape_cast { sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(5, 0, 0)), Vec3(-10, 0, 0) };
  185. // We should hit the last body
  186. ClosestHitCollisionCollector<CastShapeCollector> collector;
  187. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, collector);
  188. CHECK(collector.HadHit());
  189. CHECK(collector.mHit.mBodyID2 == bodies.back()->GetID());
  190. CHECK_APPROX_EQUAL(collector.mHit.mFraction, 2.0f / 10.0f);
  191. CHECK_APPROX_EQUAL(collector.mHit.mPenetrationAxis.Normalized(), Vec3(-1, 0, 0), 1.0e-3f);
  192. CHECK_APPROX_EQUAL(collector.mHit.mPenetrationDepth, 0.0f);
  193. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn1, Vec3(2, 0, 0), 1.0e-4f);
  194. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn2, Vec3(2, 0, 0), 1.0e-4f);
  195. CHECK(!collector.mHit.mIsBackFaceHit);
  196. }
  197. {
  198. // Create shape cast in X from 1.05 to 11, this should intersect with all bodies and have deepest penetration in bodies[5]
  199. RefConst<Shape> sphere = new SphereShape(1.0f);
  200. ShapeCast shape_cast { sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(1.05f, 0, 0)), Vec3(10, 0, 0) };
  201. // We should hit bodies[5]
  202. AllHitCollisionCollector<CastShapeCollector> collector;
  203. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, collector);
  204. collector.Sort();
  205. CHECK(collector.mHits.size() == 10);
  206. const ShapeCastResult &result = collector.mHits.front();
  207. CHECK(result.mBodyID2 == bodies[5]->GetID());
  208. CHECK_APPROX_EQUAL(result.mFraction, 0.0f);
  209. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(1, 0, 0), 1.0e-3f);
  210. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 1.05f);
  211. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(2.05f, 0, 0), 1.0e-5f); // Box starts at 1.0, center of sphere adds 0.05, radius of sphere is 1
  212. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3(1.0f, 0, 0), 1.0e-5f); // Box starts at 1.0
  213. CHECK(!result.mIsBackFaceHit);
  214. }
  215. }
  216. }