CastShapeTests.cpp 18 KB

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