CollideShapeTests.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "PhysicsTestContext.h"
  6. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  7. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  8. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  9. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  10. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  11. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  12. #include <Jolt/Physics/Collision/Shape/TriangleShape.h>
  13. #include <Jolt/Physics/Collision/CollideShape.h>
  14. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  15. #include <Jolt/Physics/Collision/CollisionDispatch.h>
  16. #include <Jolt/Geometry/EPAPenetrationDepth.h>
  17. #include "Layers.h"
  18. TEST_SUITE("CollideShapeTests")
  19. {
  20. // Compares CollideShapeResult for two spheres with given positions and radii
  21. static void sCompareCollideShapeResultSphere(Vec3Arg inPosition1, float inRadius1, Vec3Arg inPosition2, float inRadius2, const CollideShapeResult &inResult)
  22. {
  23. // Test if spheres overlap
  24. Vec3 delta = inPosition2 - inPosition1;
  25. float len = delta.Length();
  26. CHECK(len > 0.0f);
  27. CHECK(len <= inRadius1 + inRadius2);
  28. // Calculate points on surface + vector that will push 2 out of collision
  29. Vec3 expected_point1 = inPosition1 + delta * (inRadius1 / len);
  30. Vec3 expected_point2 = inPosition2 - delta * (inRadius2 / len);
  31. Vec3 expected_penetration_axis = delta / len;
  32. // Get actual results
  33. Vec3 penetration_axis = inResult.mPenetrationAxis.Normalized();
  34. // Compare
  35. CHECK_APPROX_EQUAL(expected_point1, inResult.mContactPointOn1);
  36. CHECK_APPROX_EQUAL(expected_point2, inResult.mContactPointOn2);
  37. CHECK_APPROX_EQUAL(expected_penetration_axis, penetration_axis);
  38. }
  39. // Test CollideShape function for spheres
  40. TEST_CASE("TestCollideShapeSphere")
  41. {
  42. // Locations of test sphere
  43. static const RVec3 cPosition1A(10.0f, 11.0f, 12.0f);
  44. static const RVec3 cPosition1B(10.0f, 21.0f, 12.0f);
  45. static const float cRadius1 = 2.0f;
  46. // Locations of sphere in the physics system
  47. static const RVec3 cPosition2A(13.0f, 11.0f, 12.0f);
  48. static const RVec3 cPosition2B(13.0f, 22.0f, 12.0f);
  49. static const float cRadius2 = 1.5f;
  50. // Create sphere to test with (shape 1)
  51. Ref<Shape> shape1 = new SphereShape(cRadius1);
  52. Mat44 shape1_com = Mat44::sTranslation(shape1->GetCenterOfMass());
  53. RMat44 shape1_transform = RMat44::sTranslation(cPosition1A) * Mat44::sRotationX(0.1f * JPH_PI) * shape1_com;
  54. // Create sphere to collide against (shape 2)
  55. PhysicsTestContext c;
  56. Body &body2 = c.CreateSphere(cPosition2A, cRadius2, EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING);
  57. // Filters
  58. SpecifiedBroadPhaseLayerFilter broadphase_moving_filter(BroadPhaseLayers::MOVING);
  59. SpecifiedBroadPhaseLayerFilter broadphase_non_moving_filter(BroadPhaseLayers::NON_MOVING);
  60. SpecifiedObjectLayerFilter object_moving_filter(Layers::MOVING);
  61. SpecifiedObjectLayerFilter object_non_moving_filter(Layers::NON_MOVING);
  62. // Collector that fails the test
  63. class FailCollideShapeCollector : public CollideShapeCollector
  64. {
  65. public:
  66. virtual void AddHit(const CollideShapeResult &inResult) override
  67. {
  68. FAIL("Callback should not be called");
  69. }
  70. };
  71. FailCollideShapeCollector fail_collector;
  72. // Set settings
  73. CollideShapeSettings settings;
  74. settings.mActiveEdgeMode = EActiveEdgeMode::CollideWithAll;
  75. settings.mBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  76. // Test against wrong layer
  77. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(shape1, Vec3::sReplicate(1.0f), shape1_transform, settings, RVec3::sZero(), fail_collector, broadphase_moving_filter, object_moving_filter);
  78. // Collector that tests that collision happens at position A
  79. class PositionACollideShapeCollector : public CollideShapeCollector
  80. {
  81. public:
  82. PositionACollideShapeCollector(const Body &inBody2) :
  83. mBody2(inBody2)
  84. {
  85. }
  86. virtual void AddHit(const CollideShapeResult &inResult) override
  87. {
  88. CHECK(mBody2.GetID() == GetContext()->mBodyID);
  89. sCompareCollideShapeResultSphere(Vec3(cPosition1A), cRadius1, Vec3(cPosition2A), cRadius2, inResult);
  90. mWasHit = true;
  91. }
  92. bool mWasHit = false;
  93. private:
  94. const Body & mBody2;
  95. };
  96. PositionACollideShapeCollector position_a_collector(body2);
  97. // Test collision against correct layer
  98. CHECK(!position_a_collector.mWasHit);
  99. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(shape1, Vec3::sReplicate(1.0f), shape1_transform, settings, RVec3::sZero(), position_a_collector, broadphase_non_moving_filter, object_non_moving_filter);
  100. CHECK(position_a_collector.mWasHit);
  101. // Now move body to position B
  102. c.GetSystem()->GetBodyInterface().SetPositionAndRotation(body2.GetID(), cPosition2B, Quat::sRotation(Vec3::sAxisY(), 0.2f * JPH_PI), EActivation::DontActivate);
  103. // Test that original position doesn't collide anymore
  104. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(shape1, Vec3::sReplicate(1.0f), shape1_transform, settings, RVec3::sZero(), fail_collector, broadphase_non_moving_filter, object_non_moving_filter);
  105. // Move test shape to position B
  106. shape1_transform = RMat44::sTranslation(cPosition1B) * Mat44::sRotationZ(0.3f * JPH_PI) * shape1_com;
  107. // Test against wrong layer
  108. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(shape1, Vec3::sReplicate(1.0f), shape1_transform, settings, RVec3::sZero(), fail_collector, broadphase_moving_filter, object_moving_filter);
  109. // Callback that tests that collision happens at position B
  110. class PositionBCollideShapeCollector : public CollideShapeCollector
  111. {
  112. public:
  113. PositionBCollideShapeCollector(const Body &inBody2) :
  114. mBody2(inBody2)
  115. {
  116. }
  117. virtual void Reset() override
  118. {
  119. CollideShapeCollector::Reset();
  120. mWasHit = false;
  121. }
  122. virtual void AddHit(const CollideShapeResult &inResult) override
  123. {
  124. CHECK(mBody2.GetID() == GetContext()->mBodyID);
  125. sCompareCollideShapeResultSphere(Vec3(cPosition1B), cRadius1, Vec3(cPosition2B), cRadius2, inResult);
  126. mWasHit = true;
  127. }
  128. bool mWasHit = false;
  129. private:
  130. const Body & mBody2;
  131. };
  132. PositionBCollideShapeCollector position_b_collector(body2);
  133. // Test collision
  134. CHECK(!position_b_collector.mWasHit);
  135. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(shape1, Vec3::sReplicate(1.0f), shape1_transform, settings, RVec3::sZero(), position_b_collector, broadphase_non_moving_filter, object_non_moving_filter);
  136. CHECK(position_b_collector.mWasHit);
  137. // Update the physics system (optimizes the broadphase)
  138. c.Simulate(c.GetDeltaTime());
  139. // Test against wrong layer
  140. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(shape1, Vec3::sReplicate(1.0f), shape1_transform, settings, RVec3::sZero(), fail_collector, broadphase_moving_filter, object_moving_filter);
  141. // Test collision again
  142. position_b_collector.Reset();
  143. CHECK(!position_b_collector.mWasHit);
  144. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(shape1, Vec3::sReplicate(1.0f), shape1_transform, settings, RVec3::sZero(), position_b_collector, broadphase_non_moving_filter, object_non_moving_filter);
  145. CHECK(position_b_collector.mWasHit);
  146. }
  147. // Test CollideShape function for a (scaled) sphere vs box
  148. TEST_CASE("TestCollideShapeSphereVsBox")
  149. {
  150. PhysicsTestContext c;
  151. // Create box to collide against (shape 2)
  152. // The box is scaled up by a factor 10 in the X axis and then rotated so that the X axis is up
  153. BoxShapeSettings box(Vec3::sReplicate(1.0f));
  154. box.SetEmbedded();
  155. ScaledShapeSettings scaled_box(&box, Vec3(10, 1, 1));
  156. scaled_box.SetEmbedded();
  157. 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);
  158. // Set settings
  159. CollideShapeSettings settings;
  160. settings.mActiveEdgeMode = EActiveEdgeMode::CollideWithAll;
  161. settings.mBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  162. {
  163. // Create sphere
  164. Ref<Shape> normal_sphere = new SphereShape(1.0f);
  165. // Collect hit with normal sphere
  166. AllHitCollisionCollector<CollideShapeCollector> collector;
  167. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(normal_sphere, Vec3::sReplicate(1.0f), RMat44::sTranslation(RVec3(0, 11, 0)), settings, RVec3::sZero(), collector);
  168. CHECK(collector.mHits.size() == 1);
  169. const CollideShapeResult &result = collector.mHits.front();
  170. CHECK(result.mBodyID2 == body2.GetID());
  171. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(0, 10, 0), 1.0e-4f);
  172. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3(0, 11, 0), 1.0e-4f);
  173. Vec3 pen_axis = result.mPenetrationAxis.Normalized();
  174. CHECK_APPROX_EQUAL(pen_axis, Vec3(0, -1, 0), 1.0e-4f);
  175. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 1.0f, 1.0e-5f);
  176. }
  177. {
  178. // This repeats the same test as above but uses scaling at all levels
  179. Ref<Shape> scaled_sphere = new ScaledShape(new SphereShape(0.1f), Vec3::sReplicate(5.0f));
  180. // Collect hit with scaled sphere
  181. AllHitCollisionCollector<CollideShapeCollector> collector;
  182. c.GetSystem()->GetNarrowPhaseQuery().CollideShape(scaled_sphere, Vec3::sReplicate(2.0f), RMat44::sTranslation(RVec3(0, 11, 0)), settings, RVec3::sZero(), collector);
  183. CHECK(collector.mHits.size() == 1);
  184. const CollideShapeResult &result = collector.mHits.front();
  185. CHECK(result.mBodyID2 == body2.GetID());
  186. CHECK_APPROX_EQUAL(result.mContactPointOn1, Vec3(0, 10, 0), 1.0e-4f);
  187. CHECK_APPROX_EQUAL(result.mContactPointOn2, Vec3(0, 11, 0), 1.0e-4f);
  188. Vec3 pen_axis = result.mPenetrationAxis.Normalized();
  189. CHECK_APPROX_EQUAL(pen_axis, Vec3(0, -1, 0), 1.0e-4f);
  190. CHECK_APPROX_EQUAL(result.mPenetrationDepth, 1.0f, 1.0e-5f);
  191. }
  192. }
  193. // Test colliding a very long capsule vs a box that is intersecting with the linesegment inside the capsule
  194. // This particular config reported the wrong penetration due to accuracy problems before
  195. TEST_CASE("TestCollideShapeLongCapsuleVsEmbeddedBox")
  196. {
  197. // Create box
  198. Vec3 box_min(-1.0f, -2.0f, 0.5f);
  199. Vec3 box_max(2.0f, -0.5f, 3.0f);
  200. Ref<RotatedTranslatedShapeSettings> box_settings = new RotatedTranslatedShapeSettings(0.5f * (box_min + box_max), Quat::sIdentity(), new BoxShapeSettings(0.5f * (box_max - box_min)));
  201. Ref<Shape> box_shape = box_settings->Create().Get();
  202. Mat44 box_transform(Vec4(0.516170502f, -0.803887904f, -0.295520246f, 0.0f), Vec4(0.815010250f, 0.354940295f, 0.458012700f, 0.0f), Vec4(-0.263298869f, -0.477264702f, 0.838386655f, 0.0f), Vec4(-10.2214508f, -18.6808319f, 40.7468987f, 1.0f));
  203. // Create capsule
  204. float capsule_half_height = 75.0f;
  205. float capsule_radius = 1.5f;
  206. Ref<RotatedTranslatedShapeSettings> capsule_settings = new RotatedTranslatedShapeSettings(Vec3(0, 0, 75), Quat(0.499999970f, -0.499999970f, -0.499999970f, 0.499999970f), new CapsuleShapeSettings(capsule_half_height, capsule_radius));
  207. Ref<Shape> capsule_shape = capsule_settings->Create().Get();
  208. Mat44 capsule_transform = Mat44::sTranslation(Vec3(-9.68538570f, -18.0328083f, 41.3212280f));
  209. // Collision settings
  210. CollideShapeSettings settings;
  211. settings.mActiveEdgeMode = EActiveEdgeMode::CollideWithAll;
  212. settings.mBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  213. settings.mCollectFacesMode = ECollectFacesMode::NoFaces;
  214. // Collide the two shapes
  215. AllHitCollisionCollector<CollideShapeCollector> collector;
  216. CollisionDispatch::sCollideShapeVsShape(capsule_shape, box_shape, Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), capsule_transform, box_transform, SubShapeIDCreator(), SubShapeIDCreator(), settings, collector);
  217. // Check that there was a hit
  218. CHECK(collector.mHits.size() == 1);
  219. const CollideShapeResult &result = collector.mHits.front();
  220. // Now move the box 1% further than the returned penetration depth and check that it is no longer in collision
  221. Vec3 distance_to_move_box = result.mPenetrationAxis.Normalized() * result.mPenetrationDepth;
  222. collector.Reset();
  223. CHECK(!collector.HadHit());
  224. CollisionDispatch::sCollideShapeVsShape(capsule_shape, box_shape, Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), capsule_transform, Mat44::sTranslation(1.01f * distance_to_move_box) * box_transform, SubShapeIDCreator(), SubShapeIDCreator(), settings, collector);
  225. CHECK(!collector.HadHit());
  226. // Now check that moving 1% less than the penetration distance makes the shapes still overlap
  227. CollisionDispatch::sCollideShapeVsShape(capsule_shape, box_shape, Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), capsule_transform, Mat44::sTranslation(0.99f * distance_to_move_box) * box_transform, SubShapeIDCreator(), SubShapeIDCreator(), settings, collector);
  228. CHECK(collector.mHits.size() == 1);
  229. }
  230. // Another test case found in practice of a very large oriented box (convex hull) vs a small triangle outside the hull. This should not report a collision
  231. TEST_CASE("TestCollideShapeSmallTriangleVsLargeBox")
  232. {
  233. // Triangle vertices
  234. Vec3 v0(-81.5637589f, -126.987244f, -146.771729f);
  235. Vec3 v1(-81.8749924f, -127.270691f, -146.544403f);
  236. Vec3 v2(-81.6972275f, -127.383545f, -146.773254f);
  237. // Oriented box vertices
  238. Array<Vec3> obox_points = {
  239. Vec3(125.932892f, -374.712250f, 364.192169f),
  240. Vec3(319.492218f, -73.2614441f, 475.009613f),
  241. Vec3(-122.277550f, -152.200287f, 192.441437f),
  242. Vec3(71.2817841f, 149.250519f, 303.258881f),
  243. Vec3(-77.8921967f, -359.410797f, 678.579712f),
  244. Vec3(115.667137f, -57.9600067f, 789.397095f),
  245. Vec3(-326.102631f, -136.898834f, 506.828949f),
  246. Vec3(-132.543304f, 164.551971f, 617.646362f)
  247. };
  248. ConvexHullShapeSettings hull_settings(obox_points, 0.0f);
  249. RefConst<ConvexShape> convex_hull = static_cast<const ConvexShape *>(hull_settings.Create().Get().GetPtr());
  250. // Create triangle support function
  251. TriangleConvexSupport triangle(v0, v1, v2);
  252. // Create the convex hull support function
  253. ConvexShape::SupportBuffer buffer;
  254. const ConvexShape::Support *support = convex_hull->GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  255. // Triangle is close enough to make GJK report indeterminate
  256. Vec3 penetration_axis = Vec3::sAxisX(), point1, point2;
  257. EPAPenetrationDepth pen_depth;
  258. EPAPenetrationDepth::EStatus status = pen_depth.GetPenetrationDepthStepGJK(*support, support->GetConvexRadius(), triangle, 0.0f, cDefaultCollisionTolerance, penetration_axis, point1, point2);
  259. CHECK(status == EPAPenetrationDepth::EStatus::Indeterminate);
  260. // But there should not be an actual collision
  261. CHECK(!pen_depth.GetPenetrationDepthStepEPA(*support, triangle, cDefaultPenetrationTolerance, penetration_axis, point1, point2));
  262. }
  263. // A test case of a triangle that's nearly parallel to a capsule and penetrating it. This one was causing numerical issues.
  264. TEST_CASE("TestCollideParallelTriangleVsCapsule")
  265. {
  266. Vec3 v1(-0.479988575f, -1.36185002f, 0.269966960f);
  267. Vec3 v2(-0.104996204f, 0.388152480f, 0.269967079f);
  268. Vec3 v3(-0.104996204f, -1.36185002f, 0.269966960f);
  269. TriangleShape triangle(v1, v2, v3);
  270. triangle.SetEmbedded();
  271. float capsule_radius = 0.37f;
  272. float capsule_half_height = 0.5f;
  273. CapsuleShape capsule(capsule_half_height, capsule_radius);
  274. capsule.SetEmbedded();
  275. CollideShapeSettings settings;
  276. AllHitCollisionCollector<CollideShapeCollector> collector;
  277. CollisionDispatch::sCollideShapeVsShape(&triangle, &capsule, Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), Mat44::sIdentity(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), settings, collector);
  278. // The capsule's center is closest to the triangle's edge v2 v3
  279. Vec3 capsule_center_to_triangle_v2_v3 = v3;
  280. capsule_center_to_triangle_v2_v3.SetY(0); // The penetration axis will be in x, z only because the triangle is parallel to the capsule axis
  281. float capsule_center_to_triangle_v2_v3_len = capsule_center_to_triangle_v2_v3.Length();
  282. Vec3 expected_penetration_axis = -capsule_center_to_triangle_v2_v3 / capsule_center_to_triangle_v2_v3_len;
  283. float expected_penetration_depth = capsule_radius - capsule_center_to_triangle_v2_v3_len;
  284. CHECK(collector.mHits.size() == 1);
  285. const CollideShapeResult &hit = collector.mHits[0];
  286. Vec3 actual_penetration_axis = hit.mPenetrationAxis.Normalized();
  287. float actual_penetration_depth = hit.mPenetrationDepth;
  288. CHECK_APPROX_EQUAL(actual_penetration_axis, expected_penetration_axis);
  289. CHECK_APPROX_EQUAL(actual_penetration_depth, expected_penetration_depth);
  290. }
  291. // A test case of a triangle that's nearly parallel to a capsule and penetrating it. This one was causing numerical issues.
  292. TEST_CASE("TestCollideParallelTriangleVsCapsule2")
  293. {
  294. Vec3 v1(-0.0904417038f, -4.72410202f, 0.307858467f);
  295. Vec3 v2(-0.0904417038f, 5.27589798f, 0.307857513f);
  296. Vec3 v3(9.90955830f, 5.27589798f, 0.307864189f);
  297. TriangleShape triangle(v1, v2, v3);
  298. triangle.SetEmbedded();
  299. float capsule_radius = 0.42f;
  300. float capsule_half_height = 0.675f;
  301. CapsuleShape capsule(capsule_half_height, capsule_radius);
  302. capsule.SetEmbedded();
  303. CollideShapeSettings settings;
  304. AllHitCollisionCollector<CollideShapeCollector> collector;
  305. CollisionDispatch::sCollideShapeVsShape(&triangle, &capsule, Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), Mat44::sIdentity(), Mat44::sIdentity(), SubShapeIDCreator(), SubShapeIDCreator(), settings, collector);
  306. // The capsule intersects with the triangle and the closest point is in the interior of the triangle
  307. Vec3 expected_penetration_axis = Vec3(0, 0, -1); // Triangle is in the XY plane so the normal is Z
  308. float expected_penetration_depth = capsule_radius - v1.GetZ();
  309. CHECK(collector.mHits.size() == 1);
  310. const CollideShapeResult &hit = collector.mHits[0];
  311. Vec3 actual_penetration_axis = hit.mPenetrationAxis.Normalized();
  312. float actual_penetration_depth = hit.mPenetrationDepth;
  313. CHECK_APPROX_EQUAL(actual_penetration_axis, expected_penetration_axis);
  314. CHECK_APPROX_EQUAL(actual_penetration_depth, expected_penetration_depth);
  315. }
  316. /*
  317. // TODO: Re-enable after a better fix is found
  318. // A test case of a box and a convex hull that are nearly touching and that should return a contact with correct normal because the collision settings specify a max separation distance. This was producing the wrong normal.
  319. TEST_CASE("BoxVsConvexHullNoConvexRadius")
  320. {
  321. const float separation_distance = 0.001f;
  322. const float box_separation_from_hull = 0.5f * separation_distance;
  323. const float hull_height = 0.25f;
  324. // Box with no convex radius
  325. Ref<BoxShapeSettings> box_settings = new BoxShapeSettings(Vec3(0.25f, 0.75f, 0.375f), 0.0f);
  326. Ref<Shape> box_shape = box_settings->Create().Get();
  327. // Convex hull (also a box) with no convex radius
  328. Vec3 hull_points[] =
  329. {
  330. Vec3(-2.5f, -hull_height, -1.5f),
  331. Vec3(-2.5f, hull_height, -1.5f),
  332. Vec3(2.5f, -hull_height, -1.5f),
  333. Vec3(-2.5f, -hull_height, 1.5f),
  334. Vec3(-2.5f, hull_height, 1.5f),
  335. Vec3(2.5f, hull_height, -1.5f),
  336. Vec3(2.5f, -hull_height, 1.5f),
  337. Vec3(2.5f, hull_height, 1.5f)
  338. };
  339. Ref<ConvexHullShapeSettings> hull_settings = new ConvexHullShapeSettings(hull_points, 8, 0.0f);
  340. Ref<Shape> hull_shape = hull_settings->Create().Get();
  341. float angle = 0.0f;
  342. for (int i = 0; i < 481; ++i)
  343. {
  344. // Slowly rotate both box and convex hull
  345. angle += DegreesToRadians(45.0f) / 60.0f;
  346. Mat44 hull_transform = Mat44::sRotationY(angle);
  347. const Mat44 box_local_translation = Mat44::sTranslation(Vec3(0.1f, 1.0f + box_separation_from_hull, -0.5f));
  348. const Mat44 box_local_rotation = Mat44::sRotationY(DegreesToRadians(-45.0f));
  349. const Mat44 box_local_transform = box_local_translation * box_local_rotation;
  350. const Mat44 box_transform = hull_transform * box_local_transform;
  351. CollideShapeSettings settings;
  352. settings.mMaxSeparationDistance = separation_distance;
  353. ClosestHitCollisionCollector<CollideShapeCollector> collector;
  354. CollisionDispatch::sCollideShapeVsShape(box_shape, hull_shape, Vec3::sReplicate(1.0f), Vec3::sReplicate(1.0f), box_transform, hull_transform, SubShapeIDCreator(), SubShapeIDCreator(), settings, collector);
  355. // Check that there was a hit and that the contact normal is correct
  356. CHECK(collector.HadHit());
  357. const CollideShapeResult &hit = collector.mHit;
  358. CHECK_APPROX_EQUAL(hit.mContactPointOn1.GetY(), hull_height + box_separation_from_hull, 2.0e-4f);
  359. CHECK_APPROX_EQUAL(hit.mContactPointOn2.GetY(), hull_height);
  360. CHECK_APPROX_EQUAL(hit.mPenetrationAxis.NormalizedOr(Vec3::sZero()), -Vec3::sAxisY(), 1.0e-3f);
  361. }
  362. CHECK(angle >= 2.0f * JPH_PI);
  363. }
  364. */
  365. }