CastShapeTests.cpp 14 KB

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