ShapeTests.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. constexpr float cMinScaleToleranceSq = Square(1.0e-6f * ScaleHelpers::cMinScale);
  109. // Test simple shapes
  110. Ref<Shape> sphere = new SphereShape(2.0f);
  111. CHECK(!sphere->IsValidScale(Vec3::sZero()));
  112. CHECK(sphere->IsValidScale(Vec3(2, 2, 2)));
  113. CHECK(sphere->IsValidScale(Vec3(-1, 1, -1)));
  114. CHECK(!sphere->IsValidScale(Vec3(2, 1, 1)));
  115. CHECK(!sphere->IsValidScale(Vec3(1, 2, 1)));
  116. CHECK(!sphere->IsValidScale(Vec3(1, 1, 2)));
  117. CHECK(sphere->MakeScaleValid(Vec3::sZero()).IsClose(Vec3::sReplicate(ScaleHelpers::cMinScale), cMinScaleToleranceSq)); // Averaging can cause a slight error
  118. CHECK(sphere->MakeScaleValid(Vec3(-2, 3, 4)) == Vec3(-3, 3, 3));
  119. Ref<Shape> capsule = new CapsuleShape(2.0f, 0.5f);
  120. CHECK(!capsule->IsValidScale(Vec3::sZero()));
  121. CHECK(!capsule->IsValidScale(Vec3(0, 1, 0)));
  122. CHECK(!capsule->IsValidScale(Vec3(1, 0, 1)));
  123. CHECK(capsule->IsValidScale(Vec3(2, 2, 2)));
  124. CHECK(capsule->IsValidScale(Vec3(-1, 1, -1)));
  125. CHECK(!capsule->IsValidScale(Vec3(2, 1, 1)));
  126. CHECK(!capsule->IsValidScale(Vec3(1, 2, 1)));
  127. CHECK(!capsule->IsValidScale(Vec3(1, 1, 2)));
  128. CHECK(capsule->MakeScaleValid(Vec3::sZero()).IsClose(Vec3::sReplicate(ScaleHelpers::cMinScale), cMinScaleToleranceSq));
  129. CHECK(capsule->MakeScaleValid(Vec3(-2, 3, 4)) == Vec3(-3, 3, 3));
  130. Ref<Shape> tapered_capsule = TaperedCapsuleShapeSettings(2.0f, 0.5f, 0.7f).Create().Get();
  131. CHECK(!tapered_capsule->IsValidScale(Vec3::sZero()));
  132. CHECK(tapered_capsule->IsValidScale(Vec3(2, 2, 2)));
  133. CHECK(tapered_capsule->IsValidScale(Vec3(-1, 1, -1)));
  134. CHECK(!tapered_capsule->IsValidScale(Vec3(2, 1, 1)));
  135. CHECK(!tapered_capsule->IsValidScale(Vec3(1, 2, 1)));
  136. CHECK(!tapered_capsule->IsValidScale(Vec3(1, 1, 2)));
  137. CHECK(tapered_capsule->MakeScaleValid(Vec3::sZero()).IsClose(Vec3::sReplicate(ScaleHelpers::cMinScale), cMinScaleToleranceSq));
  138. CHECK(tapered_capsule->MakeScaleValid(Vec3(2, -3, 4)) == Vec3(3, -3, 3));
  139. Ref<Shape> cylinder = new CylinderShape(0.5f, 2.0f);
  140. CHECK(!cylinder->IsValidScale(Vec3::sZero()));
  141. CHECK(!cylinder->IsValidScale(Vec3(0, 1, 0)));
  142. CHECK(!cylinder->IsValidScale(Vec3(1, 0, 1)));
  143. CHECK(cylinder->IsValidScale(Vec3(2, 2, 2)));
  144. CHECK(cylinder->IsValidScale(Vec3(-1, 1, -1)));
  145. CHECK(!cylinder->IsValidScale(Vec3(2, 1, 1)));
  146. CHECK(cylinder->IsValidScale(Vec3(1, 2, 1)));
  147. CHECK(!cylinder->IsValidScale(Vec3(1, 1, 2)));
  148. CHECK(cylinder->MakeScaleValid(Vec3::sZero()).IsClose(Vec3::sReplicate(ScaleHelpers::cMinScale), cMinScaleToleranceSq));
  149. CHECK(cylinder->MakeScaleValid(Vec3(-1.0e-10f, 1, 1.0e-10f)) == Vec3(-ScaleHelpers::cMinScale, 1, ScaleHelpers::cMinScale));
  150. CHECK(cylinder->MakeScaleValid(Vec3(2, 5, -4)) == Vec3(3, 5, -3));
  151. Ref<Shape> triangle = new TriangleShape(Vec3(1, 2, 3), Vec3(4, 5, 6), Vec3(7, 8, 9));
  152. CHECK(!triangle->IsValidScale(Vec3::sZero()));
  153. CHECK(!triangle->IsValidScale(Vec3::sAxisX()));
  154. CHECK(!triangle->IsValidScale(Vec3::sAxisY()));
  155. CHECK(!triangle->IsValidScale(Vec3::sAxisZ()));
  156. CHECK(triangle->IsValidScale(Vec3(2, 2, 2)));
  157. CHECK(triangle->IsValidScale(Vec3(-1, 1, -1)));
  158. CHECK(triangle->IsValidScale(Vec3(2, 1, 1)));
  159. CHECK(triangle->IsValidScale(Vec3(1, 2, 1)));
  160. CHECK(triangle->IsValidScale(Vec3(1, 1, 2)));
  161. CHECK(triangle->MakeScaleValid(Vec3::sZero()).IsClose(Vec3::sReplicate(ScaleHelpers::cMinScale), cMinScaleToleranceSq));
  162. CHECK(triangle->MakeScaleValid(Vec3(2, 5, -4)) == Vec3(2, 5, -4));
  163. Ref<Shape> triangle2 = new TriangleShape(Vec3(1, 2, 3), Vec3(4, 5, 6), Vec3(7, 8, 9), 0.01f); // With convex radius
  164. CHECK(!triangle2->IsValidScale(Vec3::sZero()));
  165. CHECK(!triangle2->IsValidScale(Vec3::sAxisX()));
  166. CHECK(!triangle2->IsValidScale(Vec3::sAxisY()));
  167. CHECK(!triangle2->IsValidScale(Vec3::sAxisZ()));
  168. CHECK(triangle2->IsValidScale(Vec3(2, 2, 2)));
  169. CHECK(triangle2->IsValidScale(Vec3(-1, 1, -1)));
  170. CHECK(!triangle2->IsValidScale(Vec3(2, 1, 1)));
  171. CHECK(!triangle2->IsValidScale(Vec3(1, 2, 1)));
  172. CHECK(!triangle2->IsValidScale(Vec3(1, 1, 2)));
  173. CHECK(triangle2->MakeScaleValid(Vec3::sZero()).IsClose(Vec3::sReplicate(ScaleHelpers::cMinScale), cMinScaleToleranceSq));
  174. CHECK(triangle2->MakeScaleValid(Vec3(2, 6, -4)) == Vec3(4, 4, -4));
  175. Ref<Shape> scaled = new ScaledShape(sphere, Vec3(1, 2, 1));
  176. CHECK(!scaled->IsValidScale(Vec3::sZero()));
  177. CHECK(!scaled->IsValidScale(Vec3(1, 1, 1)));
  178. CHECK(scaled->IsValidScale(Vec3(1, 0.5f, 1)));
  179. CHECK(scaled->IsValidScale(Vec3(-1, 0.5f, 1)));
  180. CHECK(!scaled->IsValidScale(Vec3(2, 1, 1)));
  181. CHECK(!scaled->IsValidScale(Vec3(1, 2, 1)));
  182. CHECK(!scaled->IsValidScale(Vec3(1, 1, 2)));
  183. CHECK(scaled->MakeScaleValid(Vec3(3, 3, 3)) == Vec3(4, 2, 4));
  184. CHECK(scaled->MakeScaleValid(Vec3(4, 2, 4)) == Vec3(4, 2, 4));
  185. Ref<Shape> scaled2 = new ScaledShape(scaled, Vec3(1, 0.5f, 1));
  186. CHECK(!scaled2->IsValidScale(Vec3::sZero()));
  187. CHECK(scaled2->IsValidScale(Vec3(2, 2, 2)));
  188. CHECK(scaled2->IsValidScale(Vec3(-1, 1, -1)));
  189. CHECK(!scaled2->IsValidScale(Vec3(2, 1, 1)));
  190. CHECK(!scaled2->IsValidScale(Vec3(1, 2, 1)));
  191. CHECK(!scaled2->IsValidScale(Vec3(1, 1, 2)));
  192. CHECK(scaled2->MakeScaleValid(Vec3(3, 3, 3)) == Vec3(3, 3, 3));
  193. CHECK(scaled2->MakeScaleValid(Vec3(5, 2, 5)) == Vec3(4, 4, 4));
  194. // Test a compound with shapes that can only be scaled uniformly
  195. StaticCompoundShapeSettings compound_settings;
  196. compound_settings.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisX(), 0.1f * JPH_PI), sphere);
  197. compound_settings.AddShape(Vec3(4, 5, 6), Quat::sRotation(Vec3::sAxisY(), 0.1f * JPH_PI), capsule);
  198. Ref<Shape> compound = compound_settings.Create().Get();
  199. CHECK(!compound->IsValidScale(Vec3::sZero()));
  200. CHECK(compound->IsValidScale(Vec3(1, 1, 1)));
  201. CHECK(compound->IsValidScale(Vec3(2, 2, 2)));
  202. CHECK(!compound->IsValidScale(Vec3(2, 1, 1)));
  203. CHECK(!compound->IsValidScale(Vec3(1, 2, 1)));
  204. CHECK(!compound->IsValidScale(Vec3(1, 1, 2)));
  205. // Test compound containing a triangle shape that can be scaled in any way
  206. StaticCompoundShapeSettings compound_settings2;
  207. compound_settings2.AddShape(Vec3(1, 2, 3), Quat::sIdentity(), triangle);
  208. compound_settings2.AddShape(Vec3(4, 5, 6), Quat::sIdentity(), new ScaledShape(triangle, Vec3(10, 11, 12)));
  209. Ref<Shape> compound2 = compound_settings2.Create().Get();
  210. CHECK(!compound2->IsValidScale(Vec3::sZero()));
  211. CHECK(compound2->IsValidScale(Vec3(1, 1, 1)));
  212. CHECK(compound2->IsValidScale(Vec3(2, 2, 2)));
  213. CHECK(compound2->IsValidScale(Vec3(2, 1, 1)));
  214. CHECK(compound2->IsValidScale(Vec3(1, 2, 1)));
  215. CHECK(compound2->IsValidScale(Vec3(1, 1, 2)));
  216. // Test rotations inside the compound of 90 degrees
  217. StaticCompoundShapeSettings compound_settings3;
  218. compound_settings3.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), triangle);
  219. compound_settings3.AddShape(Vec3(4, 5, 6), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new ScaledShape(triangle, Vec3(10, 11, 12)));
  220. Ref<Shape> compound3 = compound_settings3.Create().Get();
  221. CHECK(!compound3->IsValidScale(Vec3::sZero()));
  222. CHECK(compound3->IsValidScale(Vec3(1, 1, 1)));
  223. CHECK(compound3->IsValidScale(Vec3(2, 2, 2)));
  224. CHECK(compound3->IsValidScale(Vec3(2, 1, 1)));
  225. CHECK(compound3->IsValidScale(Vec3(1, 2, 1)));
  226. CHECK(compound3->IsValidScale(Vec3(1, 1, 2)));
  227. // Test non-90 degree rotations, this would cause shearing so is not allowed (we can't express that by passing a diagonal scale vector)
  228. StaticCompoundShapeSettings compound_settings4;
  229. compound_settings4.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), triangle);
  230. compound_settings4.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.25f * JPH_PI), triangle);
  231. Ref<Shape> compound4 = compound_settings4.Create().Get();
  232. CHECK(!compound4->IsValidScale(Vec3::sZero()));
  233. CHECK(compound4->IsValidScale(Vec3(1, 1, 1)));
  234. CHECK(compound4->IsValidScale(Vec3(2, 2, 2)));
  235. CHECK(!compound4->IsValidScale(Vec3(2, 1, 1)));
  236. CHECK(!compound4->IsValidScale(Vec3(1, 2, 1)));
  237. CHECK(compound4->IsValidScale(Vec3(1, 1, 2))); // We're rotation around Z, so non-uniform in the Z direction is ok
  238. // Test a mutable compound with shapes that can only be scaled uniformly
  239. MutableCompoundShapeSettings mutable_compound_settings;
  240. mutable_compound_settings.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisX(), 0.1f * JPH_PI), sphere);
  241. mutable_compound_settings.AddShape(Vec3(4, 5, 6), Quat::sRotation(Vec3::sAxisY(), 0.1f * JPH_PI), capsule);
  242. Ref<Shape> mutable_compound = mutable_compound_settings.Create().Get();
  243. CHECK(!mutable_compound->IsValidScale(Vec3::sZero()));
  244. CHECK(mutable_compound->IsValidScale(Vec3(1, 1, 1)));
  245. CHECK(mutable_compound->IsValidScale(Vec3(2, 2, 2)));
  246. CHECK(!mutable_compound->IsValidScale(Vec3(2, 1, 1)));
  247. CHECK(!mutable_compound->IsValidScale(Vec3(1, 2, 1)));
  248. CHECK(!mutable_compound->IsValidScale(Vec3(1, 1, 2)));
  249. // Test mutable compound containing a triangle shape that can be scaled in any way
  250. MutableCompoundShapeSettings mutable_compound_settings2;
  251. mutable_compound_settings2.AddShape(Vec3(1, 2, 3), Quat::sIdentity(), triangle);
  252. mutable_compound_settings2.AddShape(Vec3(4, 5, 6), Quat::sIdentity(), new ScaledShape(triangle, Vec3(10, 11, 12)));
  253. Ref<Shape> mutable_compound2 = mutable_compound_settings2.Create().Get();
  254. CHECK(!mutable_compound2->IsValidScale(Vec3::sZero()));
  255. CHECK(mutable_compound2->IsValidScale(Vec3(1, 1, 1)));
  256. CHECK(mutable_compound2->IsValidScale(Vec3(2, 2, 2)));
  257. CHECK(mutable_compound2->IsValidScale(Vec3(2, 1, 1)));
  258. CHECK(mutable_compound2->IsValidScale(Vec3(1, 2, 1)));
  259. CHECK(mutable_compound2->IsValidScale(Vec3(1, 1, 2)));
  260. // Test rotations inside the mutable compound of 90 degrees
  261. MutableCompoundShapeSettings mutable_compound_settings3;
  262. mutable_compound_settings3.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), triangle);
  263. mutable_compound_settings3.AddShape(Vec3(4, 5, 6), Quat::sRotation(Vec3::sAxisZ(), 0.5f * JPH_PI), new ScaledShape(triangle, Vec3(10, 11, 12)));
  264. Ref<Shape> mutable_compound3 = mutable_compound_settings3.Create().Get();
  265. CHECK(!mutable_compound3->IsValidScale(Vec3::sZero()));
  266. CHECK(mutable_compound3->IsValidScale(Vec3(1, 1, 1)));
  267. CHECK(mutable_compound3->IsValidScale(Vec3(2, 2, 2)));
  268. CHECK(mutable_compound3->IsValidScale(Vec3(2, 1, 1)));
  269. CHECK(mutable_compound3->IsValidScale(Vec3(1, 2, 1)));
  270. CHECK(mutable_compound3->IsValidScale(Vec3(1, 1, 2)));
  271. // Test non-90 degree rotations, this would cause shearing so is not allowed (we can't express that by passing a diagonal scale vector)
  272. MutableCompoundShapeSettings mutable_compound_settings4;
  273. mutable_compound_settings4.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), triangle);
  274. mutable_compound_settings4.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.25f * JPH_PI), triangle);
  275. Ref<Shape> mutable_compound4 = mutable_compound_settings4.Create().Get();
  276. CHECK(!mutable_compound4->IsValidScale(Vec3::sZero()));
  277. CHECK(mutable_compound4->IsValidScale(Vec3(1, 1, 1)));
  278. CHECK(mutable_compound4->IsValidScale(Vec3(2, 2, 2)));
  279. CHECK(!mutable_compound4->IsValidScale(Vec3(2, 1, 1)));
  280. CHECK(!mutable_compound4->IsValidScale(Vec3(1, 2, 1)));
  281. CHECK(mutable_compound4->IsValidScale(Vec3(1, 1, 2))); // We're rotation around Z, so non-uniform in the Z direction is ok
  282. // Test a cylinder rotated by 90 degrees around Z rotating Y to X, meaning that Y and Z should be scaled uniformly
  283. MutableCompoundShapeSettings mutable_compound_settings5;
  284. mutable_compound_settings5.AddShape(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), new CylinderShape(1.0f, 0.5f));
  285. Ref<Shape> mutable_compound5 = mutable_compound_settings5.Create().Get();
  286. CHECK(mutable_compound5->IsValidScale(Vec3::sReplicate(2)));
  287. CHECK(mutable_compound5->IsValidScale(Vec3(1, 2, 2)));
  288. CHECK(mutable_compound5->IsValidScale(Vec3(1, 2, -2)));
  289. CHECK(!mutable_compound5->IsValidScale(Vec3(2, 1, 2)));
  290. CHECK(!mutable_compound5->IsValidScale(Vec3(2, 2, 1)));
  291. CHECK(mutable_compound5->MakeScaleValid(Vec3::sReplicate(2)).IsClose(Vec3::sReplicate(2)));
  292. CHECK(mutable_compound5->MakeScaleValid(Vec3::sReplicate(-2)).IsClose(Vec3::sReplicate(-2)));
  293. CHECK(mutable_compound5->MakeScaleValid(Vec3(1, 2, 2)).IsClose(Vec3(1, 2, 2)));
  294. CHECK(mutable_compound5->MakeScaleValid(Vec3(1, 2, -2)).IsClose(Vec3(1, 2, -2)));
  295. CHECK(mutable_compound5->MakeScaleValid(Vec3(2, 1, 2)).IsClose(Vec3::sReplicate(5.0f / 3.0f))); // Not the best solution, but we don't have logic to average over YZ only
  296. CHECK(mutable_compound5->MakeScaleValid(Vec3(2, 2, 1)).IsClose(Vec3::sReplicate(5.0f / 3.0f))); // Not the best solution, but we don't have logic to average over YZ only
  297. // Test a rotated translated shape that can only be scaled uniformly
  298. RotatedTranslatedShapeSettings rt_settings(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisX(), 0.1f * JPH_PI), sphere);
  299. Ref<Shape> rt_shape = rt_settings.Create().Get();
  300. CHECK(!rt_shape->IsValidScale(Vec3::sZero()));
  301. CHECK(rt_shape->IsValidScale(Vec3(1, 1, 1)));
  302. CHECK(rt_shape->IsValidScale(Vec3(2, 2, 2)));
  303. CHECK(!rt_shape->IsValidScale(Vec3(2, 1, 1)));
  304. CHECK(!rt_shape->IsValidScale(Vec3(1, 2, 1)));
  305. CHECK(!rt_shape->IsValidScale(Vec3(1, 1, 2)));
  306. // Test rotated translated shape containing a triangle shape that can be scaled in any way
  307. RotatedTranslatedShapeSettings rt_settings2(Vec3(4, 5, 6), Quat::sIdentity(), new ScaledShape(triangle, Vec3(10, 11, 12)));
  308. Ref<Shape> rt_shape2 = rt_settings2.Create().Get();
  309. CHECK(!rt_shape2->IsValidScale(Vec3::sZero()));
  310. CHECK(rt_shape2->IsValidScale(Vec3(1, 1, 1)));
  311. CHECK(rt_shape2->IsValidScale(Vec3(2, 2, 2)));
  312. CHECK(rt_shape2->IsValidScale(Vec3(2, 1, 1)));
  313. CHECK(rt_shape2->IsValidScale(Vec3(1, 2, 1)));
  314. CHECK(rt_shape2->IsValidScale(Vec3(1, 1, 2)));
  315. // Test rotations inside the rotated translated of 90 degrees
  316. RotatedTranslatedShapeSettings rt_settings3(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), triangle);
  317. Ref<Shape> rt_shape3 = rt_settings3.Create().Get();
  318. CHECK(!rt_shape3->IsValidScale(Vec3::sZero()));
  319. CHECK(rt_shape3->IsValidScale(Vec3(1, 1, 1)));
  320. CHECK(rt_shape3->IsValidScale(Vec3(2, 2, 2)));
  321. CHECK(rt_shape3->IsValidScale(Vec3(2, 1, 1)));
  322. CHECK(rt_shape3->IsValidScale(Vec3(1, 2, 1)));
  323. CHECK(rt_shape3->IsValidScale(Vec3(1, 1, 2)));
  324. // Test non-90 degree rotations, this would cause shearing so is not allowed (we can't express that by passing a diagonal scale vector)
  325. RotatedTranslatedShapeSettings rt_settings4(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), 0.25f * JPH_PI), triangle);
  326. Ref<Shape> rt_shape4 = rt_settings4.Create().Get();
  327. CHECK(!rt_shape4->IsValidScale(Vec3::sZero()));
  328. CHECK(rt_shape4->IsValidScale(Vec3(1, 1, 1)));
  329. CHECK(rt_shape4->IsValidScale(Vec3(2, 2, 2)));
  330. CHECK(!rt_shape4->IsValidScale(Vec3(2, 1, 1)));
  331. CHECK(!rt_shape4->IsValidScale(Vec3(1, 2, 1)));
  332. CHECK(rt_shape4->IsValidScale(Vec3(1, 1, 2))); // We're rotation around Z, so non-uniform in the Z direction is ok
  333. // Test a cylinder rotated by 90 degrees around Z rotating Y to X, meaning that Y and Z should be scaled uniformly
  334. RotatedTranslatedShapeSettings rt_settings5(Vec3(1, 2, 3), Quat::sRotation(Vec3::sAxisZ(), -0.5f * JPH_PI), new CylinderShape(1.0f, 0.5f));
  335. Ref<Shape> rt_shape5 = rt_settings5.Create().Get();
  336. CHECK(rt_shape5->IsValidScale(Vec3::sReplicate(2)));
  337. CHECK(rt_shape5->IsValidScale(Vec3(1, 2, 2)));
  338. CHECK(rt_shape5->IsValidScale(Vec3(1, 2, -2)));
  339. CHECK(!rt_shape5->IsValidScale(Vec3(2, 1, 2)));
  340. CHECK(!rt_shape5->IsValidScale(Vec3(2, 2, 1)));
  341. CHECK(rt_shape5->MakeScaleValid(Vec3::sReplicate(2)).IsClose(Vec3::sReplicate(2)));
  342. CHECK(rt_shape5->MakeScaleValid(Vec3::sReplicate(-2)).IsClose(Vec3::sReplicate(-2)));
  343. CHECK(rt_shape5->MakeScaleValid(Vec3(1, 2, 2)).IsClose(Vec3(1, 2, 2)));
  344. CHECK(rt_shape5->MakeScaleValid(Vec3(1, 2, -2)).IsClose(Vec3(1, 2, -2)));
  345. CHECK(rt_shape5->MakeScaleValid(Vec3(2, 1, 2)).IsClose(Vec3(2, 1.5f, 1.5f))); // YZ will be averaged here
  346. CHECK(rt_shape5->MakeScaleValid(Vec3(2, 2, 1)).IsClose(Vec3(2, 1.5f, 1.5f))); // YZ will be averaged here
  347. }
  348. // Test embedded shape
  349. TEST_CASE("TestEmbeddedShape")
  350. {
  351. {
  352. // Test shape constructed on stack, where shape construction succeeds
  353. ConvexHullShapeSettings settings;
  354. settings.mPoints.push_back(Vec3(0, 0, 0));
  355. settings.mPoints.push_back(Vec3(1, 0, 0));
  356. settings.mPoints.push_back(Vec3(0, 1, 0));
  357. settings.mPoints.push_back(Vec3(0, 0, 1));
  358. Shape::ShapeResult result;
  359. ConvexHullShape shape(settings, result);
  360. shape.SetEmbedded();
  361. CHECK(result.IsValid());
  362. result.Clear(); // Release the reference from the result
  363. // Test CollidePoint for this shape
  364. AllHitCollisionCollector<CollidePointCollector> collector;
  365. shape.CollidePoint(Vec3::sReplicate(-0.1f) - shape.GetCenterOfMass(), SubShapeIDCreator(), collector);
  366. CHECK(collector.mHits.empty());
  367. shape.CollidePoint(Vec3::sReplicate(0.1f) - shape.GetCenterOfMass(), SubShapeIDCreator(), collector);
  368. CHECK(collector.mHits.size() == 1);
  369. }
  370. {
  371. // Test shape constructed on stack, where shape construction fails
  372. ConvexHullShapeSettings settings;
  373. Shape::ShapeResult result;
  374. ConvexHullShape shape(settings, result);
  375. shape.SetEmbedded();
  376. CHECK(!result.IsValid());
  377. }
  378. }
  379. // Test re-creating shape using the same settings object
  380. TEST_CASE("TestClearCachedResult")
  381. {
  382. // Create a sphere and check radius
  383. SphereShapeSettings sphere_settings(1.0f);
  384. RefConst<SphereShape> sphere1 = StaticCast<SphereShape>(sphere_settings.Create().Get());
  385. CHECK(sphere1->GetRadius() == 1.0f);
  386. // Modify radius and check that creating the shape again returns the cached result
  387. sphere_settings.mRadius = 2.0f;
  388. RefConst<SphereShape> sphere2 = StaticCast<SphereShape>(sphere_settings.Create().Get());
  389. CHECK(sphere2 == sphere1);
  390. sphere_settings.ClearCachedResult();
  391. RefConst<SphereShape> sphere3 = StaticCast<SphereShape>(sphere_settings.Create().Get());
  392. CHECK(sphere3->GetRadius() == 2.0f);
  393. }
  394. // Test submerged volume calculation
  395. TEST_CASE("TestGetSubmergedVolume")
  396. {
  397. Ref<BoxShape> box = new BoxShape(Vec3(1, 2, 3));
  398. Vec3 scale(2, -3, 4);
  399. Mat44 translation = Mat44::sTranslation(Vec3(0, 6, 0)); // Translate so we're on the y = 0 plane
  400. // Plane pointing positive Y
  401. // Entirely above the plane
  402. {
  403. float total_volume, submerged_volume;
  404. Vec3 center_of_buoyancy;
  405. 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()));
  406. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  407. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  408. }
  409. // Entirely below the plane
  410. {
  411. float total_volume, submerged_volume;
  412. Vec3 center_of_buoyancy;
  413. 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()));
  414. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  415. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  416. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  417. }
  418. // Halfway through
  419. {
  420. float total_volume, submerged_volume;
  421. Vec3 center_of_buoyancy;
  422. 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()));
  423. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  424. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 6.0f * 24.0f);
  425. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 3, 0));
  426. }
  427. // Plane pointing negative Y
  428. // Entirely above the plane
  429. {
  430. float total_volume, submerged_volume;
  431. Vec3 center_of_buoyancy;
  432. 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()));
  433. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  434. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  435. }
  436. // Entirely below the plane
  437. {
  438. float total_volume, submerged_volume;
  439. Vec3 center_of_buoyancy;
  440. 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()));
  441. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  442. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  443. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  444. }
  445. // Halfway through
  446. {
  447. float total_volume, submerged_volume;
  448. Vec3 center_of_buoyancy;
  449. 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()));
  450. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  451. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 6.0f * 24.0f);
  452. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 9, 0));
  453. }
  454. // Plane pointing positive X
  455. // Entirely above the plane
  456. {
  457. float total_volume, submerged_volume;
  458. Vec3 center_of_buoyancy;
  459. 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()));
  460. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  461. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  462. }
  463. // Entirely below the plane
  464. {
  465. float total_volume, submerged_volume;
  466. Vec3 center_of_buoyancy;
  467. 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()));
  468. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  469. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  470. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  471. }
  472. // Halfway through
  473. {
  474. float total_volume, submerged_volume;
  475. Vec3 center_of_buoyancy;
  476. 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()));
  477. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  478. CHECK_APPROX_EQUAL(submerged_volume, 2.0f * 12.0f * 24.0f);
  479. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(-1, 6, 0));
  480. }
  481. // Plane pointing negative X
  482. // Entirely above the plane
  483. {
  484. float total_volume, submerged_volume;
  485. Vec3 center_of_buoyancy;
  486. 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()));
  487. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  488. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  489. }
  490. // Entirely below the plane
  491. {
  492. float total_volume, submerged_volume;
  493. Vec3 center_of_buoyancy;
  494. 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()));
  495. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  496. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  497. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  498. }
  499. // Halfway through
  500. {
  501. float total_volume, submerged_volume;
  502. Vec3 center_of_buoyancy;
  503. 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()));
  504. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  505. CHECK_APPROX_EQUAL(submerged_volume, 2.0f * 12.0f * 24.0f);
  506. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(1, 6, 0));
  507. }
  508. // Plane pointing positive Z
  509. // Entirely above the plane
  510. {
  511. float total_volume, submerged_volume;
  512. Vec3 center_of_buoyancy;
  513. 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()));
  514. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  515. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  516. }
  517. // Entirely below the plane
  518. {
  519. float total_volume, submerged_volume;
  520. Vec3 center_of_buoyancy;
  521. 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()));
  522. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  523. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  524. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  525. }
  526. // Halfway through
  527. {
  528. float total_volume, submerged_volume;
  529. Vec3 center_of_buoyancy;
  530. 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()));
  531. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  532. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 12.0f);
  533. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, -6));
  534. }
  535. // Plane pointing negative Z
  536. // Entirely above the plane
  537. {
  538. float total_volume, submerged_volume;
  539. Vec3 center_of_buoyancy;
  540. 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()));
  541. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  542. CHECK_APPROX_EQUAL(submerged_volume, 0.0f);
  543. }
  544. // Entirely below the plane
  545. {
  546. float total_volume, submerged_volume;
  547. Vec3 center_of_buoyancy;
  548. 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()));
  549. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  550. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 24.0f);
  551. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 0));
  552. }
  553. // Halfway through
  554. {
  555. float total_volume, submerged_volume;
  556. Vec3 center_of_buoyancy;
  557. 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()));
  558. CHECK_APPROX_EQUAL(total_volume, 4.0f * 12.0f * 24.0f);
  559. CHECK_APPROX_EQUAL(submerged_volume, 4.0f * 12.0f * 12.0f);
  560. CHECK_APPROX_EQUAL(center_of_buoyancy, Vec3(0, 6, 6));
  561. }
  562. }
  563. // Test setting user data on shapes
  564. TEST_CASE("TestShapeUserData")
  565. {
  566. const float cRadius = 2.0f;
  567. // Create a sphere with user data
  568. SphereShapeSettings sphere_settings(cRadius);
  569. sphere_settings.mUserData = 0x1234567887654321;
  570. Ref<Shape> sphere = sphere_settings.Create().Get();
  571. CHECK(sphere->GetUserData() == 0x1234567887654321);
  572. // Change the user data
  573. sphere->SetUserData(0x5678123443218765);
  574. CHECK(sphere->GetUserData() == 0x5678123443218765);
  575. stringstream data;
  576. // Write sphere to a binary stream
  577. {
  578. StreamOutWrapper stream_out(data);
  579. sphere->SaveBinaryState(stream_out);
  580. }
  581. // Destroy the sphere
  582. sphere = nullptr;
  583. // Read sphere from binary stream
  584. {
  585. StreamInWrapper stream_in(data);
  586. sphere = Shape::sRestoreFromBinaryState(stream_in).Get();
  587. }
  588. // Check that the sphere and its user data was preserved
  589. CHECK(sphere->GetType() == EShapeType::Convex);
  590. CHECK(sphere->GetSubType() == EShapeSubType::Sphere);
  591. CHECK(sphere->GetUserData() == 0x5678123443218765);
  592. CHECK(StaticCast<SphereShape>(sphere)->GetRadius() == cRadius);
  593. }
  594. // Test setting user data on shapes
  595. TEST_CASE("TestIsValidSubShapeID")
  596. {
  597. MutableCompoundShapeSettings shape1_settings;
  598. RefConst<CompoundShape> shape1 = StaticCast<CompoundShape>(shape1_settings.Create().Get());
  599. MutableCompoundShapeSettings shape2_settings;
  600. shape2_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), new SphereShape(1.0f));
  601. shape2_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), new SphereShape(1.0f));
  602. shape2_settings.AddShape(Vec3::sZero(), Quat::sIdentity(), new SphereShape(1.0f));
  603. RefConst<CompoundShape> shape2 = StaticCast<CompoundShape>(shape2_settings.Create().Get());
  604. // Get sub shape IDs of shape 2 and test if they're valid
  605. SubShapeID sub_shape1 = shape2->GetSubShapeIDFromIndex(0, SubShapeIDCreator()).GetID();
  606. CHECK(shape2->IsSubShapeIDValid(sub_shape1));
  607. SubShapeID sub_shape2 = shape2->GetSubShapeIDFromIndex(1, SubShapeIDCreator()).GetID();
  608. CHECK(shape2->IsSubShapeIDValid(sub_shape2));
  609. SubShapeID sub_shape3 = shape2->GetSubShapeIDFromIndex(2, SubShapeIDCreator()).GetID();
  610. CHECK(shape2->IsSubShapeIDValid(sub_shape3));
  611. SubShapeID sub_shape4 = shape2->GetSubShapeIDFromIndex(3, SubShapeIDCreator()).GetID(); // This one doesn't exist
  612. CHECK(!shape2->IsSubShapeIDValid(sub_shape4));
  613. // Shape 1 has no parts so these sub shape ID's should not be valid
  614. CHECK(!shape1->IsSubShapeIDValid(sub_shape1));
  615. CHECK(!shape1->IsSubShapeIDValid(sub_shape2));
  616. CHECK(!shape1->IsSubShapeIDValid(sub_shape3));
  617. CHECK(!shape1->IsSubShapeIDValid(sub_shape4));
  618. }
  619. // Test that an error is reported when we run out of sub shape bits
  620. TEST_CASE("TestOutOfSubShapeIDBits")
  621. {
  622. static constexpr uint32 cHeightFieldSamples = 1024;
  623. static constexpr int cNumBitsPerCompound = 4;
  624. // Create a heightfield
  625. float *samples = new float [cHeightFieldSamples * cHeightFieldSamples];
  626. memset(samples, 0, cHeightFieldSamples * cHeightFieldSamples * sizeof(float));
  627. RefConst<Shape> previous_shape = HeightFieldShapeSettings(samples, Vec3::sZero(), Vec3::sReplicate(1.0f), cHeightFieldSamples).Create().Get();
  628. delete [] samples;
  629. // Calculate the amount of bits needed to address all triangles in the heightfield
  630. uint num_bits = 32 - CountLeadingZeros((cHeightFieldSamples - 1) * (cHeightFieldSamples - 1) * 2);
  631. for (;;)
  632. {
  633. // Check that the total sub shape ID bits up to this point is correct
  634. CHECK(previous_shape->GetSubShapeIDBitsRecursive() == num_bits);
  635. // Create a compound with a number of sub shapes
  636. StaticCompoundShapeSettings compound_settings;
  637. compound_settings.SetEmbedded();
  638. for (int i = 0; i < (1 << cNumBitsPerCompound) ; ++i)
  639. compound_settings.AddShape(Vec3((float)i, 0, 0), Quat::sIdentity(), previous_shape);
  640. Shape::ShapeResult result = compound_settings.Create();
  641. num_bits += cNumBitsPerCompound;
  642. if (num_bits < SubShapeID::MaxBits)
  643. {
  644. // Creation should have succeeded
  645. CHECK(result.IsValid());
  646. previous_shape = result.Get();
  647. }
  648. else
  649. {
  650. // Creation should have failed because we ran out of bits
  651. CHECK(!result.IsValid());
  652. break;
  653. }
  654. }
  655. }
  656. TEST_CASE("TestEmptyMutableCompound")
  657. {
  658. // Create empty shape
  659. RefConst<Shape> mutable_compound = new MutableCompoundShape();
  660. // A non-identity rotation
  661. Quat rotation = Quat::sRotation(Vec3::sReplicate(1.0f / sqrt(3.0f)), 0.1f * JPH_PI);
  662. // Check that local bounding box is invalid
  663. AABox bounds1 = mutable_compound->GetLocalBounds();
  664. CHECK(!bounds1.IsValid());
  665. // Check that get world space bounds returns an invalid bounding box
  666. AABox bounds2 = mutable_compound->GetWorldSpaceBounds(Mat44::sRotationTranslation(rotation, Vec3(100, 200, 300)), Vec3(1, 2, 3));
  667. CHECK(!bounds2.IsValid());
  668. // Check that get world space bounds returns an invalid bounding box for double precision parameters
  669. AABox bounds3 = mutable_compound->GetWorldSpaceBounds(DMat44::sRotationTranslation(rotation, DVec3(100, 200, 300)), Vec3(1, 2, 3));
  670. CHECK(!bounds3.IsValid());
  671. }
  672. TEST_CASE("TestSaveMeshShape")
  673. {
  674. // Create an n x n grid of triangles
  675. const int n = 10;
  676. const float s = 0.1f;
  677. TriangleList triangles;
  678. for (int z = 0; z < n; ++z)
  679. for (int x = 0; x < n; ++x)
  680. {
  681. float fx = s * x - s * n / 2, fz = s * z - s * n / 2;
  682. triangles.push_back(Triangle(Vec3(fx, 0, fz), Vec3(fx, 0, fz + s), Vec3(fx + s, 0, fz + s)));
  683. triangles.push_back(Triangle(Vec3(fx, 0, fz), Vec3(fx + s, 0, fz + s), Vec3(fx + s, 0, fz)));
  684. }
  685. MeshShapeSettings mesh_settings(triangles);
  686. mesh_settings.SetEmbedded();
  687. RefConst<Shape> shape = mesh_settings.Create().Get();
  688. // Calculate expected bounds
  689. AABox expected_bounds;
  690. for (const Triangle &t : triangles)
  691. for (const Float3 &v : t.mV)
  692. expected_bounds.Encapsulate(Vec3(v));
  693. stringstream stream;
  694. {
  695. // Write mesh to stream
  696. StreamOutWrapper wrapper(stream);
  697. shape->SaveBinaryState(wrapper);
  698. }
  699. {
  700. // Read back mesh
  701. StreamInWrapper iwrapper(stream);
  702. Shape::ShapeResult result = Shape::sRestoreFromBinaryState(iwrapper);
  703. CHECK(result.IsValid());
  704. RefConst<MeshShape> mesh_shape = StaticCast<MeshShape>(result.Get());
  705. // Test if it contains the same amount of triangles
  706. Shape::Stats stats = mesh_shape->GetStats();
  707. CHECK(stats.mNumTriangles == triangles.size());
  708. // Check bounding box
  709. CHECK(mesh_shape->GetLocalBounds() == expected_bounds);
  710. // Check if we can hit it with a ray
  711. RayCastResult hit;
  712. RayCast ray(Vec3(0.5f * s, 1, 0.25f * s), Vec3(0, -2, 0)); // Hit in the center of a triangle
  713. CHECK(mesh_shape->CastRay(ray, SubShapeIDCreator(), hit));
  714. CHECK(hit.mFraction == 0.5f);
  715. CHECK(mesh_shape->GetSurfaceNormal(hit.mSubShapeID2, ray.GetPointOnRay(hit.mFraction)) == Vec3::sAxisY());
  716. }
  717. }
  718. TEST_CASE("TestMutableCompoundShapeAdjustCenterOfMass")
  719. {
  720. // Start with a box at (-1 0 0)
  721. MutableCompoundShapeSettings settings;
  722. Ref<Shape> box_shape1 = new BoxShape(Vec3::sReplicate(1.0f));
  723. box_shape1->SetUserData(1);
  724. settings.AddShape(Vec3(-1.0f, 0.0f, 0.0f), Quat::sIdentity(), box_shape1);
  725. Ref<MutableCompoundShape> shape = StaticCast<MutableCompoundShape>(settings.Create().Get());
  726. CHECK(shape->GetCenterOfMass() == Vec3(-1.0f, 0.0f, 0.0f));
  727. CHECK(shape->GetLocalBounds() == AABox(Vec3::sReplicate(-1.0f), Vec3::sReplicate(1.0f)));
  728. // Check that we can hit the box
  729. AllHitCollisionCollector<CollidePointCollector> collector;
  730. shape->CollidePoint(Vec3(-0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  731. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 1));
  732. collector.Reset();
  733. CHECK(collector.mHits.empty());
  734. // Now add another box at (1 0 0)
  735. Ref<Shape> box_shape2 = new BoxShape(Vec3::sReplicate(1.0f));
  736. box_shape2->SetUserData(2);
  737. shape->AddShape(Vec3(1.0f, 0.0f, 0.0f), Quat::sIdentity(), box_shape2);
  738. CHECK(shape->GetCenterOfMass() == Vec3(-1.0f, 0.0f, 0.0f));
  739. CHECK(shape->GetLocalBounds() == AABox(Vec3(-1.0f, -1.0f, -1.0f), Vec3(3.0f, 1.0f, 1.0f)));
  740. // Check that we can hit both boxes
  741. shape->CollidePoint(Vec3(-0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  742. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 1));
  743. collector.Reset();
  744. shape->CollidePoint(Vec3(0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  745. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 2));
  746. collector.Reset();
  747. // Adjust the center of mass
  748. shape->AdjustCenterOfMass();
  749. CHECK(shape->GetCenterOfMass() == Vec3::sZero());
  750. CHECK(shape->GetLocalBounds() == AABox(Vec3(-2.0f, -1.0f, -1.0f), Vec3(2.0f, 1.0f, 1.0f)));
  751. // Check that we can hit both boxes
  752. shape->CollidePoint(Vec3(-0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  753. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 1));
  754. collector.Reset();
  755. shape->CollidePoint(Vec3(0.5f, 0.0f, 0.0f) - shape->GetCenterOfMass(), SubShapeIDCreator(), collector);
  756. CHECK((collector.mHits.size() == 1 && shape->GetSubShapeUserData(collector.mHits[0].mSubShapeID2) == 2));
  757. collector.Reset();
  758. }
  759. }