RayShapeTests.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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/RayCast.h>
  6. #include <Jolt/Physics/Collision/CastResult.h>
  7. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  8. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  9. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  10. #include <Jolt/Physics/Collision/Shape/ConvexHullShape.h>
  11. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  12. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  13. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  14. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  15. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  16. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  17. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  18. #include <Jolt/Physics/PhysicsSystem.h>
  19. #include <Layers.h>
  20. TEST_SUITE("RayShapeTests")
  21. {
  22. // Function that does the actual ray cast test, inExpectedFraction1/2 should be FLT_MAX if no hit expected
  23. using TestFunction = function<void(const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)>;
  24. // Test ray against inShape with lines going through inHitA and inHitB (which should be surface positions of the shape)
  25. static void TestRayHelperInternal(Vec3Arg inHitA, Vec3Arg inHitB, TestFunction inTestFunction)
  26. {
  27. // Determine points before and after the surface on both sides
  28. Vec3 delta = inHitB - inHitA;
  29. Vec3 l1 = inHitA - 2.0f * delta;
  30. Vec3 l2 = inHitA - 0.1f * delta;
  31. Vec3 i1 = inHitA + 0.1f * delta;
  32. Vec3 i2 = inHitB - 0.1f * delta;
  33. Vec3 r1 = inHitB + 0.1f * delta;
  34. Vec3 r2 = inHitB + 2.0f * delta;
  35. // -O---->-|--------|--------
  36. inTestFunction(RayCast { l1, l2 - l1 }, FLT_MAX, FLT_MAX);
  37. // -----O>-|--------|--------
  38. inTestFunction(RayCast { l2, Vec3::sZero() }, FLT_MAX, FLT_MAX);
  39. // ------O-|->------|--------
  40. inTestFunction(RayCast { l2, i1 - l2 }, 0.5f, FLT_MAX);
  41. // ------O-|--------|->------
  42. inTestFunction(RayCast { l2, r1 - l2 }, 0.1f / 1.2f, 1.1f / 1.2f);
  43. // --------|-----O>-|--------
  44. inTestFunction(RayCast { i2, Vec3::sZero() }, 0.0f, FLT_MAX);
  45. // --------|------O-|->------
  46. inTestFunction(RayCast { i2, r1 - i2 }, 0.0f, 0.5f);
  47. // --------|--------|-O---->-
  48. inTestFunction(RayCast { r1, r2 - l1 }, FLT_MAX, FLT_MAX);
  49. }
  50. static void TestRayHelper(const Shape *inShape, Vec3Arg inHitA, Vec3Arg inHitB)
  51. {
  52. //////////////////////////////////////////////////////////////////////////////////////////////////
  53. // Test function that directly tests against a shape
  54. //////////////////////////////////////////////////////////////////////////////////////////////////
  55. TestFunction TestShapeRay = [inShape](const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)
  56. {
  57. // CastRay works relative to center of mass, so transform the ray
  58. RayCast ray = inRay;
  59. ray.mOrigin -= inShape->GetCenterOfMass();
  60. RayCastResult hit;
  61. SubShapeIDCreator id_creator;
  62. if (inExpectedFraction1 != FLT_MAX)
  63. {
  64. CHECK(inShape->CastRay(ray, id_creator, hit));
  65. CHECK_APPROX_EQUAL(hit.mFraction, inExpectedFraction1, 1.0e-5f);
  66. }
  67. else
  68. {
  69. CHECK_FALSE(inShape->CastRay(ray, id_creator, hit));
  70. }
  71. };
  72. // Test normal ray
  73. TestRayHelperInternal(inHitA, inHitB, TestShapeRay);
  74. // Test inverse ray
  75. TestRayHelperInternal(inHitB, inHitA, TestShapeRay);
  76. //////////////////////////////////////////////////////////////////////////////////////////////////
  77. // Test function that directly tests against a shape allowing multiple hits but no back facing hits, treating convex objects as solids
  78. //////////////////////////////////////////////////////////////////////////////////////////////////
  79. TestFunction TestShapeRayMultiHitIgnoreBackFace = [inShape](const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)
  80. {
  81. // CastRay works relative to center of mass, so transform the ray
  82. RayCast ray = inRay;
  83. ray.mOrigin -= inShape->GetCenterOfMass();
  84. // Ray cast settings
  85. RayCastSettings settings;
  86. settings.mBackFaceMode = EBackFaceMode::IgnoreBackFaces;
  87. settings.mTreatConvexAsSolid = true;
  88. AllHitCollisionCollector<CastRayCollector> collector;
  89. SubShapeIDCreator id_creator;
  90. inShape->CastRay(ray, settings, id_creator, collector);
  91. if (inExpectedFraction1 != FLT_MAX)
  92. {
  93. CHECK(collector.mHits.size() == 1);
  94. CHECK_APPROX_EQUAL(collector.mHits[0].mFraction, inExpectedFraction1, 1.0e-5f);
  95. }
  96. else
  97. {
  98. CHECK(collector.mHits.empty());
  99. }
  100. };
  101. // Test normal ray
  102. TestRayHelperInternal(inHitA, inHitB, TestShapeRayMultiHitIgnoreBackFace);
  103. // Test inverse ray
  104. TestRayHelperInternal(inHitB, inHitA, TestShapeRayMultiHitIgnoreBackFace);
  105. //////////////////////////////////////////////////////////////////////////////////////////////////
  106. // Test function that directly tests against a shape allowing multiple hits and back facing hits, treating convex objects as solids
  107. //////////////////////////////////////////////////////////////////////////////////////////////////
  108. TestFunction TestShapeRayMultiHitWithBackFace = [inShape](const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)
  109. {
  110. // CastRay works relative to center of mass, so transform the ray
  111. RayCast ray = inRay;
  112. ray.mOrigin -= inShape->GetCenterOfMass();
  113. // Ray cast settings
  114. RayCastSettings settings;
  115. settings.mBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  116. settings.mTreatConvexAsSolid = true;
  117. AllHitCollisionCollector<CastRayCollector> collector;
  118. SubShapeIDCreator id_creator;
  119. inShape->CastRay(ray, settings, id_creator, collector);
  120. if (inExpectedFraction1 != FLT_MAX)
  121. {
  122. CHECK(collector.mHits.size() >= 1);
  123. CHECK_APPROX_EQUAL(collector.mHits[0].mFraction, inExpectedFraction1, 1.0e-5f);
  124. }
  125. else
  126. {
  127. JPH_ASSERT(inExpectedFraction2 == FLT_MAX);
  128. CHECK(collector.mHits.empty());
  129. }
  130. if (inExpectedFraction2 != FLT_MAX)
  131. {
  132. CHECK(collector.mHits.size() >= 2);
  133. CHECK_APPROX_EQUAL(collector.mHits[1].mFraction, inExpectedFraction2, 1.0e-5f);
  134. }
  135. else
  136. {
  137. CHECK(collector.mHits.size() < 2);
  138. }
  139. };
  140. // Test normal ray
  141. TestRayHelperInternal(inHitA, inHitB, TestShapeRayMultiHitWithBackFace);
  142. // Test inverse ray
  143. TestRayHelperInternal(inHitB, inHitA, TestShapeRayMultiHitWithBackFace);
  144. //////////////////////////////////////////////////////////////////////////////////////////////////
  145. // Test function that directly tests against a shape allowing multiple hits but no back facing hits, treating convex object as non-solids
  146. //////////////////////////////////////////////////////////////////////////////////////////////////
  147. TestFunction TestShapeRayMultiHitIgnoreBackFaceNonSolid = [inShape](const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)
  148. {
  149. // CastRay works relative to center of mass, so transform the ray
  150. RayCast ray = inRay;
  151. ray.mOrigin -= inShape->GetCenterOfMass();
  152. // Ray cast settings
  153. RayCastSettings settings;
  154. settings.mBackFaceMode = EBackFaceMode::IgnoreBackFaces;
  155. settings.mTreatConvexAsSolid = false;
  156. AllHitCollisionCollector<CastRayCollector> collector;
  157. SubShapeIDCreator id_creator;
  158. inShape->CastRay(ray, settings, id_creator, collector);
  159. // A fraction of 0 means that the ray starts in solid, we treat this as a non-hit
  160. if (inExpectedFraction1 != 0.0f && inExpectedFraction1 != FLT_MAX)
  161. {
  162. CHECK(collector.mHits.size() == 1);
  163. CHECK_APPROX_EQUAL(collector.mHits[0].mFraction, inExpectedFraction1, 1.0e-5f);
  164. }
  165. else
  166. {
  167. CHECK(collector.mHits.empty());
  168. }
  169. };
  170. // Test normal ray
  171. TestRayHelperInternal(inHitA, inHitB, TestShapeRayMultiHitIgnoreBackFaceNonSolid);
  172. // Test inverse ray
  173. TestRayHelperInternal(inHitB, inHitA, TestShapeRayMultiHitIgnoreBackFaceNonSolid);
  174. //////////////////////////////////////////////////////////////////////////////////////////////////
  175. // Test function that directly tests against a shape allowing multiple hits and back facing hits, treating convex object as non-solids
  176. //////////////////////////////////////////////////////////////////////////////////////////////////
  177. TestFunction TestShapeRayMultiHitWithBackFaceNonSolid = [inShape](const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)
  178. {
  179. // CastRay works relative to center of mass, so transform the ray
  180. RayCast ray = inRay;
  181. ray.mOrigin -= inShape->GetCenterOfMass();
  182. // Ray cast settings
  183. RayCastSettings settings;
  184. settings.mBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  185. settings.mTreatConvexAsSolid = false;
  186. AllHitCollisionCollector<CastRayCollector> collector;
  187. SubShapeIDCreator id_creator;
  188. inShape->CastRay(ray, settings, id_creator, collector);
  189. // A fraction of 0 means that the ray starts in solid, we treat this as a non-hit
  190. if (inExpectedFraction1 == 0.0f)
  191. {
  192. inExpectedFraction1 = inExpectedFraction2;
  193. inExpectedFraction2 = FLT_MAX;
  194. }
  195. if (inExpectedFraction1 != FLT_MAX)
  196. {
  197. CHECK(collector.mHits.size() >= 1);
  198. CHECK_APPROX_EQUAL(collector.mHits[0].mFraction, inExpectedFraction1, 1.0e-5f);
  199. }
  200. else
  201. {
  202. JPH_ASSERT(inExpectedFraction2 == FLT_MAX);
  203. CHECK(collector.mHits.empty());
  204. }
  205. if (inExpectedFraction2 != FLT_MAX)
  206. {
  207. CHECK(collector.mHits.size() >= 2);
  208. CHECK_APPROX_EQUAL(collector.mHits[1].mFraction, inExpectedFraction2, 1.0e-5f);
  209. }
  210. else
  211. {
  212. CHECK(collector.mHits.size() < 2);
  213. }
  214. };
  215. // Test normal ray
  216. TestRayHelperInternal(inHitA, inHitB, TestShapeRayMultiHitWithBackFaceNonSolid);
  217. // Test inverse ray
  218. TestRayHelperInternal(inHitB, inHitA, TestShapeRayMultiHitWithBackFaceNonSolid);
  219. //////////////////////////////////////////////////////////////////////////////////////////////////
  220. // Insert the shape into the world
  221. //////////////////////////////////////////////////////////////////////////////////////////////////
  222. // A non-zero test position for the shape
  223. const Vec3 cShapePosition(2, 3, 4);
  224. const Quat cShapeRotation = Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI);
  225. const Mat44 cShapeMatrix = Mat44::sRotationTranslation(cShapeRotation, cShapePosition);
  226. // Make the shape part of a body and insert it into the physics system
  227. BPLayerInterfaceImpl broad_phase_layer_interface;
  228. ObjectVsBroadPhaseLayerFilter object_vs_broadphase_layer_filter;
  229. ObjectLayerPairFilter object_vs_object_layer_filter;
  230. PhysicsSystem system;
  231. system.Init(1, 0, 4, 4, broad_phase_layer_interface, object_vs_broadphase_layer_filter, object_vs_object_layer_filter);
  232. system.GetBodyInterface().CreateAndAddBody(BodyCreationSettings(inShape, RVec3(cShapePosition), cShapeRotation, EMotionType::Static, 0), EActivation::DontActivate);
  233. //////////////////////////////////////////////////////////////////////////////////////////////////
  234. // Test a ray against a shape through a physics system
  235. //////////////////////////////////////////////////////////////////////////////////////////////////
  236. TestFunction TestSystemRay = [&system, cShapeMatrix](const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)
  237. {
  238. // inRay is relative to shape, transform it into world space
  239. RayCast ray = inRay.Transformed(cShapeMatrix);
  240. RayCastResult hit;
  241. if (inExpectedFraction1 != FLT_MAX)
  242. {
  243. CHECK(system.GetNarrowPhaseQuery().CastRay(RRayCast(ray), hit));
  244. CHECK_APPROX_EQUAL(hit.mFraction, inExpectedFraction1, 2.0e-5f);
  245. }
  246. else
  247. {
  248. CHECK_FALSE(system.GetNarrowPhaseQuery().CastRay(RRayCast(ray), hit));
  249. }
  250. };
  251. // Test normal ray
  252. TestRayHelperInternal(inHitA, inHitB, TestSystemRay);
  253. // Test inverse ray
  254. TestRayHelperInternal(inHitB, inHitA, TestSystemRay);
  255. //////////////////////////////////////////////////////////////////////////////////////////////////
  256. // Test a ray against a shape through a physics system allowing multiple hits but no back facing hits
  257. //////////////////////////////////////////////////////////////////////////////////////////////////
  258. TestFunction TestSystemRayMultiHitIgnoreBackFace = [&system, cShapeMatrix](const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)
  259. {
  260. // inRay is relative to shape, transform it into world space
  261. RayCast ray = inRay.Transformed(cShapeMatrix);
  262. // Ray cast settings
  263. RayCastSettings settings;
  264. settings.mBackFaceMode = EBackFaceMode::IgnoreBackFaces;
  265. settings.mTreatConvexAsSolid = true;
  266. AllHitCollisionCollector<CastRayCollector> collector;
  267. system.GetNarrowPhaseQuery().CastRay(RRayCast(ray), settings, collector);
  268. if (inExpectedFraction1 != FLT_MAX)
  269. {
  270. CHECK(collector.mHits.size() == 1);
  271. CHECK_APPROX_EQUAL(collector.mHits[0].mFraction, inExpectedFraction1, 2.0e-5f);
  272. }
  273. else
  274. {
  275. CHECK(collector.mHits.empty());
  276. }
  277. };
  278. // Test normal ray
  279. TestRayHelperInternal(inHitA, inHitB, TestSystemRayMultiHitIgnoreBackFace);
  280. // Test inverse ray
  281. TestRayHelperInternal(inHitB, inHitA, TestSystemRayMultiHitIgnoreBackFace);
  282. //////////////////////////////////////////////////////////////////////////////////////////////////
  283. // Test a ray against a shape through a physics system allowing multiple hits and back facing hits
  284. //////////////////////////////////////////////////////////////////////////////////////////////////
  285. TestFunction TestSystemRayMultiHitWithBackFace = [&system, cShapeMatrix](const RayCast &inRay, float inExpectedFraction1, float inExpectedFraction2)
  286. {
  287. // inRay is relative to shape, transform it into world space
  288. RayCast ray = inRay.Transformed(cShapeMatrix);
  289. // Ray cast settings
  290. RayCastSettings settings;
  291. settings.mBackFaceMode = EBackFaceMode::CollideWithBackFaces;
  292. settings.mTreatConvexAsSolid = true;
  293. AllHitCollisionCollector<CastRayCollector> collector;
  294. system.GetNarrowPhaseQuery().CastRay(RRayCast(ray), settings, collector);
  295. collector.Sort();
  296. if (inExpectedFraction1 != FLT_MAX)
  297. {
  298. CHECK(collector.mHits.size() >= 1);
  299. CHECK_APPROX_EQUAL(collector.mHits[0].mFraction, inExpectedFraction1, 2.0e-5f);
  300. }
  301. else
  302. {
  303. JPH_ASSERT(inExpectedFraction2 == FLT_MAX);
  304. CHECK(collector.mHits.empty());
  305. }
  306. if (inExpectedFraction2 != FLT_MAX)
  307. {
  308. CHECK(collector.mHits.size() >= 2);
  309. CHECK_APPROX_EQUAL(collector.mHits[1].mFraction, inExpectedFraction2, 2.0e-5f);
  310. }
  311. else
  312. {
  313. CHECK(collector.mHits.size() < 2);
  314. }
  315. };
  316. // Test normal ray
  317. TestRayHelperInternal(inHitA, inHitB, TestSystemRayMultiHitWithBackFace);
  318. // Test inverse ray
  319. TestRayHelperInternal(inHitB, inHitA, TestSystemRayMultiHitWithBackFace);
  320. }
  321. /// Helper function to check that a ray misses a shape
  322. static void TestRayMiss(const Shape *inShape, Vec3Arg inOrigin, Vec3Arg inDirection)
  323. {
  324. RayCastResult hit;
  325. CHECK(!inShape->CastRay({ inOrigin - inShape->GetCenterOfMass(), inDirection }, SubShapeIDCreator(), hit));
  326. }
  327. TEST_CASE("TestBoxShapeRay")
  328. {
  329. // Create box shape
  330. BoxShape box(Vec3(2, 3, 4)); // Allocate on the stack to test embedded refcounted structs
  331. box.SetEmbedded();
  332. Ref<Shape> shape = &box; // Add a reference to see if we don't hit free() of a stack allocated struct
  333. TestRayHelper(shape, Vec3(-2, 0, 0), Vec3(2, 0, 0));
  334. TestRayHelper(shape, Vec3(0, -3, 0), Vec3(0, 3, 0));
  335. TestRayHelper(shape, Vec3(0, 0, -4), Vec3(0, 0, 4));
  336. }
  337. TEST_CASE("TestSphereShapeRay")
  338. {
  339. // Create sphere shape
  340. Ref<Shape> shape = new SphereShape(2);
  341. TestRayHelper(shape, Vec3(-2, 0, 0), Vec3(2, 0, 0));
  342. TestRayHelper(shape, Vec3(0, -2, 0), Vec3(0, 2, 0));
  343. TestRayHelper(shape, Vec3(0, 0, -2), Vec3(0, 0, 2));
  344. }
  345. TEST_CASE("TestConvexHullShapeRay")
  346. {
  347. // Create convex hull shape of a box (off center so the center of mass is not zero)
  348. Array<Vec3> box;
  349. box.push_back(Vec3(-2, -4, -6));
  350. box.push_back(Vec3(-2, -4, 7));
  351. box.push_back(Vec3(-2, 5, -6));
  352. box.push_back(Vec3(-2, 5, 7));
  353. box.push_back(Vec3(3, -4, -6));
  354. box.push_back(Vec3(3, -4, 7));
  355. box.push_back(Vec3(3, 5, -6));
  356. box.push_back(Vec3(3, 5, 7));
  357. RefConst<Shape> shape = ConvexHullShapeSettings(box).Create().Get();
  358. TestRayHelper(shape, Vec3(-2, 0, 0), Vec3(3, 0, 0));
  359. TestRayHelper(shape, Vec3(0, -4, 0), Vec3(0, 5, 0));
  360. TestRayHelper(shape, Vec3(0, 0, -6), Vec3(0, 0, 7));
  361. TestRayMiss(shape, Vec3(-3, -5, 0), Vec3(0, 1, 0));
  362. TestRayMiss(shape, Vec3(-3, 0, 0), Vec3(0, 1, 0));
  363. TestRayMiss(shape, Vec3(-3, 6, 0), Vec3(0, 1, 0));
  364. }
  365. TEST_CASE("TestCapsuleShapeRay")
  366. {
  367. // Create capsule shape
  368. Ref<Shape> shape = new CapsuleShape(4, 2);
  369. TestRayHelper(shape, Vec3(-2, 0, 0), Vec3(2, 0, 0));
  370. TestRayHelper(shape, Vec3(0, -6, 0), Vec3(0, 6, 0));
  371. TestRayHelper(shape, Vec3(0, 0, -2), Vec3(0, 0, 2));
  372. }
  373. TEST_CASE("TestTaperedCapsuleShapeRay")
  374. {
  375. // Create tapered capsule shape
  376. RefConst<Shape> shape = TaperedCapsuleShapeSettings(3, 4, 2).Create().Get();
  377. TestRayHelper(shape, Vec3(0, 7, 0), Vec3(0, -5, 0)); // Top to bottom
  378. TestRayHelper(shape, Vec3(-4, 3, 0), Vec3(4, 3, 0)); // Top sphere
  379. TestRayHelper(shape, Vec3(0, 3, -4), Vec3(0, 3, 4)); // Top sphere
  380. }
  381. TEST_CASE("TestCylinderShapeRay")
  382. {
  383. // Create cylinder shape
  384. Ref<Shape> shape = new CylinderShape(4, 2);
  385. TestRayHelper(shape, Vec3(-2, 0, 0), Vec3(2, 0, 0));
  386. TestRayHelper(shape, Vec3(0, -4, 0), Vec3(0, 4, 0));
  387. TestRayHelper(shape, Vec3(0, 0, -2), Vec3(0, 0, 2));
  388. }
  389. TEST_CASE("TestScaledShapeRay")
  390. {
  391. // Create convex hull shape of a box (off center so the center of mass is not zero)
  392. Array<Vec3> box;
  393. box.push_back(Vec3(-2, -4, -6));
  394. box.push_back(Vec3(-2, -4, 7));
  395. box.push_back(Vec3(-2, 5, -6));
  396. box.push_back(Vec3(-2, 5, 7));
  397. box.push_back(Vec3(3, -4, -6));
  398. box.push_back(Vec3(3, -4, 7));
  399. box.push_back(Vec3(3, 5, -6));
  400. box.push_back(Vec3(3, 5, 7));
  401. RefConst<Shape> hull = ConvexHullShapeSettings(box).Create().Get();
  402. // Scale the hull
  403. Ref<Shape> shape1 = new ScaledShape(hull, Vec3(2, 3, 4));
  404. TestRayHelper(shape1, Vec3(-4, 0, 0), Vec3(6, 0, 0));
  405. TestRayHelper(shape1, Vec3(0, -12, 0), Vec3(0, 15, 0));
  406. TestRayHelper(shape1, Vec3(0, 0, -24), Vec3(0, 0, 28));
  407. // Scale the hull (and flip it inside out)
  408. Ref<Shape> shape2 = new ScaledShape(hull, Vec3(-2, 3, 4));
  409. TestRayHelper(shape2, Vec3(-6, 0, 0), Vec3(4, 0, 0));
  410. TestRayHelper(shape2, Vec3(0, -12, 0), Vec3(0, 15, 0));
  411. TestRayHelper(shape2, Vec3(0, 0, -24), Vec3(0, 0, 28));
  412. }
  413. TEST_CASE("TestStaticCompoundShapeRay")
  414. {
  415. // Create convex hull shape of a box (off center so the center of mass is not zero)
  416. Array<Vec3> box;
  417. box.push_back(Vec3(-2, -4, -6));
  418. box.push_back(Vec3(-2, -4, 7));
  419. box.push_back(Vec3(-2, 5, -6));
  420. box.push_back(Vec3(-2, 5, 7));
  421. box.push_back(Vec3(3, -4, -6));
  422. box.push_back(Vec3(3, -4, 7));
  423. box.push_back(Vec3(3, 5, -6));
  424. box.push_back(Vec3(3, 5, 7));
  425. RefConst<ShapeSettings> hull = new ConvexHullShapeSettings(box);
  426. // Translate/rotate the shape through a compound (off center to force center of mass not zero)
  427. const Vec3 cShape1Position(10, 20, 30);
  428. const Quat cShape1Rotation = Quat::sRotation(Vec3::sAxisX(), 0.1f * JPH_PI) * Quat::sRotation(Vec3::sAxisY(), 0.2f * JPH_PI);
  429. const Vec3 cShape2Position(40, 50, 60);
  430. const Quat cShape2Rotation = Quat::sRotation(Vec3::sAxisZ(), 0.3f * JPH_PI);
  431. StaticCompoundShapeSettings compound_settings;
  432. compound_settings.AddShape(cShape1Position, cShape1Rotation, hull); // Shape 1
  433. compound_settings.AddShape(cShape2Position, cShape2Rotation, hull); // Shape 2
  434. RefConst<Shape> compound = compound_settings.Create().Get();
  435. // Hitting shape 1
  436. TestRayHelper(compound, cShape1Position + cShape1Rotation * Vec3(-2, 0, 0), cShape1Position + cShape1Rotation * Vec3(3, 0, 0));
  437. TestRayHelper(compound, cShape1Position + cShape1Rotation * Vec3(0, -4, 0), cShape1Position + cShape1Rotation * Vec3(0, 5, 0));
  438. TestRayHelper(compound, cShape1Position + cShape1Rotation * Vec3(0, 0, -6), cShape1Position + cShape1Rotation * Vec3(0, 0, 7));
  439. // Hitting shape 2
  440. TestRayHelper(compound, cShape2Position + cShape2Rotation * Vec3(-2, 0, 0), cShape2Position + cShape2Rotation * Vec3(3, 0, 0));
  441. TestRayHelper(compound, cShape2Position + cShape2Rotation * Vec3(0, -4, 0), cShape2Position + cShape2Rotation * Vec3(0, 5, 0));
  442. TestRayHelper(compound, cShape2Position + cShape2Rotation * Vec3(0, 0, -6), cShape2Position + cShape2Rotation * Vec3(0, 0, 7));
  443. }
  444. TEST_CASE("TestMutableCompoundShapeRay")
  445. {
  446. // Create convex hull shape of a box (off center so the center of mass is not zero)
  447. Array<Vec3> box;
  448. box.push_back(Vec3(-2, -4, -6));
  449. box.push_back(Vec3(-2, -4, 7));
  450. box.push_back(Vec3(-2, 5, -6));
  451. box.push_back(Vec3(-2, 5, 7));
  452. box.push_back(Vec3(3, -4, -6));
  453. box.push_back(Vec3(3, -4, 7));
  454. box.push_back(Vec3(3, 5, -6));
  455. box.push_back(Vec3(3, 5, 7));
  456. RefConst<ShapeSettings> hull = new ConvexHullShapeSettings(box);
  457. // Translate/rotate the shape through a compound (off center to force center of mass not zero)
  458. const Vec3 cShape1Position(10, 20, 30);
  459. const Quat cShape1Rotation = Quat::sRotation(Vec3::sAxisX(), 0.1f * JPH_PI) * Quat::sRotation(Vec3::sAxisY(), 0.2f * JPH_PI);
  460. const Vec3 cShape2Position(40, 50, 60);
  461. const Quat cShape2Rotation = Quat::sRotation(Vec3::sAxisZ(), 0.3f * JPH_PI);
  462. MutableCompoundShapeSettings compound_settings;
  463. compound_settings.AddShape(cShape1Position, cShape1Rotation, hull); // Shape 1
  464. compound_settings.AddShape(cShape2Position, cShape2Rotation, hull); // Shape 2
  465. RefConst<Shape> compound = compound_settings.Create().Get();
  466. // Hitting shape 1
  467. TestRayHelper(compound, cShape1Position + cShape1Rotation * Vec3(-2, 0, 0), cShape1Position + cShape1Rotation * Vec3(3, 0, 0));
  468. TestRayHelper(compound, cShape1Position + cShape1Rotation * Vec3(0, -4, 0), cShape1Position + cShape1Rotation * Vec3(0, 5, 0));
  469. TestRayHelper(compound, cShape1Position + cShape1Rotation * Vec3(0, 0, -6), cShape1Position + cShape1Rotation * Vec3(0, 0, 7));
  470. // Hitting shape 2
  471. TestRayHelper(compound, cShape2Position + cShape2Rotation * Vec3(-2, 0, 0), cShape2Position + cShape2Rotation * Vec3(3, 0, 0));
  472. TestRayHelper(compound, cShape2Position + cShape2Rotation * Vec3(0, -4, 0), cShape2Position + cShape2Rotation * Vec3(0, 5, 0));
  473. TestRayHelper(compound, cShape2Position + cShape2Rotation * Vec3(0, 0, -6), cShape2Position + cShape2Rotation * Vec3(0, 0, 7));
  474. }
  475. }