CastShapeTests.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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/StaticCompoundShape.h>
  10. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  11. #include <Jolt/Physics/Collision/Shape/TriangleShape.h>
  12. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  13. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  14. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  15. #include <Jolt/Physics/Collision/ShapeFilter.h>
  16. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  17. #include <Jolt/Physics/Collision/CastSphereVsTriangles.h>
  18. #include "PhysicsTestContext.h"
  19. #include "Layers.h"
  20. TEST_SUITE("CastShapeTests")
  21. {
  22. /// Helper function that tests a sphere against a triangle
  23. static void sTestCastSphereVertexOrEdge(const Shape *inSphere, Vec3Arg inPosition, Vec3Arg inDirection, const Shape *inTriangle)
  24. {
  25. ShapeCast shape_cast(inSphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(inPosition - inDirection), inDirection);
  26. ShapeCastSettings cast_settings;
  27. cast_settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  28. cast_settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  29. AllHitCollisionCollector<CastShapeCollector> collector;
  30. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, cast_settings, inTriangle, 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, 1.0f - 0.2f / inDirection.Length(), 1.0e-4f);
  34. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), inDirection.Normalized(), 1.0e-3f);
  35. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 0.0f, 1.0e-3f);
  36. CHECK_APPROX_EQUAL(result.mContactPointOn1, inPosition, 1.0e-3f);
  37. CHECK_APPROX_EQUAL(result.mContactPointOn2, inPosition, 1.0e-3f);
  38. }
  39. /// Helper function that tests a sphere against a triangle centered on the origin with normal Z
  40. static void sTestCastSphereTriangle(const Shape *inTriangle)
  41. {
  42. // Create sphere
  43. Ref<Shape> sphere = SphereShapeSettings(0.2f).Create().Get();
  44. {
  45. // Hit front face
  46. ShapeCast shape_cast(sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(0, 0, 15)), Vec3(0, 0, -30));
  47. ShapeCastSettings cast_settings;
  48. cast_settings.mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  49. cast_settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  50. cast_settings.mReturnDeepestPoint = false;
  51. AllHitCollisionCollector<CastShapeCollector> collector;
  52. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, cast_settings, inTriangle, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  53. CHECK(collector.mHits.size() == 1);
  54. const ShapeCastResult &result = collector.mHits.back();
  55. CHECK_APPROX_EQUAL(result.mFraction, (15.0f - 0.2f) / 30.0f, 1.0e-4f);
  56. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, 0, -1), 1.0e-3f);
  57. CHECK(result.mPenetrationDepth == 0.0f);
  58. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3::sZero(), 1.0e-3f);
  59. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3::sZero(), 1.0e-3f);
  60. CHECK(!result.mIsBackFaceHit);
  61. }
  62. {
  63. // Hit back face -> ignored
  64. ShapeCast shape_cast(sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(0, 0, -15)), Vec3(0, 0, 30));
  65. ShapeCastSettings cast_settings;
  66. cast_settings.mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  67. cast_settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  68. cast_settings.mReturnDeepestPoint = false;
  69. AllHitCollisionCollector<CastShapeCollector> collector;
  70. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, cast_settings, inTriangle, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  71. CHECK(collector.mHits.empty());
  72. // Hit back face -> collision
  73. cast_settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  74. cast_settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  75. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, cast_settings, inTriangle, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  76. CHECK(collector.mHits.size() == 1);
  77. const ShapeCastResult &result = collector.mHits.back();
  78. CHECK_APPROX_EQUAL(result.mFraction, (15.0f - 0.2f) / 30.0f, 1.0e-4f);
  79. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, 0, 1), 1.0e-3f);
  80. CHECK(result.mPenetrationDepth == 0.0f);
  81. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3::sZero(), 1.0e-3f);
  82. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3::sZero(), 1.0e-3f);
  83. CHECK(result.mIsBackFaceHit);
  84. }
  85. {
  86. // Hit back face while starting in collision -> ignored
  87. ShapeCast shape_cast(sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(0, 0, -0.1f)), Vec3(0, 0, 15));
  88. ShapeCastSettings cast_settings;
  89. cast_settings.mBackFaceModeTriangles = EBackFaceMode::IgnoreBackFaces;
  90. cast_settings.mBackFaceModeConvex = EBackFaceMode::IgnoreBackFaces;
  91. cast_settings.mReturnDeepestPoint = true;
  92. AllHitCollisionCollector<CastShapeCollector> collector;
  93. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, cast_settings, inTriangle, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  94. CHECK(collector.mHits.empty());
  95. // Hit back face while starting in collision -> collision
  96. cast_settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  97. cast_settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  98. CollisionDispatch::sCastShapeVsShapeLocalSpace(shape_cast, cast_settings, inTriangle, Vec3::sReplicate(1.0f), ShapeFilter(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), collector);
  99. CHECK(collector.mHits.size() == 1);
  100. const ShapeCastResult &result = collector.mHits.back();
  101. CHECK_APPROX_EQUAL(result.mFraction, 0.0f);
  102. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, 0, 1), 1.0e-3f);
  103. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 0.1f, 1.0e-3f);
  104. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(0, 0, 0.1f), 1.0e-3f);
  105. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3::sZero(), 1.0e-3f);
  106. CHECK(result.mIsBackFaceHit);
  107. }
  108. // Hit vertex 1, 2 and 3
  109. sTestCastSphereVertexOrEdge(sphere, Vec3(50, 25, 0), Vec3(-10, -10, 0), inTriangle);
  110. sTestCastSphereVertexOrEdge(sphere, Vec3(-50, 25, 0), Vec3(10, -10, 0), inTriangle);
  111. sTestCastSphereVertexOrEdge(sphere, Vec3(0, -25, 0), Vec3(0, 10, 0), inTriangle);
  112. // Hit edge 1, 2 and 3
  113. sTestCastSphereVertexOrEdge(sphere, Vec3(0, 25, 0), Vec3(0, -10, 0), inTriangle); // Edge: Vec3(50, 25, 0), Vec3(-50, 25, 0)
  114. sTestCastSphereVertexOrEdge(sphere, Vec3(-25, 0, 0), Vec3(10, 10, 0), inTriangle); // Edge: Vec3(-50, 25, 0), Vec3(0,-25, 0)
  115. sTestCastSphereVertexOrEdge(sphere, Vec3(25, 0, 0), Vec3(-10, 10, 0), inTriangle); // Edge: Float3(0,-25, 0), Float3(50, 25, 0)
  116. }
  117. TEST_CASE("TestCastSphereTriangle")
  118. {
  119. // Create triangle
  120. Ref<Shape> triangle = TriangleShapeSettings(Vec3(50, 25, 0), Vec3(-50, 25, 0), Vec3(0,-25, 0)).Create().Get();
  121. sTestCastSphereTriangle(triangle);
  122. // Create a triangle mesh shape
  123. Ref<Shape> triangle_mesh = MeshShapeSettings({ Triangle(Float3(50, 25, 0), Float3(-50, 25, 0), Float3(0,-25, 0)) }).Create().Get();
  124. sTestCastSphereTriangle(triangle_mesh);
  125. }
  126. // Test CastShape for a (scaled) sphere vs box
  127. TEST_CASE("TestCastShapeSphereVsBox")
  128. {
  129. PhysicsTestContext c;
  130. // Create box to collide against (shape 2)
  131. // The box is scaled up by a factor 10 in the X axis and then rotated so that the X axis is up
  132. BoxShapeSettings box(Vec3::sReplicate(1.0f));
  133. box.SetEmbedded();
  134. ScaledShapeSettings scaled_box(&box, Vec3(10, 1, 1));
  135. scaled_box.SetEmbedded();
  136. 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);
  137. // Set settings
  138. ShapeCastSettings settings;
  139. settings.mReturnDeepestPoint = true;
  140. settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  141. settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  142. {
  143. // Create shape cast
  144. Ref<Shape> normal_sphere = new SphereShape(1.0f);
  145. RShapeCast shape_cast { normal_sphere, Vec3::sReplicate(1.0f), RMat44::sTranslation(RVec3(0, 11, 0)), Vec3(0, 1, 0) };
  146. // Shape is intersecting at the start
  147. AllHitCollisionCollector<CastShapeCollector> collector;
  148. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, RVec3::sZero(), collector);
  149. CHECK(collector.mHits.size() == 1);
  150. const ShapeCastResult &result = collector.mHits.front();
  151. CHECK(result.mBodyID2 == body2.GetID());
  152. CHECK_APPROX_EQUAL(result.mFraction, 0.0f);
  153. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, -1, 0), 1.0e-3f);
  154. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 1.0f, 1.0e-5f);
  155. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(0, 10, 0), 1.0e-3f);
  156. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3(0, 11, 0), 1.0e-3f);
  157. CHECK(!result.mIsBackFaceHit);
  158. }
  159. {
  160. // This repeats the same test as above but uses scaling at all levels and validate that the penetration depth is still correct
  161. Ref<Shape> scaled_sphere = new ScaledShape(new SphereShape(0.1f), Vec3::sReplicate(5.0f));
  162. RShapeCast shape_cast { scaled_sphere, Vec3::sReplicate(2.0f), RMat44::sTranslation(RVec3(0, 11, 0)), Vec3(0, 1, 0) };
  163. // Shape is intersecting at the start
  164. AllHitCollisionCollector<CastShapeCollector> collector;
  165. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, RVec3::sZero(), collector);
  166. CHECK(collector.mHits.size() == 1);
  167. const ShapeCastResult &result = collector.mHits.front();
  168. CHECK(result.mBodyID2 == body2.GetID());
  169. CHECK_APPROX_EQUAL(result.mFraction, 0.0f);
  170. CHECK_APPROX_EQUAL(result.mPenetrationAxis.Normalized(), Vec3(0, -1, 0), 1.0e-3f);
  171. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 1.0f, 1.0e-5f);
  172. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(0, 10, 0), 1.0e-3f);
  173. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3(0, 11, 0), 1.0e-3f);
  174. CHECK(!result.mIsBackFaceHit);
  175. }
  176. }
  177. // Test CastShape ordering according to penetration depth
  178. TEST_CASE("TestCastShapePenetrationDepthOrdering")
  179. {
  180. PhysicsTestContext c;
  181. // Create box to collide against (shape 2)
  182. BoxShapeSettings box(Vec3(0.1f, 2.0f, 2.0f));
  183. box.SetEmbedded();
  184. // 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
  185. Array<Body *> bodies;
  186. for (int i = 0; i < 10; ++i)
  187. 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));
  188. // Set settings
  189. ShapeCastSettings settings;
  190. settings.mReturnDeepestPoint = true;
  191. settings.mBackFaceModeTriangles = EBackFaceMode::CollideWithBackFaces;
  192. settings.mBackFaceModeConvex = EBackFaceMode::CollideWithBackFaces;
  193. settings.mCollisionTolerance = 1.0e-5f; // Increased precision
  194. settings.mPenetrationTolerance = 1.0e-5f;
  195. {
  196. // Create shape cast in X from -5 to 5
  197. RefConst<Shape> sphere = new SphereShape(1.0f);
  198. RShapeCast shape_cast { sphere, Vec3::sReplicate(1.0f), RMat44::sTranslation(RVec3(-5, 0, 0)), Vec3(10, 0, 0) };
  199. // We should hit the first body
  200. ClosestHitCollisionCollector<CastShapeCollector> collector;
  201. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, RVec3::sZero(), collector);
  202. CHECK(collector.HadHit());
  203. CHECK(collector.mHit.mBodyID2 == bodies.front()->GetID());
  204. CHECK_APPROX_EQUAL(collector.mHit.mFraction, 4.0f / 10.0f);
  205. CHECK(collector.mHit.mPenetrationAxis.Normalized().Dot(Vec3(1, 0, 0)) > Cos(DegreesToRadians(1.0f)));
  206. CHECK_APPROX_EQUAL(collector.mHit.mPenetrationDepth, 0.0f);
  207. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn1, Vec3(0, 0, 0), 2.0e-3f);
  208. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn2, Vec3(0, 0, 0), 2.0e-3f);
  209. CHECK(!collector.mHit.mIsBackFaceHit);
  210. }
  211. {
  212. // Create shape cast in X from 5 to -5
  213. RefConst<Shape> sphere = new SphereShape(1.0f);
  214. RShapeCast shape_cast { sphere, Vec3::sReplicate(1.0f), RMat44::sTranslation(RVec3(5, 0, 0)), Vec3(-10, 0, 0) };
  215. // We should hit the last body
  216. ClosestHitCollisionCollector<CastShapeCollector> collector;
  217. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, RVec3::sZero(), collector);
  218. CHECK(collector.HadHit());
  219. CHECK(collector.mHit.mBodyID2 == bodies.back()->GetID());
  220. CHECK_APPROX_EQUAL(collector.mHit.mFraction, 2.0f / 10.0f, 1.0e-4f);
  221. CHECK(collector.mHit.mPenetrationAxis.Normalized().Dot(Vec3(-1, 0, 0)) > Cos(DegreesToRadians(1.0f)));
  222. CHECK_APPROX_EQUAL(collector.mHit.mPenetrationDepth, 0.0f);
  223. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn1, Vec3(2, 0, 0), 4.0e-4f);
  224. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn2, Vec3(2, 0, 0), 4.0e-4f);
  225. CHECK(!collector.mHit.mIsBackFaceHit);
  226. }
  227. {
  228. // Create shape cast in X from 1.05 to 11, this should intersect with all bodies and have deepest penetration in bodies[5]
  229. RefConst<Shape> sphere = new SphereShape(1.0f);
  230. RShapeCast shape_cast { sphere, Vec3::sReplicate(1.0f), RMat44::sTranslation(RVec3(1.05_r, 0, 0)), Vec3(10, 0, 0) };
  231. // We should hit bodies[5]
  232. AllHitCollisionCollector<CastShapeCollector> collector;
  233. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, settings, RVec3::sZero(), collector);
  234. collector.Sort();
  235. CHECK(collector.mHits.size() == 10);
  236. const ShapeCastResult &result = collector.mHits.front();
  237. CHECK(result.mBodyID2 == bodies[5]->GetID());
  238. CHECK_APPROX_EQUAL(result.mFraction, 0.0f);
  239. CHECK(result.mPenetrationAxis.Normalized().Dot(Vec3(1, 0, 0)) > Cos(DegreesToRadians(1.0f)));
  240. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 1.05f);
  241. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(2.05f, 0, 0), 2.0e-5f); // Box starts at 1.0, center of sphere adds 0.05, radius of sphere is 1
  242. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3(1.0f, 0, 0), 2.0e-5f); // Box starts at 1.0
  243. CHECK(!result.mIsBackFaceHit);
  244. }
  245. }
  246. // Test casting a capsule against a mesh that is intersecting at fraction 0 and test that it returns the deepest penetration
  247. TEST_CASE("TestDeepestPenetrationAtFraction0")
  248. {
  249. // Create an n x n grid of triangles
  250. const int n = 10;
  251. const float s = 0.1f;
  252. TriangleList triangles;
  253. for (int z = 0; z < n; ++z)
  254. for (int x = 0; x < n; ++x)
  255. {
  256. float fx = s * x - s * n / 2, fz = s * z - s * n / 2;
  257. triangles.push_back(Triangle(Vec3(fx, 0, fz), Vec3(fx, 0, fz + s), Vec3(fx + s, 0, fz + s)));
  258. triangles.push_back(Triangle(Vec3(fx, 0, fz), Vec3(fx + s, 0, fz + s), Vec3(fx + s, 0, fz)));
  259. }
  260. MeshShapeSettings mesh_settings(triangles);
  261. mesh_settings.SetEmbedded();
  262. // Create a compound shape with two copies of the mesh
  263. StaticCompoundShapeSettings compound_settings;
  264. compound_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), &mesh_settings);
  265. compound_settings.AddShape(Vec3(0, -0.01f, 0), Quat::sIdentity(), &mesh_settings); // This will not result in the deepest penetration
  266. compound_settings.SetEmbedded();
  267. // Add it to the scene
  268. PhysicsTestContext c;
  269. c.CreateBody(&compound_settings, RVec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  270. // Add the same compound a little bit lower (this will not result in the deepest penetration)
  271. c.CreateBody(&compound_settings, RVec3(0, -0.1_r, 0), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, EActivation::DontActivate);
  272. // We want the deepest hit
  273. ShapeCastSettings cast_settings;
  274. cast_settings.mReturnDeepestPoint = true;
  275. // Create capsule to test
  276. const float capsule_half_height = 2.0f;
  277. const float capsule_radius = 1.0f;
  278. RefConst<Shape> cast_shape = new CapsuleShape(capsule_half_height, capsule_radius);
  279. // Cast the shape starting inside the mesh with a long distance so that internally in the mesh shape the RayAABox4 test will return a low negative fraction.
  280. // This used to be confused with the penetration depth and would cause an early out and return the wrong result.
  281. const float capsule_offset = 0.1f;
  282. RShapeCast shape_cast(cast_shape, Vec3::sReplicate(1.0f), RMat44::sTranslation(RVec3(0, capsule_half_height + capsule_offset, 0)), Vec3(0, -100, 0));
  283. // Cast first using the closest hit collector
  284. ClosestHitCollisionCollector<CastShapeCollector> collector;
  285. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, cast_settings, RVec3::sZero(), collector);
  286. // Check that it indeed found a hit at fraction 0 with the deepest penetration of all triangles
  287. CHECK(collector.HadHit());
  288. CHECK(collector.mHit.mFraction == 0.0f);
  289. CHECK_APPROX_EQUAL(collector.mHit.mPenetrationDepth, capsule_radius - capsule_offset, 1.0e-4f);
  290. CHECK_APPROX_EQUAL(collector.mHit.mPenetrationAxis.Normalized(), Vec3(0, -1, 0));
  291. CHECK_APPROX_EQUAL(collector.mHit.mContactPointOn2, Vec3::sZero());
  292. // Cast again while triggering a force early out after the first hit
  293. class MyCollector : public CastShapeCollector
  294. {
  295. public:
  296. virtual void AddHit(const ShapeCastResult &inResult) override
  297. {
  298. ++mNumHits;
  299. ForceEarlyOut();
  300. }
  301. int mNumHits = 0;
  302. };
  303. MyCollector collector2;
  304. c.GetSystem()->GetNarrowPhaseQuery().CastShape(shape_cast, cast_settings, RVec3::sZero(), collector2);
  305. // Ensure that we indeed stopped after the first hit
  306. CHECK(collector2.mNumHits == 1);
  307. }
  308. // Test a problem case where a sphere cast would incorrectly hit a degenerate triangle (see: https://github.com/jrouwe/JoltPhysics/issues/886)
  309. TEST_CASE("TestCastSphereVsDegenerateTriangle")
  310. {
  311. AllHitCollisionCollector<CastShapeCollector> collector;
  312. SphereShape sphere(0.2f);
  313. sphere.SetEmbedded();
  314. ShapeCast cast(&sphere, Vec3::sReplicate(1.0f), Mat44::sTranslation(Vec3(14.8314590f, 8.19055080f, -4.30825043f)), Vec3(-0.0988006592f, 5.96046448e-08f, 0.000732421875f));
  315. ShapeCastSettings settings;
  316. CastSphereVsTriangles caster(cast, settings, Vec3::sReplicate(1.0f), Mat44::sIdentity(), { }, collector);
  317. caster.Cast(Vec3(14.5536213f, 10.5973721f, -0.00600051880f), Vec3(14.5536213f, 10.5969315f, -3.18638134f), Vec3(14.5536213f, 10.5969315f, -5.18637228f), 0b111, SubShapeID());
  318. CHECK(!collector.HadHit());
  319. }
  320. }