ShapeTests.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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/ConvexHullShape.h>
  7. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  8. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  9. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  10. #include <Jolt/Physics/Collision/Shape/TaperedCapsuleShape.h>
  11. #include <Jolt/Physics/Collision/Shape/CylinderShape.h>
  12. #include <Jolt/Physics/Collision/Shape/ScaledShape.h>
  13. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  14. #include <Jolt/Physics/Collision/Shape/MutableCompoundShape.h>
  15. #include <Jolt/Physics/Collision/Shape/TriangleShape.h>
  16. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  17. #include <Jolt/Physics/Collision/Shape/HeightFieldShape.h>
  18. #include <Jolt/Physics/Collision/Shape/MeshShape.h>
  19. #include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
  20. #include <Jolt/Physics/Collision/CollidePointResult.h>
  21. #include <Jolt/Physics/Collision/RayCast.h>
  22. #include <Jolt/Physics/Collision/CastResult.h>
  23. #include <Jolt/Core/StreamWrapper.h>
  24. TEST_SUITE("ShapeTests")
  25. {
  26. // Test convex hull shape
  27. TEST_CASE("TestConvexHullShape")
  28. {
  29. const float cDensity = 1.5f;
  30. // Create convex hull shape of a box
  31. Array<Vec3> box;
  32. box.push_back(Vec3(5, 6, 7));
  33. box.push_back(Vec3(5, 6, 14));
  34. box.push_back(Vec3(5, 12, 7));
  35. box.push_back(Vec3(5, 12, 14));
  36. box.push_back(Vec3(10, 6, 7));
  37. box.push_back(Vec3(10, 6, 14));
  38. box.push_back(Vec3(10, 12, 7));
  39. box.push_back(Vec3(10, 12, 14));
  40. ConvexHullShapeSettings settings(box);
  41. settings.SetDensity(cDensity);
  42. RefConst<Shape> shape = settings.Create().Get();
  43. // Validate calculated center of mass
  44. Vec3 com = shape->GetCenterOfMass();
  45. CHECK_APPROX_EQUAL(Vec3(7.5f, 9.0f, 10.5f), com, 1.0e-5f);
  46. // Calculate reference value of mass and inertia of a box
  47. MassProperties reference;
  48. reference.SetMassAndInertiaOfSolidBox(Vec3(5, 6, 7), cDensity);
  49. // Mass is easy to calculate, double check if SetMassAndInertiaOfSolidBox calculated it correctly
  50. CHECK_APPROX_EQUAL(5.0f * 6.0f * 7.0f * cDensity, reference.mMass, 1.0e-6f);
  51. // Get calculated inertia tensor
  52. MassProperties m = shape->GetMassProperties();
  53. CHECK_APPROX_EQUAL(reference.mMass, m.mMass, 1.0e-6f);
  54. CHECK_APPROX_EQUAL(reference.mInertia, m.mInertia, 1.0e-4f);
  55. // Check inner radius
  56. CHECK_APPROX_EQUAL(shape->GetInnerRadius(), 2.5f);
  57. }
  58. // Test inertia calculations for a capsule vs that of a convex hull of a capsule
  59. TEST_CASE("TestCapsuleVsConvexHullInertia")
  60. {
  61. const float half_height = 5.0f;
  62. const float radius = 3.0f;
  63. // Create a capsule
  64. CapsuleShape capsule(half_height, radius);
  65. capsule.SetDensity(7.0f);
  66. capsule.SetEmbedded();
  67. MassProperties mp_capsule = capsule.GetMassProperties();
  68. // Verify mass
  69. float mass_cylinder = 2.0f * half_height * JPH_PI * Square(radius) * capsule.GetDensity();
  70. float mass_sphere = 4.0f / 3.0f * JPH_PI * Cubed(radius) * capsule.GetDensity();
  71. CHECK_APPROX_EQUAL(mp_capsule.mMass, mass_cylinder + mass_sphere);
  72. // Extract support points
  73. ConvexShape::SupportBuffer buffer;
  74. const ConvexShape::Support *support = capsule.GetSupportFunction(ConvexShape::ESupportMode::IncludeConvexRadius, buffer, Vec3::sReplicate(1.0f));
  75. Array<Vec3> capsule_points;
  76. capsule_points.reserve(Vec3::sUnitSphere.size());
  77. for (const Vec3 &v : Vec3::sUnitSphere)
  78. capsule_points.push_back(support->GetSupport(v));
  79. // Create a convex hull using the support points
  80. ConvexHullShapeSettings capsule_hull(capsule_points);
  81. capsule_hull.SetDensity(capsule.GetDensity());
  82. RefConst<Shape> capsule_hull_shape = capsule_hull.Create().Get();
  83. MassProperties mp_capsule_hull = capsule_hull_shape->GetMassProperties();
  84. // Check that the mass and inertia of the convex hull match that of the capsule (within certain tolerance since the convex hull is an approximation)
  85. float mass_error = (mp_capsule_hull.mMass - mp_capsule.mMass) / mp_capsule.mMass;
  86. CHECK(mass_error > -0.05f);
  87. CHECK(mass_error < 0.0f); // Mass is smaller since the convex hull is smaller
  88. for (int i = 0; i < 3; ++i)
  89. for (int j = 0; j < 3; ++j)
  90. {
  91. if (i == j)
  92. {
  93. float inertia_error = (mp_capsule_hull.mInertia(i, j) - mp_capsule.mInertia(i, j)) / mp_capsule.mInertia(i, j);
  94. CHECK(inertia_error > -0.05f);
  95. CHECK(inertia_error < 0.0f); // Inertia is smaller since the convex hull is smaller
  96. }
  97. else
  98. {
  99. CHECK(mp_capsule.mInertia(i, j) == 0.0f);
  100. float scaled_inertia = mp_capsule_hull.mInertia(i, j) / mp_capsule_hull.mMass;
  101. CHECK_APPROX_EQUAL(scaled_inertia, 0.0f, 1.0e-3f);
  102. }
  103. }
  104. }
  105. // Test IsValidScale function
  106. TEST_CASE("TestIsValidScale")
  107. {
  108. // Test simple shapes
  109. Ref<Shape> sphere = new SphereShape(2.0f);
  110. CHECK(!sphere->IsValidScale(Vec3::sZero()));
  111. CHECK(sphere->IsValidScale(Vec3(2, 2, 2)));
  112. CHECK(sphere->IsValidScale(Vec3(-1, 1, -1)));
  113. CHECK(!sphere->IsValidScale(Vec3(2, 1, 1)));
  114. CHECK(!sphere->IsValidScale(Vec3(1, 2, 1)));
  115. CHECK(!sphere->IsValidScale(Vec3(1, 1, 2)));
  116. Ref<Shape> capsule = new CapsuleShape(2.0f, 0.5f);
  117. CHECK(!capsule->IsValidScale(Vec3::sZero()));
  118. CHECK(capsule->IsValidScale(Vec3(2, 2, 2)));
  119. CHECK(capsule->IsValidScale(Vec3(-1, 1, -1)));
  120. CHECK(!capsule->IsValidScale(Vec3(2, 1, 1)));
  121. CHECK(!capsule->IsValidScale(Vec3(1, 2, 1)));
  122. CHECK(!capsule->IsValidScale(Vec3(1, 1, 2)));
  123. Ref<Shape> tapered_capsule = TaperedCapsuleShapeSettings(2.0f, 0.5f, 0.7f).Create().Get();
  124. CHECK(!tapered_capsule->IsValidScale(Vec3::sZero()));
  125. CHECK(tapered_capsule->IsValidScale(Vec3(2, 2, 2)));
  126. CHECK(tapered_capsule->IsValidScale(Vec3(-1, 1, -1)));
  127. CHECK(!tapered_capsule->IsValidScale(Vec3(2, 1, 1)));
  128. CHECK(!tapered_capsule->IsValidScale(Vec3(1, 2, 1)));
  129. CHECK(!tapered_capsule->IsValidScale(Vec3(1, 1, 2)));
  130. Ref<Shape> cylinder = new CylinderShape(0.5f, 2.0f);
  131. CHECK(!cylinder->IsValidScale(Vec3::sZero()));
  132. CHECK(cylinder->IsValidScale(Vec3(2, 2, 2)));
  133. CHECK(cylinder->IsValidScale(Vec3(-1, 1, -1)));
  134. CHECK(!cylinder->IsValidScale(Vec3(2, 1, 1)));
  135. CHECK(cylinder->IsValidScale(Vec3(1, 2, 1)));
  136. CHECK(!cylinder->IsValidScale(Vec3(1, 1, 2)));
  137. Ref<Shape> triangle = new TriangleShape(Vec3(1, 2, 3), Vec3(4, 5, 6), Vec3(7, 8, 9));
  138. CHECK(!triangle->IsValidScale(Vec3::sZero()));
  139. CHECK(triangle->IsValidScale(Vec3(2, 2, 2)));
  140. CHECK(triangle->IsValidScale(Vec3(-1, 1, -1)));
  141. CHECK(triangle->IsValidScale(Vec3(2, 1, 1)));
  142. CHECK(triangle->IsValidScale(Vec3(1, 2, 1)));
  143. CHECK(triangle->IsValidScale(Vec3(1, 1, 2)));
  144. Ref<Shape> triangle2 = new TriangleShape(Vec3(1, 2, 3), Vec3(4, 5, 6), Vec3(7, 8, 9), 0.01f); // With convex radius
  145. CHECK(!triangle2->IsValidScale(Vec3::sZero()));
  146. CHECK(triangle2->IsValidScale(Vec3(2, 2, 2)));
  147. CHECK(triangle2->IsValidScale(Vec3(-1, 1, -1)));
  148. CHECK(!triangle2->IsValidScale(Vec3(2, 1, 1)));
  149. CHECK(!triangle2->IsValidScale(Vec3(1, 2, 1)));
  150. CHECK(!triangle2->IsValidScale(Vec3(1, 1, 2)));
  151. Ref<Shape> scaled = new ScaledShape(sphere, Vec3(1, 2, 1));
  152. CHECK(!scaled->IsValidScale(Vec3::sZero()));
  153. CHECK(!scaled->IsValidScale(Vec3(1, 1, 1)));
  154. CHECK(scaled->IsValidScale(Vec3(1, 0.5f, 1)));
  155. CHECK(scaled->IsValidScale(Vec3(-1, 0.5f, 1)));
  156. CHECK(!scaled->IsValidScale(Vec3(2, 1, 1)));
  157. CHECK(!scaled->IsValidScale(Vec3(1, 2, 1)));
  158. CHECK(!scaled->IsValidScale(Vec3(1, 1, 2)));
  159. Ref<Shape> scaled2 = new ScaledShape(scaled, Vec3(1, 0.5f, 1));
  160. CHECK(!scaled2->IsValidScale(Vec3::sZero()));
  161. CHECK(scaled2->IsValidScale(Vec3(2, 2, 2)));
  162. CHECK(scaled2->IsValidScale(Vec3(-1, 1, -1)));
  163. CHECK(!scaled2->IsValidScale(Vec3(2, 1, 1)));
  164. CHECK(!scaled2->IsValidScale(Vec3(1, 2, 1)));
  165. CHECK(!scaled2->IsValidScale(Vec3(1, 1, 2)));
  166. // Test a compound with shapes that can only be scaled uniformly
  167. StaticCompoundShapeSettings compound_settings;
  168. compound_settings.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisX(), 0.1f * JPH_PI), sphere);
  169. compound_settings.AddShape(Vec3(4, 5, 6), Quat::sRotation(Vec3::sAxisY(), 0.1f * JPH_PI), capsule);
  170. Ref<Shape> compound = compound_settings.Create().Get();
  171. CHECK(!compound->IsValidScale(Vec3::sZero()));
  172. CHECK(compound->IsValidScale(Vec3(1, 1, 1)));
  173. CHECK(compound->IsValidScale(Vec3(2, 2, 2)));
  174. CHECK(!compound->IsValidScale(Vec3(2, 1, 1)));
  175. CHECK(!compound->IsValidScale(Vec3(1, 2, 1)));
  176. CHECK(!compound->IsValidScale(Vec3(1, 1, 2)));
  177. // Test compound containing a triangle shape that can be scaled in any way
  178. StaticCompoundShapeSettings compound_settings2;
  179. compound_settings2.AddShape(Vec3(1, 2, 3), Quat::sIdentity(), triangle);
  180. compound_settings2.AddShape(Vec3(4, 5, 6), Quat::sIdentity(), new ScaledShape(triangle, Vec3(10, 11, 12)));
  181. Ref<Shape> compound2 = compound_settings2.Create().Get();
  182. CHECK(!compound2->IsValidScale(Vec3::sZero()));
  183. CHECK(compound2->IsValidScale(Vec3(1, 1, 1)));
  184. CHECK(compound2->IsValidScale(Vec3(2, 2, 2)));
  185. CHECK(compound2->IsValidScale(Vec3(2, 1, 1)));
  186. CHECK(compound2->IsValidScale(Vec3(1, 2, 1)));
  187. CHECK(compound2->IsValidScale(Vec3(1, 1, 2)));
  188. // Test rotations inside the compound of 90 degrees
  189. StaticCompoundShapeSettings compound_settings3;
  190. compound_settings3.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), triangle);
  191. compound_settings3.AddShape(Vec3(4, 5, 6), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new ScaledShape(triangle, Vec3(10, 11, 12)));
  192. Ref<Shape> compound3 = compound_settings3.Create().Get();
  193. CHECK(!compound3->IsValidScale(Vec3::sZero()));
  194. CHECK(compound3->IsValidScale(Vec3(1, 1, 1)));
  195. CHECK(compound3->IsValidScale(Vec3(2, 2, 2)));
  196. CHECK(compound3->IsValidScale(Vec3(2, 1, 1)));
  197. CHECK(compound3->IsValidScale(Vec3(1, 2, 1)));
  198. CHECK(compound3->IsValidScale(Vec3(1, 1, 2)));
  199. // Test non-90 degree rotations, this would cause shearing so is not allowed (we can't express that by passing a diagonal scale vector)
  200. StaticCompoundShapeSettings compound_settings4;
  201. compound_settings4.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), triangle);
  202. compound_settings4.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.25f * JPH_PI), triangle);
  203. Ref<Shape> compound4 = compound_settings4.Create().Get();
  204. CHECK(!compound4->IsValidScale(Vec3::sZero()));
  205. CHECK(compound4->IsValidScale(Vec3(1, 1, 1)));
  206. CHECK(compound4->IsValidScale(Vec3(2, 2, 2)));
  207. CHECK(!compound4->IsValidScale(Vec3(2, 1, 1)));
  208. CHECK(!compound4->IsValidScale(Vec3(1, 2, 1)));
  209. CHECK(compound4->IsValidScale(Vec3(1, 1, 2))); // We're rotation around Z, so non-uniform in the Z direction is ok
  210. // Test a mutable compound with shapes that can only be scaled uniformly
  211. MutableCompoundShapeSettings mutable_compound_settings;
  212. mutable_compound_settings.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisX(), 0.1f * JPH_PI), sphere);
  213. mutable_compound_settings.AddShape(Vec3(4, 5, 6), Quat::sRotation(Vec3::sAxisY(), 0.1f * JPH_PI), capsule);
  214. Ref<Shape> mutable_compound = mutable_compound_settings.Create().Get();
  215. CHECK(!mutable_compound->IsValidScale(Vec3::sZero()));
  216. CHECK(mutable_compound->IsValidScale(Vec3(1, 1, 1)));
  217. CHECK(mutable_compound->IsValidScale(Vec3(2, 2, 2)));
  218. CHECK(!mutable_compound->IsValidScale(Vec3(2, 1, 1)));
  219. CHECK(!mutable_compound->IsValidScale(Vec3(1, 2, 1)));
  220. CHECK(!mutable_compound->IsValidScale(Vec3(1, 1, 2)));
  221. // Test mutable compound containing a triangle shape that can be scaled in any way
  222. MutableCompoundShapeSettings mutable_compound_settings2;
  223. mutable_compound_settings2.AddShape(Vec3(1, 2, 3), Quat::sIdentity(), triangle);
  224. mutable_compound_settings2.AddShape(Vec3(4, 5, 6), Quat::sIdentity(), new ScaledShape(triangle, Vec3(10, 11, 12)));
  225. Ref<Shape> mutable_compound2 = mutable_compound_settings2.Create().Get();
  226. CHECK(!mutable_compound2->IsValidScale(Vec3::sZero()));
  227. CHECK(mutable_compound2->IsValidScale(Vec3(1, 1, 1)));
  228. CHECK(mutable_compound2->IsValidScale(Vec3(2, 2, 2)));
  229. CHECK(mutable_compound2->IsValidScale(Vec3(2, 1, 1)));
  230. CHECK(mutable_compound2->IsValidScale(Vec3(1, 2, 1)));
  231. CHECK(mutable_compound2->IsValidScale(Vec3(1, 1, 2)));
  232. // Test rotations inside the mutable compound of 90 degrees
  233. MutableCompoundShapeSettings mutable_compound_settings3;
  234. mutable_compound_settings3.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), triangle);
  235. mutable_compound_settings3.AddShape(Vec3(4, 5, 6), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new ScaledShape(triangle, Vec3(10, 11, 12)));
  236. Ref<Shape> mutable_compound3 = mutable_compound_settings3.Create().Get();
  237. CHECK(!mutable_compound3->IsValidScale(Vec3::sZero()));
  238. CHECK(mutable_compound3->IsValidScale(Vec3(1, 1, 1)));
  239. CHECK(mutable_compound3->IsValidScale(Vec3(2, 2, 2)));
  240. CHECK(mutable_compound3->IsValidScale(Vec3(2, 1, 1)));
  241. CHECK(mutable_compound3->IsValidScale(Vec3(1, 2, 1)));
  242. CHECK(mutable_compound3->IsValidScale(Vec3(1, 1, 2)));
  243. // Test non-90 degree rotations, this would cause shearing so is not allowed (we can't express that by passing a diagonal scale vector)
  244. MutableCompoundShapeSettings mutable_compound_settings4;
  245. mutable_compound_settings4.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), triangle);
  246. mutable_compound_settings4.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.25f * JPH_PI), triangle);
  247. Ref<Shape> mutable_compound4 = mutable_compound_settings4.Create().Get();
  248. CHECK(!mutable_compound4->IsValidScale(Vec3::sZero()));
  249. CHECK(mutable_compound4->IsValidScale(Vec3(1, 1, 1)));
  250. CHECK(mutable_compound4->IsValidScale(Vec3(2, 2, 2)));
  251. CHECK(!mutable_compound4->IsValidScale(Vec3(2, 1, 1)));
  252. CHECK(!mutable_compound4->IsValidScale(Vec3(1, 2, 1)));
  253. CHECK(mutable_compound4->IsValidScale(Vec3(1, 1, 2))); // We're rotation around Z, so non-uniform in the Z direction is ok
  254. // Test a rotated translated shape that can only be scaled uniformly
  255. RotatedTranslatedShapeSettings rt_settings(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisX(), 0.1f * JPH_PI), sphere);
  256. Ref<Shape> rt_shape = rt_settings.Create().Get();
  257. CHECK(!rt_shape->IsValidScale(Vec3::sZero()));
  258. CHECK(rt_shape->IsValidScale(Vec3(1, 1, 1)));
  259. CHECK(rt_shape->IsValidScale(Vec3(2, 2, 2)));
  260. CHECK(!rt_shape->IsValidScale(Vec3(2, 1, 1)));
  261. CHECK(!rt_shape->IsValidScale(Vec3(1, 2, 1)));
  262. CHECK(!rt_shape->IsValidScale(Vec3(1, 1, 2)));
  263. // Test rotated translated shape containing a triangle shape that can be scaled in any way
  264. RotatedTranslatedShapeSettings rt_settings2(Vec3(4, 5, 6), Quat::sIdentity(), new ScaledShape(triangle, Vec3(10, 11, 12)));
  265. Ref<Shape> rt_shape2 = rt_settings2.Create().Get();
  266. CHECK(!rt_shape2->IsValidScale(Vec3::sZero()));
  267. CHECK(rt_shape2->IsValidScale(Vec3(1, 1, 1)));
  268. CHECK(rt_shape2->IsValidScale(Vec3(2, 2, 2)));
  269. CHECK(rt_shape2->IsValidScale(Vec3(2, 1, 1)));
  270. CHECK(rt_shape2->IsValidScale(Vec3(1, 2, 1)));
  271. CHECK(rt_shape2->IsValidScale(Vec3(1, 1, 2)));
  272. // Test rotations inside the rotated translated of 90 degrees
  273. RotatedTranslatedShapeSettings rt_settings3(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), triangle);
  274. Ref<Shape> rt_shape3 = rt_settings3.Create().Get();
  275. CHECK(!rt_shape3->IsValidScale(Vec3::sZero()));
  276. CHECK(rt_shape3->IsValidScale(Vec3(1, 1, 1)));
  277. CHECK(rt_shape3->IsValidScale(Vec3(2, 2, 2)));
  278. CHECK(rt_shape3->IsValidScale(Vec3(2, 1, 1)));
  279. CHECK(rt_shape3->IsValidScale(Vec3(1, 2, 1)));
  280. CHECK(rt_shape3->IsValidScale(Vec3(1, 1, 2)));
  281. // Test non-90 degree rotations, this would cause shearing so is not allowed (we can't express that by passing a diagonal scale vector)
  282. RotatedTranslatedShapeSettings rt_settings4(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), triangle);
  283. Ref<Shape> rt_shape4 = rt_settings4.Create().Get();
  284. CHECK(!rt_shape4->IsValidScale(Vec3::sZero()));
  285. CHECK(rt_shape4->IsValidScale(Vec3(1, 1, 1)));
  286. CHECK(rt_shape4->IsValidScale(Vec3(2, 2, 2)));
  287. CHECK(!rt_shape4->IsValidScale(Vec3(2, 1, 1)));
  288. CHECK(!rt_shape4->IsValidScale(Vec3(1, 2, 1)));
  289. CHECK(rt_shape4->IsValidScale(Vec3(1, 1, 2))); // We're rotation around Z, so non-uniform in the Z direction is ok
  290. }
  291. // Test embedded shape
  292. TEST_CASE("TestEmbeddedShape")
  293. {
  294. {
  295. // Test shape constructed on stack, where shape construction succeeds
  296. ConvexHullShapeSettings settings;
  297. settings.mPoints.push_back(Vec3(0, 0, 0));
  298. settings.mPoints.push_back(Vec3(1, 0, 0));
  299. settings.mPoints.push_back(Vec3(0, 1, 0));
  300. settings.mPoints.push_back(Vec3(0, 0, 1));
  301. Shape::ShapeResult result;
  302. ConvexHullShape shape(settings, result);
  303. shape.SetEmbedded();
  304. CHECK(result.IsValid());
  305. result.Clear(); // Release the reference from the result
  306. // Test CollidePoint for this shape
  307. AllHitCollisionCollector<CollidePointCollector> collector;
  308. shape.CollidePoint(Vec3::sReplicate(-0.1f) - shape.GetCenterOfMass(), SubShapeIDCreator(), collector);
  309. CHECK(collector.mHits.empty());
  310. shape.CollidePoint(Vec3::sReplicate(0.1f) - shape.GetCenterOfMass(), SubShapeIDCreator(), collector);
  311. CHECK(collector.mHits.size() == 1);
  312. }
  313. {
  314. // Test shape constructed on stack, where shape construction fails
  315. ConvexHullShapeSettings settings;
  316. Shape::ShapeResult result;
  317. ConvexHullShape shape(settings, result);
  318. shape.SetEmbedded();
  319. CHECK(!result.IsValid());
  320. }
  321. }
  322. // Test submerged volume calculation
  323. TEST_CASE("TestGetSubmergedVolume")
  324. {
  325. Ref<BoxShape> box = new BoxShape(Vec3(1, 2, 3));
  326. Vec3 scale(2, -3, 4);
  327. Mat44 translation = Mat44::sTranslation(Vec3(0, 6, 0)); // Translate so we're on the y = 0 plane
  328. // Plane pointing positive Y
  329. // Entirely above the plane
  330. {
  331. float total_volume, submerged_volume;
  332. Vec3 center_of_buoyancy;
  333. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, -0.001f, 0), Vec3::sAxisY()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  334. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  335. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  336. }
  337. // Entirely below the plane
  338. {
  339. float total_volume, submerged_volume;
  340. Vec3 center_of_buoyancy;
  341. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 12.001f, 0), Vec3::sAxisY()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  342. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  343. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  344. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  345. }
  346. // Halfway through
  347. {
  348. float total_volume, submerged_volume;
  349. Vec3 center_of_buoyancy;
  350. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 6.0f, 0), Vec3::sAxisY()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  351. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  352. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 6.0f * 24.0f);
  353. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 3, 0));
  354. }
  355. // Plane pointing negative Y
  356. // Entirely above the plane
  357. {
  358. float total_volume, submerged_volume;
  359. Vec3 center_of_buoyancy;
  360. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(-4, 12.001f, 0), -Vec3::sAxisY()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  361. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  362. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  363. }
  364. // Entirely below the plane
  365. {
  366. float total_volume, submerged_volume;
  367. Vec3 center_of_buoyancy;
  368. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, -0.001f, 0), -Vec3::sAxisY()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  369. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  370. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  371. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  372. }
  373. // Halfway through
  374. {
  375. float total_volume, submerged_volume;
  376. Vec3 center_of_buoyancy;
  377. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 6.0f, 0), -Vec3::sAxisY()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  378. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  379. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 6.0f * 24.0f);
  380. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 9, 0));
  381. }
  382. // Plane pointing positive X
  383. // Entirely above the plane
  384. {
  385. float total_volume, submerged_volume;
  386. Vec3 center_of_buoyancy;
  387. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(-2.001f, 0, 0), Vec3::sAxisX()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  388. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  389. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  390. }
  391. // Entirely below the plane
  392. {
  393. float total_volume, submerged_volume;
  394. Vec3 center_of_buoyancy;
  395. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(2.001f, 0, 0), Vec3::sAxisX()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  396. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  397. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  398. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  399. }
  400. // Halfway through
  401. {
  402. float total_volume, submerged_volume;
  403. Vec3 center_of_buoyancy;
  404. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 0, 0), Vec3::sAxisX()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  405. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  406. CHECK_APPROX_EQUAL(submerged_volume, 2.0f * 12.0f * 24.0f);
  407. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(-1, 6, 0));
  408. }
  409. // Plane pointing negative X
  410. // Entirely above the plane
  411. {
  412. float total_volume, submerged_volume;
  413. Vec3 center_of_buoyancy;
  414. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(2.001f, 0, 0), -Vec3::sAxisX()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  415. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  416. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  417. }
  418. // Entirely below the plane
  419. {
  420. float total_volume, submerged_volume;
  421. Vec3 center_of_buoyancy;
  422. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(-2.001f, 0, 0), -Vec3::sAxisX()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  423. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  424. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  425. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  426. }
  427. // Halfway through
  428. {
  429. float total_volume, submerged_volume;
  430. Vec3 center_of_buoyancy;
  431. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 0, 0), -Vec3::sAxisX()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  432. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  433. CHECK_APPROX_EQUAL(submerged_volume, 2.0f * 12.0f * 24.0f);
  434. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(1, 6, 0));
  435. }
  436. // Plane pointing positive Z
  437. // Entirely above the plane
  438. {
  439. float total_volume, submerged_volume;
  440. Vec3 center_of_buoyancy;
  441. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 0, -12.001f), Vec3::sAxisZ()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  442. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  443. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  444. }
  445. // Entirely below the plane
  446. {
  447. float total_volume, submerged_volume;
  448. Vec3 center_of_buoyancy;
  449. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 0, 12.001f), Vec3::sAxisZ()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  450. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  451. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  452. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  453. }
  454. // Halfway through
  455. {
  456. float total_volume, submerged_volume;
  457. Vec3 center_of_buoyancy;
  458. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 0, 0), Vec3::sAxisZ()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  459. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  460. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 12.0f);
  461. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, -6));
  462. }
  463. // Plane pointing negative Z
  464. // Entirely above the plane
  465. {
  466. float total_volume, submerged_volume;
  467. Vec3 center_of_buoyancy;
  468. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 0, 12.001f), -Vec3::sAxisZ()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  469. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  470. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  471. }
  472. // Entirely below the plane
  473. {
  474. float total_volume, submerged_volume;
  475. Vec3 center_of_buoyancy;
  476. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 0, -12.001f), -Vec3::sAxisZ()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  477. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  478. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  479. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  480. }
  481. // Halfway through
  482. {
  483. float total_volume, submerged_volume;
  484. Vec3 center_of_buoyancy;
  485. box->GetSubmergedVolume(translation, scale, Plane::sFromPointAndNormal(Vec3(0, 0, 0), -Vec3::sAxisZ()), total_volume, submerged_volume, center_of_buoyancy JPH_IF_DEBUG_RENDERER(, RVec3::sZero()));
  486. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  487. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 12.0f);
  488. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 6));
  489. }
  490. }
  491. // Test setting user data on shapes
  492. TEST_CASE("TestShapeUserData")
  493. {
  494. const float cRadius = 2.0f;
  495. // Create a sphere with user data
  496. SphereShapeSettings sphere_settings(cRadius);
  497. sphere_settings.mUserData = 0x1234567887654321;
  498. Ref<Shape> sphere = sphere_settings.Create().Get();
  499. CHECK(sphere->GetUserData() == 0x1234567887654321);
  500. // Change the user data
  501. sphere->SetUserData(0x5678123443218765);
  502. CHECK(sphere->GetUserData() == 0x5678123443218765);
  503. stringstream data;
  504. // Write sphere to a binary stream
  505. {
  506. StreamOutWrapper stream_out(data);
  507. sphere->SaveBinaryState(stream_out);
  508. }
  509. // Destroy the sphere
  510. sphere = nullptr;
  511. // Read sphere from binary stream
  512. {
  513. StreamInWrapper stream_in(data);
  514. sphere = Shape::sRestoreFromBinaryState(stream_in).Get();
  515. }
  516. // Check that the sphere and its user data was preserved
  517. CHECK(sphere->GetType() == EShapeType::Convex);
  518. CHECK(sphere->GetSubType() == EShapeSubType::Sphere);
  519. CHECK(sphere->GetUserData() == 0x5678123443218765);
  520. CHECK(static_cast<SphereShape *>(sphere.GetPtr())->GetRadius() == cRadius);
  521. }
  522. // Test setting user data on shapes
  523. TEST_CASE("TestIsValidSubShapeID")
  524. {
  525. MutableCompoundShapeSettings shape1_settings;
  526. RefConst<CompoundShape> shape1 = static_cast<const CompoundShape *>(shape1_settings.Create().Get().GetPtr());
  527. MutableCompoundShapeSettings shape2_settings;
  528. shape2_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), new SphereShape(1.0f));
  529. shape2_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), new SphereShape(1.0f));
  530. shape2_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), new SphereShape(1.0f));
  531. RefConst<CompoundShape> shape2 = static_cast<const CompoundShape *>(shape2_settings.Create().Get().GetPtr());
  532. // Get sub shape IDs of shape 2 and test if they're valid
  533. SubShapeID sub_shape1 = shape2->GetSubShapeIDFromIndex(0, SubShapeIDCreator()).GetID();
  534. CHECK(shape2->IsSubShapeIDValid(sub_shape1));
  535. SubShapeID sub_shape2 = shape2->GetSubShapeIDFromIndex(1, SubShapeIDCreator()).GetID();
  536. CHECK(shape2->IsSubShapeIDValid(sub_shape2));
  537. SubShapeID sub_shape3 = shape2->GetSubShapeIDFromIndex(2, SubShapeIDCreator()).GetID();
  538. CHECK(shape2->IsSubShapeIDValid(sub_shape3));
  539. SubShapeID sub_shape4 = shape2->GetSubShapeIDFromIndex(3, SubShapeIDCreator()).GetID(); // This one doesn't exist
  540. CHECK(!shape2->IsSubShapeIDValid(sub_shape4));
  541. // Shape 1 has no parts so these sub shape ID's should not be valid
  542. CHECK(!shape1->IsSubShapeIDValid(sub_shape1));
  543. CHECK(!shape1->IsSubShapeIDValid(sub_shape2));
  544. CHECK(!shape1->IsSubShapeIDValid(sub_shape3));
  545. CHECK(!shape1->IsSubShapeIDValid(sub_shape4));
  546. }
  547. // Test that an error is reported when we run out of sub shape bits
  548. TEST_CASE("TestOutOfSubShapeIDBits")
  549. {
  550. static constexpr uint32 cHeightFieldSamples = 1024;
  551. static constexpr int cNumBitsPerCompound = 4;
  552. // Create a heightfield
  553. float *samples = new float [cHeightFieldSamples * cHeightFieldSamples];
  554. memset(samples, 0, cHeightFieldSamples * cHeightFieldSamples * sizeof(float));
  555. RefConst<Shape> previous_shape = HeightFieldShapeSettings(samples, Vec3::sZero(), Vec3::sReplicate(1.0f), cHeightFieldSamples).Create().Get();
  556. delete [] samples;
  557. // Calculate the amount of bits needed to address all triangles in the heightfield
  558. uint num_bits = 32 - CountLeadingZeros((cHeightFieldSamples - 1) * (cHeightFieldSamples - 1) * 2);
  559. for (;;)
  560. {
  561. // Check that the total sub shape ID bits up to this point is correct
  562. CHECK(previous_shape->GetSubShapeIDBitsRecursive() == num_bits);
  563. // Create a compound with a number of sub shapes
  564. StaticCompoundShapeSettings compound_settings;
  565. compound_settings.SetEmbedded();
  566. for (int i = 0; i < (1 << cNumBitsPerCompound) ; ++i)
  567. compound_settings.AddShape(Vec3((float)i, 0, 0), Quat::sIdentity(), previous_shape);
  568. Shape::ShapeResult result = compound_settings.Create();
  569. num_bits += cNumBitsPerCompound;
  570. if (num_bits < SubShapeID::MaxBits)
  571. {
  572. // Creation should have succeeded
  573. CHECK(result.IsValid());
  574. previous_shape = result.Get();
  575. }
  576. else
  577. {
  578. // Creation should have failed because we ran out of bits
  579. CHECK(!result.IsValid());
  580. break;
  581. }
  582. }
  583. }
  584. TEST_CASE("TestEmptyMutableCompound")
  585. {
  586. // Create empty shape
  587. RefConst<Shape> mutable_compound = new MutableCompoundShape();
  588. // A non-identity rotation
  589. Quat rotation = Quat::sRotation(Vec3::sReplicate(1.0f / sqrt(3.0f)), 0.1f * JPH_PI);
  590. // Check that local bounding box is invalid
  591. AABox bounds1 = mutable_compound->GetLocalBounds();
  592. CHECK(!bounds1.IsValid());
  593. // Check that get world space bounds returns an invalid bounding box
  594. AABox bounds2 = mutable_compound->GetWorldSpaceBounds(Mat44::sRotationTranslation(rotation, Vec3(100, 200, 300)), Vec3(1, 2, 3));
  595. CHECK(!bounds2.IsValid());
  596. // Check that get world space bounds returns an invalid bounding box for double precision parameters
  597. AABox bounds3 = mutable_compound->GetWorldSpaceBounds(DMat44::sRotationTranslation(rotation, DVec3(100, 200, 300)), Vec3(1, 2, 3));
  598. CHECK(!bounds3.IsValid());
  599. }
  600. TEST_CASE("TestSaveMeshShape")
  601. {
  602. // Create an n x n grid of triangles
  603. const int n = 10;
  604. const float s = 0.1f;
  605. TriangleList triangles;
  606. for (int z = 0; z < n; ++z)
  607. for (int x = 0; x < n; ++x)
  608. {
  609. float fx = s * x - s * n / 2, fz = s * z - s * n / 2;
  610. triangles.push_back(Triangle(Vec3(fx, 0, fz), Vec3(fx, 0, fz + s), Vec3(fx + s, 0, fz + s)));
  611. triangles.push_back(Triangle(Vec3(fx, 0, fz), Vec3(fx + s, 0, fz + s), Vec3(fx + s, 0, fz)));
  612. }
  613. MeshShapeSettings mesh_settings(triangles);
  614. mesh_settings.SetEmbedded();
  615. RefConst<Shape> shape = mesh_settings.Create().Get();
  616. // Calculate expected bounds
  617. AABox expected_bounds;
  618. for (const Triangle &t : triangles)
  619. for (const Float3 &v : t.mV)
  620. expected_bounds.Encapsulate(Vec3(v));
  621. stringstream stream;
  622. {
  623. // Write mesh to stream
  624. StreamOutWrapper wrapper(stream);
  625. shape->SaveBinaryState(wrapper);
  626. }
  627. {
  628. // Read back mesh
  629. StreamInWrapper iwrapper(stream);
  630. Shape::ShapeResult result = Shape::sRestoreFromBinaryState(iwrapper);
  631. CHECK(result.IsValid());
  632. RefConst<MeshShape> mesh_shape = static_cast<const MeshShape *>(result.Get().GetPtr());
  633. // Test if it contains the same amount of triangles
  634. Shape::Stats stats = mesh_shape->GetStats();
  635. CHECK(stats.mNumTriangles == triangles.size());
  636. // Check bounding box
  637. CHECK(mesh_shape->GetLocalBounds() == expected_bounds);
  638. // Check if we can hit it with a ray
  639. RayCastResult hit;
  640. RayCast ray(Vec3(0.5f * s, 1, 0.25f * s), Vec3(0, -2, 0)); // Hit in the center of a triangle
  641. CHECK(mesh_shape->CastRay(ray, SubShapeIDCreator(), hit));
  642. CHECK(hit.mFraction == 0.5f);
  643. CHECK(mesh_shape->GetSurfaceNormal(hit.mSubShapeID2, ray.GetPointOnRay(hit.mFraction)) == Vec3::sAxisY());
  644. }
  645. }
  646. }