PhysicsTests.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include "PhysicsTestContext.h"
  5. #include "Layers.h"
  6. #include "LoggingBodyActivationListener.h"
  7. #include "LoggingContactListener.h"
  8. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  9. #include <Jolt/Physics/Collision/Shape/RotatedTranslatedShape.h>
  10. #include <Jolt/Physics/Body/BodyLockMulti.h>
  11. TEST_SUITE("PhysicsTests")
  12. {
  13. // Gravity vector
  14. const Vec3 cGravity = Vec3(0.0f, -9.81f, 0.0f);
  15. // Test the test framework's helper functions
  16. TEST_CASE("TestPhysicsTestContext")
  17. {
  18. // Test that the Symplectic Euler integrator is close enough to the real value
  19. const float cSimulationTime = 2.0f;
  20. // For position: x = x0 + v0 * t + 1/2 * a * t^2
  21. const Vec3 cInitialPos(0.0f, 10.0f, 0.0f);
  22. PhysicsTestContext c;
  23. Vec3 simulated_pos = c.PredictPosition(cInitialPos, Vec3::sZero(), cGravity, cSimulationTime);
  24. Vec3 integrated_position = cInitialPos + 0.5f * cGravity * Square(cSimulationTime);
  25. CHECK_APPROX_EQUAL(integrated_position, simulated_pos, 0.2f);
  26. // For rotation
  27. const Quat cInitialRot(Quat::sRotation(Vec3::sAxisY(), 0.1f));
  28. const Vec3 cAngularAcceleration(0.0f, 2.0f, 0.0f);
  29. Quat simulated_rot = c.PredictOrientation(cInitialRot, Vec3::sZero(), cAngularAcceleration, cSimulationTime);
  30. Vec3 integrated_acceleration = 0.5f * cAngularAcceleration * Square(cSimulationTime);
  31. float integrated_acceleration_len = integrated_acceleration.Length();
  32. Quat integrated_rot = Quat::sRotation(integrated_acceleration / integrated_acceleration_len, integrated_acceleration_len) * cInitialRot;
  33. CHECK_APPROX_EQUAL(integrated_rot, simulated_rot, 0.02f);
  34. }
  35. TEST_CASE("TestPhysicsBodyLock")
  36. {
  37. PhysicsTestContext c;
  38. // Check that we cannot lock the invalid body ID
  39. {
  40. BodyLockRead lock(c.GetSystem()->GetBodyLockInterface(), BodyID());
  41. CHECK_FALSE(lock.Succeeded());
  42. CHECK_FALSE(lock.SucceededAndIsInBroadPhase());
  43. }
  44. BodyID body1_id;
  45. {
  46. // Create a box
  47. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, 0, Vec3::sReplicate(1.0f));
  48. body1_id = body1.GetID();
  49. CHECK(body1_id.GetIndex() == 0);
  50. CHECK(body1_id.GetSequenceNumber() == 1);
  51. // Create another box
  52. Body &body2 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, 0, Vec3::sReplicate(1.0f));
  53. BodyID body2_id = body2.GetID();
  54. CHECK(body2_id.GetIndex() == 1);
  55. CHECK(body2_id.GetSequenceNumber() == 1);
  56. // Check that we can lock the first box
  57. {
  58. BodyLockRead lock1(c.GetSystem()->GetBodyLockInterface(), body1_id);
  59. CHECK(lock1.Succeeded());
  60. CHECK(lock1.SucceededAndIsInBroadPhase());
  61. }
  62. // Remove the first box
  63. c.GetSystem()->GetBodyInterface().RemoveBody(body1_id);
  64. // Check that we can lock the first box
  65. {
  66. BodyLockWrite lock1(c.GetSystem()->GetBodyLockInterface(), body1_id);
  67. CHECK(lock1.Succeeded());
  68. CHECK_FALSE(lock1.SucceededAndIsInBroadPhase());
  69. }
  70. // Destroy the first box
  71. c.GetSystem()->GetBodyInterface().DestroyBody(body1_id);
  72. // Check that we can not lock the body anymore
  73. {
  74. BodyLockWrite lock1(c.GetSystem()->GetBodyLockInterface(), body1_id);
  75. CHECK_FALSE(lock1.Succeeded());
  76. CHECK_FALSE(lock1.SucceededAndIsInBroadPhase());
  77. }
  78. }
  79. // Create another box
  80. Body &body3 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, 0, Vec3::sReplicate(1.0f));
  81. BodyID body3_id = body3.GetID();
  82. CHECK(body3_id.GetIndex() == 0); // Check index reused
  83. CHECK(body3_id.GetSequenceNumber() == 2); // Check sequence number changed
  84. // Check that we can lock it
  85. {
  86. BodyLockRead lock3(c.GetSystem()->GetBodyLockInterface(), body3_id);
  87. CHECK(lock3.Succeeded());
  88. CHECK(lock3.SucceededAndIsInBroadPhase());
  89. }
  90. // Check that we can't lock the old body with the same body index anymore
  91. {
  92. BodyLockRead lock1(c.GetSystem()->GetBodyLockInterface(), body1_id);
  93. CHECK_FALSE(lock1.Succeeded());
  94. CHECK_FALSE(lock1.SucceededAndIsInBroadPhase());
  95. }
  96. }
  97. TEST_CASE("TestPhysicsBodyLockMulti")
  98. {
  99. PhysicsTestContext c;
  100. // Check that we cannot lock the invalid body ID
  101. {
  102. BodyID bodies[] = { BodyID(), BodyID() };
  103. BodyLockMultiRead lock(c.GetSystem()->GetBodyLockInterface(), bodies, 2);
  104. CHECK(lock.GetBody(0) == nullptr);
  105. CHECK(lock.GetBody(1) == nullptr);
  106. }
  107. {
  108. // Create two bodies
  109. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, 0, Vec3::sReplicate(1.0f));
  110. Body &body2 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, 0, Vec3::sReplicate(1.0f));
  111. BodyID bodies[] = { body1.GetID(), body2.GetID() };
  112. {
  113. // Lock the bodies
  114. BodyLockMultiWrite lock(c.GetSystem()->GetBodyLockInterface(), bodies, 2);
  115. CHECK(lock.GetBody(0) == &body1);
  116. CHECK(lock.GetBody(1) == &body2);
  117. }
  118. // Destroy body 1
  119. c.GetSystem()->GetBodyInterface().RemoveBody(bodies[0]);
  120. c.GetSystem()->GetBodyInterface().DestroyBody(bodies[0]);
  121. {
  122. // Lock the bodies
  123. BodyLockMultiRead lock(c.GetSystem()->GetBodyLockInterface(), bodies, 2);
  124. CHECK(lock.GetBody(0) == nullptr);
  125. CHECK(lock.GetBody(1) == &body2);
  126. }
  127. }
  128. }
  129. TEST_CASE("TestPhysicsBodyID")
  130. {
  131. {
  132. BodyID body_id(0);
  133. CHECK(body_id.GetIndex() == 0);
  134. CHECK(body_id.GetSequenceNumber() == 0);
  135. }
  136. {
  137. BodyID body_id(~BodyID::cBroadPhaseBit);
  138. CHECK(body_id.GetIndex() == BodyID::cMaxBodyIndex);
  139. CHECK(body_id.GetSequenceNumber() == BodyID::cMaxSequenceNumber);
  140. }
  141. }
  142. TEST_CASE("TestPhysicsBodyIDSequenceNumber")
  143. {
  144. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  145. BodyInterface &bi = c.GetBodyInterface();
  146. // Create a body and check it's id
  147. BodyID body0_id = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1)).GetID();
  148. CHECK(body0_id == BodyID(0, 1)); // Body 0, sequence number 1
  149. // Check that the sequence numbers aren't reused until after 256 iterations
  150. for (int seq_no = 1; seq_no < 258; ++seq_no)
  151. {
  152. BodyID body1_id = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1)).GetID();
  153. CHECK(body1_id == BodyID(1, uint8(seq_no))); // Body 1
  154. bi.RemoveBody(body1_id);
  155. bi.DestroyBody(body1_id);
  156. }
  157. bi.RemoveBody(body0_id);
  158. bi.DestroyBody(body0_id);
  159. }
  160. TEST_CASE("TestPhysicsBodyIDOverride")
  161. {
  162. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  163. BodyInterface &bi = c.GetBodyInterface();
  164. // Dummy creation settings
  165. BodyCreationSettings bc(new BoxShape(Vec3::sReplicate(1.0f)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, Layers::NON_MOVING);
  166. // Create a body
  167. Body *b1 = bi.CreateBody(bc);
  168. CHECK(b1->GetID() == BodyID(0, 1));
  169. // Create body with same ID and same sequence number
  170. Body *b2 = bi.CreateBodyWithID(BodyID(0, 1), bc);
  171. CHECK(b2 == nullptr);
  172. // Create body with same ID and different sequence number
  173. b2 = bi.CreateBodyWithID(BodyID(0, 2), bc);
  174. CHECK(b2 == nullptr);
  175. // Create body with different ID (leave 1 open slot)
  176. b2 = bi.CreateBodyWithID(BodyID(2, 1), bc);
  177. CHECK(b2 != nullptr);
  178. CHECK(b2->GetID() == BodyID(2, 1));
  179. // Create another body and check that the open slot is returned
  180. Body *b3 = bi.CreateBody(bc);
  181. CHECK(b3->GetID() == BodyID(1, 1));
  182. // Create another body and check that we do not hand out the body with specified ID
  183. Body *b4 = bi.CreateBody(bc);
  184. CHECK(b4->GetID() == BodyID(3, 1));
  185. // Delete and recreate body 4
  186. CHECK(bi.CreateBodyWithID(BodyID(3, 1), bc) == nullptr);
  187. bi.DestroyBody(b4->GetID());
  188. b4 = bi.CreateBodyWithID(BodyID(3, 1), bc);
  189. CHECK(b4 != nullptr);
  190. CHECK(b4->GetID() == BodyID(3, 1));
  191. // Clean up all bodies
  192. bi.DestroyBody(b1->GetID());
  193. bi.DestroyBody(b2->GetID());
  194. bi.DestroyBody(b3->GetID());
  195. bi.DestroyBody(b4->GetID());
  196. // Recreate body 1
  197. b1 = bi.CreateBodyWithID(BodyID(0, 1), bc);
  198. CHECK(b1 != nullptr);
  199. CHECK(b1->GetID() == BodyID(0, 1));
  200. // Destroy last body
  201. bi.DestroyBody(b1->GetID());
  202. }
  203. TEST_CASE("TestPhysicsBodyUserData")
  204. {
  205. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  206. BodyInterface &bi = c.GetBodyInterface();
  207. // Create a body and pass user data through the creation settings
  208. BodyCreationSettings body_settings(new BoxShape(Vec3::sReplicate(1.0f)), Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  209. body_settings.mUserData = 0x1234567887654321;
  210. Body *body = bi.CreateBody(body_settings);
  211. CHECK(body->GetUserData() == 0x1234567887654321);
  212. // Change the user data
  213. body->SetUserData(0x5678123443218765);
  214. CHECK(body->GetUserData() == 0x5678123443218765);
  215. }
  216. TEST_CASE("TestPhysicsPosition")
  217. {
  218. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  219. BodyInterface &bi = c.GetBodyInterface();
  220. // Translate / rotate the box
  221. Vec3 box_pos(1, 2, 3);
  222. Quat box_rotation = Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI);
  223. // Translate / rotate the body
  224. Vec3 body_pos(4, 5, 6);
  225. Quat body_rotation = Quat::sRotation(Vec3::sAxisY(), 0.3f * JPH_PI);
  226. Mat44 body_transform = Mat44::sRotationTranslation(body_rotation, body_pos);
  227. Mat44 com_transform = body_transform * Mat44::sTranslation(box_pos);
  228. // Create body
  229. BodyCreationSettings body_settings(new RotatedTranslatedShapeSettings(box_pos, box_rotation, new BoxShape(Vec3::sReplicate(1.0f))), body_pos, body_rotation, EMotionType::Static, Layers::NON_MOVING);
  230. Body *body = bi.CreateBody(body_settings);
  231. // Check that the correct positions / rotations are reported
  232. CHECK_APPROX_EQUAL(body->GetPosition(), body_pos);
  233. CHECK_APPROX_EQUAL(body->GetRotation(), body_rotation);
  234. CHECK_APPROX_EQUAL(body->GetWorldTransform(), body_transform);
  235. CHECK_APPROX_EQUAL(body->GetCenterOfMassPosition(), com_transform.GetTranslation());
  236. CHECK_APPROX_EQUAL(body->GetCenterOfMassTransform(), com_transform);
  237. CHECK_APPROX_EQUAL(body->GetInverseCenterOfMassTransform(), com_transform.InversedRotationTranslation());
  238. }
  239. TEST_CASE("TestPhysicsOverrideMassAndInertia")
  240. {
  241. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  242. BodyInterface &bi = c.GetBodyInterface();
  243. const float cDensity = 1234.0f;
  244. const Vec3 cBoxExtent(2.0f, 4.0f, 6.0f);
  245. const float cExpectedMass = cBoxExtent.GetX() * cBoxExtent.GetY() * cBoxExtent.GetZ() * cDensity;
  246. // See: https://en.wikipedia.org/wiki/List_of_moments_of_inertia
  247. const Vec3 cSquaredExtents = Vec3(Square(cBoxExtent.GetY()) + Square(cBoxExtent.GetZ()), Square(cBoxExtent.GetX()) + Square(cBoxExtent.GetZ()), Square(cBoxExtent.GetX()) + Square(cBoxExtent.GetY()));
  248. const Vec3 cExpectedInertiaDiagonal = cExpectedMass / 12.0f * cSquaredExtents;
  249. Ref<BoxShapeSettings> shape_settings = new BoxShapeSettings(0.5f * cBoxExtent);
  250. shape_settings->SetDensity(cDensity);
  251. BodyCreationSettings body_settings(shape_settings, Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING);
  252. // Create body as is
  253. Body &b1 = *bi.CreateBody(body_settings);
  254. CHECK_APPROX_EQUAL(b1.GetMotionProperties()->GetInverseMass(), 1.0f / cExpectedMass);
  255. CHECK_APPROX_EQUAL(b1.GetMotionProperties()->GetInertiaRotation(), Quat::sIdentity());
  256. CHECK_APPROX_EQUAL(b1.GetMotionProperties()->GetInverseInertiaDiagonal(), cExpectedInertiaDiagonal.Reciprocal());
  257. // Override only the mass
  258. const float cOverriddenMass = 13.0f;
  259. const Vec3 cOverriddenMassInertiaDiagonal = cOverriddenMass / 12.0f * cSquaredExtents;
  260. body_settings.mOverrideMassProperties = EOverrideMassProperties::CalculateInertia;
  261. body_settings.mMassPropertiesOverride.mMass = cOverriddenMass;
  262. Body &b2 = *bi.CreateBody(body_settings);
  263. CHECK_APPROX_EQUAL(b2.GetMotionProperties()->GetInverseMass(), 1.0f / cOverriddenMass);
  264. CHECK_APPROX_EQUAL(b2.GetMotionProperties()->GetInertiaRotation(), Quat::sIdentity());
  265. CHECK_APPROX_EQUAL(b2.GetMotionProperties()->GetInverseInertiaDiagonal(), cOverriddenMassInertiaDiagonal.Reciprocal());
  266. // Override both the mass and inertia
  267. const Vec3 cOverriddenInertiaDiagonal(3.0f, 2.0f, 1.0f); // From big to small so that MassProperties::DecomposePrincipalMomentsOfInertia returns the same rotation as we put in
  268. const Quat cOverriddenInertiaRotation = Quat::sRotation(Vec3(1, 1, 1).Normalized(), 0.1f * JPH_PI);
  269. body_settings.mOverrideMassProperties = EOverrideMassProperties::MassAndInertiaProvided;
  270. body_settings.mMassPropertiesOverride.mInertia = Mat44::sRotation(cOverriddenInertiaRotation) * Mat44::sScale(cOverriddenInertiaDiagonal) * Mat44::sRotation(cOverriddenInertiaRotation.Inversed());
  271. Body &b3 = *bi.CreateBody(body_settings);
  272. CHECK_APPROX_EQUAL(b3.GetMotionProperties()->GetInverseMass(), 1.0f / cOverriddenMass);
  273. CHECK_APPROX_EQUAL(b3.GetMotionProperties()->GetInertiaRotation(), cOverriddenInertiaRotation);
  274. CHECK_APPROX_EQUAL(b3.GetMotionProperties()->GetInverseInertiaDiagonal(), cOverriddenInertiaDiagonal.Reciprocal());
  275. }
  276. // Test a box free falling under gravity
  277. static void TestPhysicsFreeFall(PhysicsTestContext &ioContext)
  278. {
  279. const Vec3 cInitialPos(0.0f, 10.0f, 0.0f);
  280. const float cSimulationTime = 2.0f;
  281. // Create box
  282. Body &body = ioContext.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  283. CHECK_APPROX_EQUAL(cInitialPos, body.GetPosition());
  284. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetLinearVelocity());
  285. ioContext.Simulate(cSimulationTime);
  286. // Test resulting velocity (due to gravity)
  287. CHECK_APPROX_EQUAL(cSimulationTime * cGravity, body.GetLinearVelocity(), 1.0e-4f);
  288. // Test resulting position
  289. Vec3 expected_pos = ioContext.PredictPosition(cInitialPos, Vec3::sZero(), cGravity, cSimulationTime);
  290. CHECK_APPROX_EQUAL(expected_pos, body.GetPosition());
  291. }
  292. TEST_CASE("TestPhysicsFreeFall")
  293. {
  294. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  295. TestPhysicsFreeFall(c);
  296. }
  297. TEST_CASE("TestPhysicsFreeFallSubStep")
  298. {
  299. PhysicsTestContext c1(2.0f / 60.0f, 1, 2);
  300. TestPhysicsFreeFall(c1);
  301. PhysicsTestContext c2(4.0f / 60.0f, 1, 4);
  302. TestPhysicsFreeFall(c2);
  303. PhysicsTestContext c3(4.0f / 60.0f, 2, 2);
  304. TestPhysicsFreeFall(c3);
  305. PhysicsTestContext c4(2.0f / 60.0f, 2, 1);
  306. TestPhysicsFreeFall(c4);
  307. PhysicsTestContext c5(8.0f / 60.0f, 4, 2);
  308. TestPhysicsFreeFall(c5);
  309. PhysicsTestContext c6(4.0f / 60.0f, 4, 1);
  310. TestPhysicsFreeFall(c6);
  311. }
  312. // Test acceleration of a box with force applied
  313. static void TestPhysicsApplyForce(PhysicsTestContext &ioContext)
  314. {
  315. const Vec3 cInitialPos(0.0f, 10.0f, 0.0f);
  316. const Vec3 cAcceleration(2.0f, 0.0f, 0.0f);
  317. const float cSimulationTime = 2.0f;
  318. // Create box
  319. Body &body = ioContext.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  320. CHECK_APPROX_EQUAL(cInitialPos, body.GetPosition());
  321. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetLinearVelocity());
  322. // Validate mass
  323. float mass = Cubed(2.0f) * 1000.0f; // Density * Volume
  324. CHECK_APPROX_EQUAL(1.0f / mass, body.GetMotionProperties()->GetInverseMass());
  325. // Simulate while applying force
  326. ioContext.Simulate(cSimulationTime, [&]() { body.AddForce(mass * cAcceleration); });
  327. // Test resulting velocity (due to gravity and applied force)
  328. CHECK_APPROX_EQUAL(cSimulationTime * (cGravity + cAcceleration), body.GetLinearVelocity(), 1.0e-4f);
  329. // Test resulting position
  330. Vec3 expected_pos = ioContext.PredictPosition(cInitialPos, Vec3::sZero(), cGravity + cAcceleration, cSimulationTime);
  331. CHECK_APPROX_EQUAL(expected_pos, body.GetPosition());
  332. }
  333. TEST_CASE("TestPhysicsApplyForce")
  334. {
  335. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  336. TestPhysicsApplyForce(c);
  337. }
  338. TEST_CASE("TestPhysicsApplyForceSubStep")
  339. {
  340. PhysicsTestContext c1(2.0f / 60.0f, 1, 2);
  341. TestPhysicsApplyForce(c1);
  342. PhysicsTestContext c2(4.0f / 60.0f, 1, 4);
  343. TestPhysicsApplyForce(c2);
  344. PhysicsTestContext c3(4.0f / 60.0f, 2, 2);
  345. TestPhysicsApplyForce(c3);
  346. PhysicsTestContext c4(2.0f / 60.0f, 2, 1);
  347. TestPhysicsApplyForce(c4);
  348. PhysicsTestContext c5(8.0f / 60.0f, 4, 2);
  349. TestPhysicsApplyForce(c5);
  350. PhysicsTestContext c6(4.0f / 60.0f, 4, 1);
  351. TestPhysicsApplyForce(c6);
  352. }
  353. // Test angular accelartion for a box by applying torque every frame
  354. static void TestPhysicsApplyTorque(PhysicsTestContext &ioContext)
  355. {
  356. const Vec3 cInitialPos(0.0f, 10.0f, 0.0f);
  357. const Vec3 cAngularAcceleration(0.0f, 2.0f, 0.0f);
  358. const float cSimulationTime = 2.0f;
  359. // Create box
  360. Body &body = ioContext.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  361. CHECK_APPROX_EQUAL(Quat::sIdentity(), body.GetRotation());
  362. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetAngularVelocity());
  363. // Validate mass and inertia
  364. constexpr float mass = Cubed(2.0f) * 1000.0f; // Density * Volume
  365. CHECK_APPROX_EQUAL(1.0f / mass, body.GetMotionProperties()->GetInverseMass());
  366. constexpr float inertia = mass * 8.0f / 12.0f; // See: https://en.wikipedia.org/wiki/List_of_moments_of_inertia
  367. CHECK_APPROX_EQUAL(Mat44::sScale(1.0f / inertia), body.GetMotionProperties()->GetLocalSpaceInverseInertia());
  368. // Simulate while applying torque
  369. ioContext.Simulate(cSimulationTime, [&]() { body.AddTorque(inertia * cAngularAcceleration); });
  370. // Get resulting angular velocity
  371. CHECK_APPROX_EQUAL(cSimulationTime * cAngularAcceleration, body.GetAngularVelocity(), 1.0e-4f);
  372. // Test resulting rotation
  373. Quat expected_rot = ioContext.PredictOrientation(Quat::sIdentity(), Vec3::sZero(), cAngularAcceleration, cSimulationTime);
  374. CHECK_APPROX_EQUAL(expected_rot, body.GetRotation(), 1.0e-4f);
  375. }
  376. TEST_CASE("TestPhysicsApplyTorque")
  377. {
  378. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  379. TestPhysicsApplyTorque(c);
  380. }
  381. TEST_CASE("TestPhysicsApplyTorqueSubStep")
  382. {
  383. PhysicsTestContext c1(2.0f / 60.0f, 1, 2);
  384. TestPhysicsApplyTorque(c1);
  385. PhysicsTestContext c2(4.0f / 60.0f, 1, 4);
  386. TestPhysicsApplyTorque(c2);
  387. PhysicsTestContext c3(4.0f / 60.0f, 2, 2);
  388. TestPhysicsApplyTorque(c3);
  389. PhysicsTestContext c4(2.0f / 60.0f, 2, 1);
  390. TestPhysicsApplyTorque(c4);
  391. PhysicsTestContext c5(8.0f / 60.0f, 4, 2);
  392. TestPhysicsApplyTorque(c5);
  393. PhysicsTestContext c6(4.0f / 60.0f, 4, 1);
  394. TestPhysicsApplyTorque(c6);
  395. }
  396. // Let a sphere bounce on the floor with restition = 1
  397. static void TestPhysicsCollisionElastic(PhysicsTestContext &ioContext)
  398. {
  399. const float cSimulationTime = 1.0f;
  400. const Vec3 cDistanceTraveled = ioContext.PredictPosition(Vec3::sZero(), Vec3::sZero(), cGravity, cSimulationTime);
  401. const float cFloorHitEpsilon = 1.0e-4f; // Apply epsilon so that we're sure that the collision algorithm will find a collision
  402. const Vec3 cFloorHitPos(0.0f, 1.0f - cFloorHitEpsilon, 0.0f); // Sphere with radius 1 will hit floor when 1 above the floor
  403. const Vec3 cInitialPos = cFloorHitPos - cDistanceTraveled;
  404. // Create sphere
  405. ioContext.CreateFloor();
  406. Body &body = ioContext.CreateSphere(cInitialPos, 1.0f, EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING);
  407. body.SetRestitution(1.0f);
  408. // Simulate until at floor
  409. ioContext.Simulate(cSimulationTime);
  410. CHECK_APPROX_EQUAL(cFloorHitPos, body.GetPosition());
  411. // Assert collision not yet processed
  412. CHECK_APPROX_EQUAL(cSimulationTime * cGravity, body.GetLinearVelocity(), 1.0e-4f);
  413. // Simulate one more step to process the collision
  414. ioContext.Simulate(ioContext.GetDeltaTime());
  415. // Assert that collision is processed and velocity is reversed (which is required for a fully elastic collision).
  416. // Note that the physics engine will first apply gravity for the time step and then do collision detection,
  417. // hence the reflected velocity is actually 1 sub-step times gravity bigger than it would be in reality
  418. // For the remainder of cDeltaTime normal gravity will be applied
  419. float sub_step_delta_time = ioContext.GetSubStepDeltaTime();
  420. float remaining_step_time = ioContext.GetDeltaTime() - ioContext.GetSubStepDeltaTime();
  421. Vec3 reflected_velocity_after_sub_step = -(cSimulationTime + sub_step_delta_time) * cGravity;
  422. Vec3 reflected_velocity_after_full_step = reflected_velocity_after_sub_step + remaining_step_time * cGravity;
  423. CHECK_APPROX_EQUAL(reflected_velocity_after_full_step, body.GetLinearVelocity(), 1.0e-4f);
  424. // Body should have bounced back
  425. Vec3 pos_after_bounce_sub_step = cFloorHitPos + reflected_velocity_after_sub_step * sub_step_delta_time;
  426. Vec3 pos_after_bounce_full_step = ioContext.PredictPosition(pos_after_bounce_sub_step, reflected_velocity_after_sub_step, cGravity, remaining_step_time);
  427. CHECK_APPROX_EQUAL(pos_after_bounce_full_step, body.GetPosition());
  428. // Simulate same time, with a fully elastic body we should reach the initial position again
  429. // In our physics engine because of the velocity being too big we actually end up a bit higher than our initial position
  430. Vec3 expected_pos = ioContext.PredictPosition(pos_after_bounce_full_step, reflected_velocity_after_full_step, cGravity, cSimulationTime);
  431. ioContext.Simulate(cSimulationTime);
  432. CHECK_APPROX_EQUAL(expected_pos, body.GetPosition(), 1.0e-4f);
  433. CHECK(expected_pos.GetY() >= cInitialPos.GetY());
  434. }
  435. TEST_CASE("TestPhysicsCollisionElastic")
  436. {
  437. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  438. TestPhysicsCollisionElastic(c);
  439. }
  440. TEST_CASE("TestPhysicsCollisionElasticSubStep")
  441. {
  442. PhysicsTestContext c1(2.0f / 60.0f, 1, 2);
  443. TestPhysicsCollisionElastic(c1);
  444. PhysicsTestContext c2(4.0f / 60.0f, 1, 4);
  445. TestPhysicsCollisionElastic(c2);
  446. PhysicsTestContext c3(4.0f / 60.0f, 2, 2);
  447. TestPhysicsCollisionElastic(c3);
  448. PhysicsTestContext c4(2.0f / 60.0f, 2, 1);
  449. TestPhysicsCollisionElastic(c4);
  450. PhysicsTestContext c5(4.0f / 60.0f, 4, 1);
  451. TestPhysicsCollisionElastic(c5);
  452. }
  453. // Let a sphere bounce on the floor with restitution = 0
  454. static void TestPhysicsCollisionInelastic(PhysicsTestContext &ioContext)
  455. {
  456. const float cSimulationTime = 1.0f;
  457. const Vec3 cDistanceTraveled = ioContext.PredictPosition(Vec3::sZero(), Vec3::sZero(), cGravity, cSimulationTime);
  458. const float cFloorHitEpsilon = 1.0e-4f; // Apply epsilon so that we're sure that the collision algorithm will find a collision
  459. const Vec3 cFloorHitPos(0.0f, 1.0f - cFloorHitEpsilon, 0.0f); // Sphere with radius 1 will hit floor when 1 above the floor
  460. const Vec3 cInitialPos = cFloorHitPos - cDistanceTraveled;
  461. // Create sphere
  462. ioContext.CreateFloor();
  463. Body &body = ioContext.CreateSphere(cInitialPos, 1.0f, EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING);
  464. body.SetRestitution(0.0f);
  465. // Simulate until at floor
  466. ioContext.Simulate(cSimulationTime);
  467. CHECK_APPROX_EQUAL(cFloorHitPos, body.GetPosition());
  468. // Assert collision not yet processed
  469. CHECK_APPROX_EQUAL(cSimulationTime * cGravity, body.GetLinearVelocity(), 1.0e-4f);
  470. // Simulate one more step to process the collision
  471. ioContext.Simulate(ioContext.GetDeltaTime());
  472. // Assert that all velocity was lost in the collision
  473. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetLinearVelocity(), 1.0e-4f);
  474. // Assert that we're on the floor
  475. CHECK_APPROX_EQUAL(cFloorHitPos, body.GetPosition(), 1.0e-4f);
  476. // Simulate some more to validate that we remain on the floor
  477. ioContext.Simulate(cSimulationTime);
  478. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetLinearVelocity(), 1.0e-4f);
  479. CHECK_APPROX_EQUAL(cFloorHitPos, body.GetPosition(), 1.0e-4f);
  480. }
  481. TEST_CASE("TestPhysicsCollisionInelastic")
  482. {
  483. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  484. TestPhysicsCollisionInelastic(c);
  485. }
  486. TEST_CASE("TestPhysicsCollisionInelasticSubStep")
  487. {
  488. PhysicsTestContext c1(2.0f / 60.0f, 1, 2);
  489. TestPhysicsCollisionInelastic(c1);
  490. PhysicsTestContext c2(4.0f / 60.0f, 1, 4);
  491. TestPhysicsCollisionInelastic(c2);
  492. PhysicsTestContext c3(4.0f / 60.0f, 2, 2);
  493. TestPhysicsCollisionInelastic(c3);
  494. PhysicsTestContext c4(2.0f / 60.0f, 2, 1);
  495. TestPhysicsCollisionInelastic(c4);
  496. PhysicsTestContext c5(4.0f / 60.0f, 4, 1);
  497. TestPhysicsCollisionInelastic(c5);
  498. }
  499. // Let box intersect with floor by cPenetrationSlop. It should not move, this is the maximum penetration allowed.
  500. static void TestPhysicsPenetrationSlop1(PhysicsTestContext &ioContext)
  501. {
  502. const float cPenetrationSlop = ioContext.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  503. const float cSimulationTime = 1.0f;
  504. const Vec3 cInitialPos(0.0f, 1.0f - cPenetrationSlop, 0.0f);
  505. // Create box, penetrating with floor
  506. ioContext.CreateFloor();
  507. Body &body = ioContext.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  508. // Simulate
  509. ioContext.Simulate(cSimulationTime);
  510. // Test slop not resolved
  511. CHECK_APPROX_EQUAL(cInitialPos, body.GetPosition(), 1.0e-5f);
  512. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetLinearVelocity());
  513. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetAngularVelocity());
  514. }
  515. TEST_CASE("TestPhysicsPenetrationSlop1")
  516. {
  517. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  518. TestPhysicsPenetrationSlop1(c);
  519. }
  520. TEST_CASE("TestPhysicsPenetrationSlop1SubStep")
  521. {
  522. PhysicsTestContext c(1.0f / 30.0f, 1, 2);
  523. TestPhysicsPenetrationSlop1(c);
  524. PhysicsTestContext c2(1.0f / 30.0f, 2, 1);
  525. TestPhysicsPenetrationSlop1(c2);
  526. }
  527. // Let box intersect with floor with more than cPenetrationSlop. It should be resolved by SolvePositionConstraint until interpenetration is cPenetrationSlop.
  528. static void TestPhysicsPenetrationSlop2(PhysicsTestContext &ioContext)
  529. {
  530. const float cPenetrationSlop = ioContext.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  531. const float cSimulationTime = 1.0f;
  532. const Vec3 cInitialPos(0.0f, 1.0f - 2.0f * cPenetrationSlop, 0.0f);
  533. const Vec3 cFinalPos(0.0f, 1.0f - cPenetrationSlop, 0.0f);
  534. // Create box, penetrating with floor
  535. ioContext.CreateFloor();
  536. Body &body = ioContext.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  537. // Simulate
  538. ioContext.Simulate(cSimulationTime);
  539. // Test resolved until slop
  540. CHECK_APPROX_EQUAL(cFinalPos, body.GetPosition(), 1.0e-5f);
  541. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetLinearVelocity());
  542. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetAngularVelocity());
  543. }
  544. TEST_CASE("TestPhysicsPenetrationSlop2")
  545. {
  546. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  547. TestPhysicsPenetrationSlop2(c);
  548. }
  549. TEST_CASE("TestPhysicsPenetrationSlop2SubStep")
  550. {
  551. PhysicsTestContext c(1.0f / 30.0f, 1, 2);
  552. TestPhysicsPenetrationSlop2(c);
  553. PhysicsTestContext c2(1.0f / 30.0f, 2, 1);
  554. TestPhysicsPenetrationSlop2(c2);
  555. }
  556. // Let box intersect with floor with less than cPenetrationSlop. Body should not move because SolveVelocityConstraint should reset velocity.
  557. static void TestPhysicsPenetrationSlop3(PhysicsTestContext &ioContext)
  558. {
  559. const float cPenetrationSlop = ioContext.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  560. const float cSimulationTime = 1.0f;
  561. const Vec3 cInitialPos(0.0f, 1.0f - 0.1f * cPenetrationSlop, 0.0f);
  562. // Create box, penetrating with floor
  563. ioContext.CreateFloor();
  564. Body &body = ioContext.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  565. // Simulate
  566. ioContext.Simulate(cSimulationTime);
  567. // Test body remained static
  568. CHECK_APPROX_EQUAL(cInitialPos, body.GetPosition(), 1.0e-5f);
  569. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetLinearVelocity());
  570. CHECK_APPROX_EQUAL(Vec3::sZero(), body.GetAngularVelocity());
  571. }
  572. TEST_CASE("TestPhysicsPenetrationSlop3")
  573. {
  574. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  575. TestPhysicsPenetrationSlop3(c);
  576. }
  577. TEST_CASE("TestPhysicsPenetrationSlop3SubStep")
  578. {
  579. PhysicsTestContext c(1.0f / 30.0f, 1, 2);
  580. TestPhysicsPenetrationSlop3(c);
  581. PhysicsTestContext c2(1.0f / 30.0f, 2, 1);
  582. TestPhysicsPenetrationSlop3(c2);
  583. }
  584. TEST_CASE("TestPhysicsOutsideOfSpeculativeContactDistance")
  585. {
  586. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  587. Body &floor = c.CreateFloor();
  588. c.ZeroGravity();
  589. LoggingContactListener contact_listener;
  590. c.GetSystem()->SetContactListener(&contact_listener);
  591. // Create a box and a sphere just outside the speculative contact distance
  592. const float cSpeculativeContactDistance = c.GetSystem()->GetPhysicsSettings().mSpeculativeContactDistance;
  593. const float cDistanceAboveFloor = 1.1f * cSpeculativeContactDistance;
  594. const Vec3 cInitialPosBox(0, 1.0f + cDistanceAboveFloor, 0.0f);
  595. const Vec3 cInitialPosSphere = cInitialPosBox + Vec3(5, 0, 0);
  596. // Make it move 1 m per step down
  597. const Vec3 cVelocity(0, -1.0f / c.GetDeltaTime(), 0);
  598. Body &box = c.CreateBox(cInitialPosBox, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  599. box.SetLinearVelocity(cVelocity);
  600. Body &sphere = c.CreateSphere(cInitialPosSphere, 1.0f, EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING);
  601. sphere.SetLinearVelocity(cVelocity);
  602. // Simulate a step
  603. c.SimulateSingleStep();
  604. // Check that it is now penetrating the floor (collision should not have been detected as it is a discrete body and there was no collision initially)
  605. CHECK(contact_listener.GetEntryCount() == 0);
  606. CHECK_APPROX_EQUAL(box.GetPosition(), cInitialPosBox + cVelocity * c.GetDeltaTime());
  607. CHECK_APPROX_EQUAL(sphere.GetPosition(), cInitialPosSphere + cVelocity * c.GetDeltaTime());
  608. // Simulate a step
  609. c.SimulateSingleStep();
  610. // Check that the contacts are detected now
  611. CHECK(contact_listener.GetEntryCount() == 4); // 2 validates and 2 contacts
  612. CHECK(contact_listener.Contains(LoggingContactListener::EType::Validate, box.GetID(), floor.GetID()));
  613. CHECK(contact_listener.Contains(LoggingContactListener::EType::Add, box.GetID(), floor.GetID()));
  614. CHECK(contact_listener.Contains(LoggingContactListener::EType::Validate, sphere.GetID(), floor.GetID()));
  615. CHECK(contact_listener.Contains(LoggingContactListener::EType::Add, sphere.GetID(), floor.GetID()));
  616. }
  617. TEST_CASE("TestPhysicsInsideSpeculativeContactDistanceNoRestitution")
  618. {
  619. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  620. Body &floor = c.CreateFloor();
  621. c.ZeroGravity();
  622. LoggingContactListener contact_listener;
  623. c.GetSystem()->SetContactListener(&contact_listener);
  624. // Create a box and a sphere just inside the speculative contact distance
  625. const float cSpeculativeContactDistance = c.GetSystem()->GetPhysicsSettings().mSpeculativeContactDistance;
  626. const float cDistanceAboveFloor = 0.9f * cSpeculativeContactDistance;
  627. const Vec3 cInitialPosBox(0, 1.0f + cDistanceAboveFloor, 0.0f);
  628. const Vec3 cInitialPosSphere = cInitialPosBox + Vec3(5, 0, 0);
  629. // Make it move 1 m per step down
  630. const Vec3 cVelocity(0, -1.0f / c.GetDeltaTime(), 0);
  631. Body &box = c.CreateBox(cInitialPosBox, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  632. box.SetLinearVelocity(cVelocity);
  633. Body &sphere = c.CreateSphere(cInitialPosSphere, 1.0f, EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING);
  634. sphere.SetLinearVelocity(cVelocity);
  635. // Simulate a step
  636. c.SimulateSingleStep();
  637. // Check that it is now on the floor and that 2 collisions have been detected
  638. CHECK(contact_listener.GetEntryCount() == 4); // 2 validates and 2 contacts
  639. CHECK(contact_listener.Contains(LoggingContactListener::EType::Validate, box.GetID(), floor.GetID()));
  640. CHECK(contact_listener.Contains(LoggingContactListener::EType::Add, box.GetID(), floor.GetID()));
  641. CHECK(contact_listener.Contains(LoggingContactListener::EType::Validate, sphere.GetID(), floor.GetID()));
  642. CHECK(contact_listener.Contains(LoggingContactListener::EType::Add, sphere.GetID(), floor.GetID()));
  643. contact_listener.Clear();
  644. // Velocity should have been reduced to exactly hit the floor in this step
  645. const Vec3 cExpectedVelocity(0, -cDistanceAboveFloor / c.GetDeltaTime(), 0);
  646. // Box collision is less accurate than sphere as it hits with 4 corners so there's some floating point precision loss in the calculation
  647. CHECK_APPROX_EQUAL(box.GetPosition(), Vec3(0, 1, 0), 1.0e-3f);
  648. CHECK_APPROX_EQUAL(box.GetLinearVelocity(), cExpectedVelocity, 0.05f);
  649. CHECK_APPROX_EQUAL(box.GetAngularVelocity(), Vec3::sZero(), 1.0e-2f);
  650. // Sphere has only 1 contact point so is much more accurate
  651. CHECK_APPROX_EQUAL(sphere.GetPosition(), Vec3(5, 1, 0));
  652. CHECK_APPROX_EQUAL(sphere.GetLinearVelocity(), cExpectedVelocity, 1.0e-4f);
  653. CHECK_APPROX_EQUAL(sphere.GetAngularVelocity(), Vec3::sZero(), 1.0e-4f);
  654. // Simulate a step
  655. c.SimulateSingleStep();
  656. // Check that the contacts persisted
  657. CHECK(contact_listener.GetEntryCount() >= 2); // 2 persist and possibly 2 validates depending on if the cache got reused
  658. CHECK(contact_listener.Contains(LoggingContactListener::EType::Persist, box.GetID(), floor.GetID()));
  659. CHECK(contact_listener.Contains(LoggingContactListener::EType::Persist, sphere.GetID(), floor.GetID()));
  660. // Box should have come to rest
  661. CHECK_APPROX_EQUAL(box.GetPosition(), Vec3(0, 1, 0), 1.0e-3f);
  662. CHECK_APPROX_EQUAL(box.GetLinearVelocity(), Vec3::sZero(), 0.05f);
  663. CHECK_APPROX_EQUAL(box.GetAngularVelocity(), Vec3::sZero(), 1.0e-2f);
  664. // Sphere should have come to rest
  665. CHECK_APPROX_EQUAL(sphere.GetPosition(), Vec3(5, 1, 0), 1.0e-4f);
  666. CHECK_APPROX_EQUAL(sphere.GetLinearVelocity(), Vec3::sZero(), 1.0e-4f);
  667. CHECK_APPROX_EQUAL(sphere.GetAngularVelocity(), Vec3::sZero(), 1.0e-4f);
  668. }
  669. TEST_CASE("TestPhysicsInsideSpeculativeContactDistanceWithRestitution")
  670. {
  671. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  672. Body &floor = c.CreateFloor();
  673. c.ZeroGravity();
  674. LoggingContactListener contact_listener;
  675. c.GetSystem()->SetContactListener(&contact_listener);
  676. // Create a box and a sphere just inside the speculative contact distance
  677. const float cSpeculativeContactDistance = c.GetSystem()->GetPhysicsSettings().mSpeculativeContactDistance;
  678. const float cDistanceAboveFloor = 0.9f * cSpeculativeContactDistance;
  679. const Vec3 cInitialPosBox(0, 1.0f + cDistanceAboveFloor, 0.0f);
  680. const Vec3 cInitialPosSphere = cInitialPosBox + Vec3(5, 0, 0);
  681. // Make it move 1 m per step down
  682. const Vec3 cVelocity(0, -1.0f / c.GetDeltaTime(), 0);
  683. Body &box = c.CreateBox(cInitialPosBox, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  684. box.SetLinearVelocity(cVelocity);
  685. box.SetRestitution(1.0f);
  686. Body &sphere = c.CreateSphere(cInitialPosSphere, 1.0f, EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING);
  687. sphere.SetLinearVelocity(cVelocity);
  688. sphere.SetRestitution(1.0f);
  689. // Simulate a step
  690. c.SimulateSingleStep();
  691. // Check that it has triggered contact points and has bounced from it's initial position (effectively travelling the extra distance to the floor and back for free)
  692. CHECK(contact_listener.GetEntryCount() == 4); // 2 validates and 2 contacts
  693. CHECK(contact_listener.Contains(LoggingContactListener::EType::Validate, box.GetID(), floor.GetID()));
  694. CHECK(contact_listener.Contains(LoggingContactListener::EType::Add, box.GetID(), floor.GetID()));
  695. CHECK(contact_listener.Contains(LoggingContactListener::EType::Validate, sphere.GetID(), floor.GetID()));
  696. CHECK(contact_listener.Contains(LoggingContactListener::EType::Add, sphere.GetID(), floor.GetID()));
  697. contact_listener.Clear();
  698. // Box collision is less accurate than sphere as it hits with 4 corners so there's some floating point precision loss in the calculation
  699. CHECK_APPROX_EQUAL(box.GetPosition(), cInitialPosBox - cVelocity * c.GetDeltaTime(), 0.01f);
  700. CHECK_APPROX_EQUAL(box.GetLinearVelocity(), -cVelocity, 0.1f);
  701. CHECK_APPROX_EQUAL(box.GetAngularVelocity(), Vec3::sZero(), 0.02f);
  702. // Sphere has only 1 contact point so is much more accurate
  703. CHECK_APPROX_EQUAL(sphere.GetPosition(), cInitialPosSphere - cVelocity * c.GetDeltaTime(), 1.0e-5f);
  704. CHECK_APPROX_EQUAL(sphere.GetLinearVelocity(), -cVelocity, 2.0e-4f);
  705. CHECK_APPROX_EQUAL(sphere.GetAngularVelocity(), Vec3::sZero(), 2.0e-4f);
  706. // Simulate a step
  707. c.SimulateSingleStep();
  708. // Check that all contact points are removed
  709. CHECK(contact_listener.GetEntryCount() == 2); // 2 removes
  710. CHECK(contact_listener.Contains(LoggingContactListener::EType::Remove, box.GetID(), floor.GetID()));
  711. CHECK(contact_listener.Contains(LoggingContactListener::EType::Remove, sphere.GetID(), floor.GetID()));
  712. }
  713. TEST_CASE("TestPhysicsInsideSpeculativeContactDistanceMovingAway")
  714. {
  715. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  716. Body &floor = c.CreateFloor();
  717. c.ZeroGravity();
  718. LoggingContactListener contact_listener;
  719. c.GetSystem()->SetContactListener(&contact_listener);
  720. // Create a box and a sphere just inside the speculative contact distance
  721. const float cSpeculativeContactDistance = c.GetSystem()->GetPhysicsSettings().mSpeculativeContactDistance;
  722. const float cDistanceAboveFloor = 0.9f * cSpeculativeContactDistance;
  723. const Vec3 cInitialPosBox(0, 1.0f + cDistanceAboveFloor, 0.0f);
  724. const Vec3 cInitialPosSphere = cInitialPosBox + Vec3(5, 0, 0);
  725. // Make it move 1 m per step up
  726. const Vec3 cVelocity(0, 1.0f / c.GetDeltaTime(), 0);
  727. Body &box = c.CreateBox(cInitialPosBox, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  728. box.SetLinearVelocity(cVelocity);
  729. box.SetRestitution(1.0f);
  730. Body &sphere = c.CreateSphere(cInitialPosSphere, 1.0f, EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING);
  731. sphere.SetLinearVelocity(cVelocity);
  732. sphere.SetRestitution(1.0f);
  733. // Simulate a step
  734. c.SimulateSingleStep();
  735. // Check that it has triggered contact points (note that this is wrong since the object never touched the floor but that's the downside of the speculative contacts -> you'll get an incorrect collision callback)
  736. CHECK(contact_listener.GetEntryCount() == 4); // 2 validates and 2 contacts
  737. CHECK(contact_listener.Contains(LoggingContactListener::EType::Validate, box.GetID(), floor.GetID()));
  738. CHECK(contact_listener.Contains(LoggingContactListener::EType::Add, box.GetID(), floor.GetID()));
  739. CHECK(contact_listener.Contains(LoggingContactListener::EType::Validate, sphere.GetID(), floor.GetID()));
  740. CHECK(contact_listener.Contains(LoggingContactListener::EType::Add, sphere.GetID(), floor.GetID()));
  741. contact_listener.Clear();
  742. // Box should have moved unimpeded
  743. CHECK_APPROX_EQUAL(box.GetPosition(), cInitialPosBox + cVelocity * c.GetDeltaTime());
  744. CHECK_APPROX_EQUAL(box.GetLinearVelocity(), cVelocity);
  745. CHECK_APPROX_EQUAL(box.GetAngularVelocity(), Vec3::sZero());
  746. // Sphere should have moved unimpeded
  747. CHECK_APPROX_EQUAL(sphere.GetPosition(), cInitialPosSphere + cVelocity * c.GetDeltaTime());
  748. CHECK_APPROX_EQUAL(sphere.GetLinearVelocity(), cVelocity);
  749. CHECK_APPROX_EQUAL(sphere.GetAngularVelocity(), Vec3::sZero());
  750. // Simulate a step
  751. c.SimulateSingleStep();
  752. // Check that all contact points are removed
  753. CHECK(contact_listener.GetEntryCount() == 2); // 2 removes
  754. CHECK(contact_listener.Contains(LoggingContactListener::EType::Remove, box.GetID(), floor.GetID()));
  755. CHECK(contact_listener.Contains(LoggingContactListener::EType::Remove, sphere.GetID(), floor.GetID()));
  756. }
  757. static void TestPhysicsActivationDeactivation(PhysicsTestContext &ioContext)
  758. {
  759. const float cPenetrationSlop = ioContext.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  760. // Install activation listener
  761. LoggingBodyActivationListener activation_listener;
  762. ioContext.GetSystem()->SetBodyActivationListener(&activation_listener);
  763. // Create floor
  764. Body &floor = ioContext.CreateBox(Vec3(0, -1, 0), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(100, 1, 100));
  765. CHECK(!floor.IsActive());
  766. // Create inactive box
  767. Body &box = ioContext.CreateBox(Vec3(0, 5, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f), EActivation::DontActivate);
  768. CHECK(!box.IsActive());
  769. CHECK(activation_listener.GetEntryCount() == 0);
  770. // Box should not activate by itself
  771. ioContext.Simulate(1.0f);
  772. CHECK(box.GetPosition() == Vec3(0, 5, 0));
  773. CHECK(!box.IsActive());
  774. CHECK(activation_listener.GetEntryCount() == 0);
  775. // Activate the body and validate it is active now
  776. ioContext.GetBodyInterface().ActivateBody(box.GetID());
  777. CHECK(box.IsActive());
  778. CHECK(box.GetLinearVelocity().IsNearZero());
  779. CHECK(activation_listener.GetEntryCount() == 1);
  780. CHECK(activation_listener.Contains(LoggingBodyActivationListener::EType::Activated, box.GetID()));
  781. activation_listener.Clear();
  782. // Do a single step and check that the body is still active and has gained some velocity
  783. ioContext.SimulateSingleStep();
  784. CHECK(box.IsActive());
  785. CHECK(activation_listener.GetEntryCount() == 0);
  786. CHECK(!box.GetLinearVelocity().IsNearZero());
  787. // Simulate 5 seconds and check it has settled on the floor and is no longer active
  788. ioContext.Simulate(5.0f);
  789. CHECK_APPROX_EQUAL(box.GetPosition(), Vec3(0, 0.5f, 0), 1.1f * cPenetrationSlop);
  790. CHECK_APPROX_EQUAL(box.GetLinearVelocity(), Vec3::sZero());
  791. CHECK_APPROX_EQUAL(box.GetAngularVelocity(), Vec3::sZero());
  792. CHECK(!box.IsActive());
  793. CHECK(activation_listener.GetEntryCount() == 1);
  794. CHECK(activation_listener.Contains(LoggingBodyActivationListener::EType::Deactivated, box.GetID()));
  795. }
  796. TEST_CASE("TestPhysicsActivationDeactivation")
  797. {
  798. PhysicsTestContext c1(1.0f / 60.0f, 1, 1);
  799. TestPhysicsActivationDeactivation(c1);
  800. PhysicsTestContext c2(2.0f / 60.0f, 1, 2);
  801. TestPhysicsActivationDeactivation(c2);
  802. PhysicsTestContext c3(2.0f / 60.0f, 2, 1);
  803. TestPhysicsActivationDeactivation(c3);
  804. PhysicsTestContext c4(4.0f / 60.0f, 4, 1);
  805. TestPhysicsActivationDeactivation(c4);
  806. PhysicsTestContext c5(8.0f / 60.0f, 4, 2);
  807. TestPhysicsActivationDeactivation(c5);
  808. }
  809. // A test that checks that a row of penetrating boxes will all activate and handle collision in 1 frame so that active bodies cannot tunnel through inactive bodies
  810. static void TestPhysicsActivateDuringStep(PhysicsTestContext &ioContext, bool inReverseCreate)
  811. {
  812. const float cPenetrationSlop = ioContext.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  813. const int cNumBodies = 10;
  814. const float cBoxExtent = 0.5f;
  815. PhysicsSystem *system = ioContext.GetSystem();
  816. BodyInterface &bi = ioContext.GetBodyInterface();
  817. LoggingBodyActivationListener activation_listener;
  818. system->SetBodyActivationListener(&activation_listener);
  819. LoggingContactListener contact_listener;
  820. system->SetContactListener(&contact_listener);
  821. // Create a row of penetrating boxes. Since some of the algorithms rely on body index, we create them normally and reversed to test both cases
  822. BodyIDVector body_ids;
  823. if (inReverseCreate)
  824. for (int i = cNumBodies - 1; i >= 0; --i)
  825. body_ids.insert(body_ids.begin(), ioContext.CreateBox(Vec3(i * (2.0f * cBoxExtent - cPenetrationSlop), 0, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(cBoxExtent), EActivation::DontActivate).GetID());
  826. else
  827. for (int i = 0; i < cNumBodies; ++i)
  828. body_ids.push_back(ioContext.CreateBox(Vec3(i * (2.0f * cBoxExtent - cPenetrationSlop), 0, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f), EActivation::DontActivate).GetID());
  829. // Test that nothing is active yet
  830. CHECK(activation_listener.GetEntryCount() == 0);
  831. CHECK(contact_listener.GetEntryCount() == 0);
  832. for (BodyID id : body_ids)
  833. CHECK(!bi.IsActive(id));
  834. // Activate the left most box and give it a velocity that is high enough to make it tunnel through the second box in a single step
  835. bi.SetLinearVelocity(body_ids.front(), Vec3(500, 0, 0));
  836. // Test that only the left most box is active
  837. CHECK(activation_listener.GetEntryCount() == 1);
  838. CHECK(contact_listener.GetEntryCount() == 0);
  839. CHECK(bi.IsActive(body_ids.front()));
  840. CHECK(activation_listener.Contains(LoggingBodyActivationListener::EType::Activated, body_ids.front()));
  841. for (int i = 1; i < cNumBodies; ++i)
  842. CHECK(!bi.IsActive(body_ids[i]));
  843. activation_listener.Clear();
  844. // Step the world
  845. ioContext.SimulateSingleStep();
  846. // Other bodies should now be awake and each body should only collide with its neighbour
  847. CHECK(activation_listener.GetEntryCount() == cNumBodies - 1);
  848. CHECK(contact_listener.GetEntryCount() == 2 * (cNumBodies - 1));
  849. for (int i = 0; i < cNumBodies; ++i)
  850. {
  851. BodyID id = body_ids[i];
  852. // Check body is active
  853. CHECK(bi.IsActive(id));
  854. // Check that body moved to the right
  855. CHECK(bi.GetPosition(id).GetX() > i * (2.0f * cBoxExtent - cPenetrationSlop));
  856. }
  857. for (int i = 1; i < cNumBodies; ++i)
  858. {
  859. BodyID id1 = body_ids[i - 1];
  860. BodyID id2 = body_ids[i];
  861. // Check that we received activation events for each body
  862. CHECK(activation_listener.Contains(LoggingBodyActivationListener::EType::Activated, id2));
  863. // Check that we received a validate and an add for each body pair
  864. int validate = contact_listener.Find(LoggingContactListener::EType::Validate, id1, id2);
  865. CHECK(validate >= 0);
  866. int add = contact_listener.Find(LoggingContactListener::EType::Add, id1, id2);
  867. CHECK(add >= 0);
  868. CHECK(add > validate);
  869. // Check that bodies did not tunnel through each other
  870. CHECK(bi.GetPosition(id1).GetX() < bi.GetPosition(id2).GetX());
  871. }
  872. }
  873. TEST_CASE("TestPhysicsActivateDuringStep")
  874. {
  875. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  876. TestPhysicsActivateDuringStep(c, false);
  877. PhysicsTestContext c2(1.0f / 60.0f, 1, 1);
  878. TestPhysicsActivateDuringStep(c2, true);
  879. }
  880. TEST_CASE("TestPhysicsBroadPhaseLayers")
  881. {
  882. PhysicsTestContext c(1.0f / 60.0f, 1, 1);
  883. BodyInterface &bi = c.GetBodyInterface();
  884. // Reduce slop
  885. PhysicsSettings settings = c.GetSystem()->GetPhysicsSettings();
  886. settings.mPenetrationSlop = 0.0f;
  887. c.GetSystem()->SetPhysicsSettings(settings);
  888. // Create static floor
  889. c.CreateFloor();
  890. // Create MOVING boxes
  891. Body &moving1 = c.CreateBox(Vec3(0, 1, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f), EActivation::Activate);
  892. Body &moving2 = c.CreateBox(Vec3(0, 2, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(0.5f), EActivation::Activate);
  893. // Create HQ_DEBRIS boxes
  894. Body &hq_debris1 = c.CreateBox(Vec3(0, 3, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::HQ_DEBRIS, Vec3::sReplicate(0.5f), EActivation::Activate);
  895. Body &hq_debris2 = c.CreateBox(Vec3(0, 4, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::HQ_DEBRIS, Vec3::sReplicate(0.5f), EActivation::Activate);
  896. // Create LQ_DEBRIS boxes
  897. Body &lq_debris1 = c.CreateBox(Vec3(0, 5, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::LQ_DEBRIS, Vec3::sReplicate(0.5f), EActivation::Activate);
  898. Body &lq_debris2 = c.CreateBox(Vec3(0, 6, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::LQ_DEBRIS, Vec3::sReplicate(0.5f), EActivation::Activate);
  899. // Check layers
  900. CHECK(moving1.GetObjectLayer() == Layers::MOVING);
  901. CHECK(moving2.GetObjectLayer() == Layers::MOVING);
  902. CHECK(hq_debris1.GetObjectLayer() == Layers::HQ_DEBRIS);
  903. CHECK(hq_debris2.GetObjectLayer() == Layers::HQ_DEBRIS);
  904. CHECK(lq_debris1.GetObjectLayer() == Layers::LQ_DEBRIS);
  905. CHECK(lq_debris2.GetObjectLayer() == Layers::LQ_DEBRIS);
  906. CHECK(moving1.GetBroadPhaseLayer() == BroadPhaseLayers::MOVING);
  907. CHECK(moving2.GetBroadPhaseLayer() == BroadPhaseLayers::MOVING);
  908. CHECK(hq_debris1.GetBroadPhaseLayer() == BroadPhaseLayers::MOVING);
  909. CHECK(hq_debris2.GetBroadPhaseLayer() == BroadPhaseLayers::MOVING);
  910. CHECK(lq_debris1.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  911. CHECK(lq_debris2.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  912. // Simulate the boxes falling
  913. c.Simulate(5.0f);
  914. // Everything should sleep
  915. CHECK_FALSE(moving1.IsActive());
  916. CHECK_FALSE(moving2.IsActive());
  917. CHECK_FALSE(hq_debris1.IsActive());
  918. CHECK_FALSE(hq_debris2.IsActive());
  919. CHECK_FALSE(lq_debris1.IsActive());
  920. CHECK_FALSE(lq_debris2.IsActive());
  921. // MOVING boxes should have stacked
  922. float slop = 0.02f;
  923. CHECK_APPROX_EQUAL(moving1.GetPosition(), Vec3(0, 0.5f, 0), slop);
  924. CHECK_APPROX_EQUAL(moving2.GetPosition(), Vec3(0, 1.5f, 0), slop);
  925. // HQ_DEBRIS boxes should have stacked on MOVING boxes but don't collide with each other
  926. CHECK_APPROX_EQUAL(hq_debris1.GetPosition(), Vec3(0, 2.5f, 0), slop);
  927. CHECK_APPROX_EQUAL(hq_debris2.GetPosition(), Vec3(0, 2.5f, 0), slop);
  928. // LQ_DEBRIS should have fallen through all but the floor
  929. CHECK_APPROX_EQUAL(lq_debris1.GetPosition(), Vec3(0, 0.5f, 0), slop);
  930. CHECK_APPROX_EQUAL(lq_debris2.GetPosition(), Vec3(0, 0.5f, 0), slop);
  931. // Now change HQ_DEBRIS to LQ_DEBRIS
  932. bi.SetObjectLayer(hq_debris1.GetID(), Layers::LQ_DEBRIS);
  933. bi.SetObjectLayer(hq_debris2.GetID(), Layers::LQ_DEBRIS);
  934. bi.ActivateBody(hq_debris1.GetID());
  935. bi.ActivateBody(hq_debris2.GetID());
  936. // Check layers
  937. CHECK(moving1.GetObjectLayer() == Layers::MOVING);
  938. CHECK(moving2.GetObjectLayer() == Layers::MOVING);
  939. CHECK(hq_debris1.GetObjectLayer() == Layers::LQ_DEBRIS);
  940. CHECK(hq_debris2.GetObjectLayer() == Layers::LQ_DEBRIS);
  941. CHECK(lq_debris1.GetObjectLayer() == Layers::LQ_DEBRIS);
  942. CHECK(lq_debris2.GetObjectLayer() == Layers::LQ_DEBRIS);
  943. CHECK(moving1.GetBroadPhaseLayer() == BroadPhaseLayers::MOVING);
  944. CHECK(moving2.GetBroadPhaseLayer() == BroadPhaseLayers::MOVING);
  945. CHECK(hq_debris1.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  946. CHECK(hq_debris2.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  947. CHECK(lq_debris1.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  948. CHECK(lq_debris2.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  949. // Simulate again
  950. c.Simulate(5.0f);
  951. // Everything should sleep
  952. CHECK_FALSE(moving1.IsActive());
  953. CHECK_FALSE(moving2.IsActive());
  954. CHECK_FALSE(hq_debris1.IsActive());
  955. CHECK_FALSE(hq_debris2.IsActive());
  956. CHECK_FALSE(lq_debris1.IsActive());
  957. CHECK_FALSE(lq_debris2.IsActive());
  958. // MOVING boxes should have stacked
  959. CHECK_APPROX_EQUAL(moving1.GetPosition(), Vec3(0, 0.5f, 0), slop);
  960. CHECK_APPROX_EQUAL(moving2.GetPosition(), Vec3(0, 1.5f, 0), slop);
  961. // HQ_DEBRIS (now LQ_DEBRIS) boxes have fallen through all but the floor
  962. CHECK_APPROX_EQUAL(hq_debris1.GetPosition(), Vec3(0, 0.5f, 0), slop);
  963. CHECK_APPROX_EQUAL(hq_debris2.GetPosition(), Vec3(0, 0.5f, 0), slop);
  964. // LQ_DEBRIS should have fallen through all but the floor
  965. CHECK_APPROX_EQUAL(lq_debris1.GetPosition(), Vec3(0, 0.5f, 0), slop);
  966. CHECK_APPROX_EQUAL(lq_debris2.GetPosition(), Vec3(0, 0.5f, 0), slop);
  967. // Now change MOVING to HQ_DEBRIS (this doesn't change the broadphase layer so avoids adding/removing bodies)
  968. bi.SetObjectLayer(moving1.GetID(), Layers::HQ_DEBRIS);
  969. bi.SetObjectLayer(moving2.GetID(), Layers::HQ_DEBRIS);
  970. bi.ActivateBody(moving1.GetID());
  971. bi.ActivateBody(moving2.GetID());
  972. // Check layers
  973. CHECK(moving1.GetObjectLayer() == Layers::HQ_DEBRIS);
  974. CHECK(moving2.GetObjectLayer() == Layers::HQ_DEBRIS);
  975. CHECK(hq_debris1.GetObjectLayer() == Layers::LQ_DEBRIS);
  976. CHECK(hq_debris2.GetObjectLayer() == Layers::LQ_DEBRIS);
  977. CHECK(lq_debris1.GetObjectLayer() == Layers::LQ_DEBRIS);
  978. CHECK(lq_debris2.GetObjectLayer() == Layers::LQ_DEBRIS);
  979. CHECK(moving1.GetBroadPhaseLayer() == BroadPhaseLayers::MOVING); // Broadphase layer didn't change
  980. CHECK(moving2.GetBroadPhaseLayer() == BroadPhaseLayers::MOVING);
  981. CHECK(hq_debris1.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  982. CHECK(hq_debris2.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  983. CHECK(lq_debris1.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  984. CHECK(lq_debris2.GetBroadPhaseLayer() == BroadPhaseLayers::LQ_DEBRIS);
  985. // Simulate again
  986. c.Simulate(5.0f);
  987. // Everything should sleep
  988. CHECK_FALSE(moving1.IsActive());
  989. CHECK_FALSE(moving2.IsActive());
  990. CHECK_FALSE(hq_debris1.IsActive());
  991. CHECK_FALSE(hq_debris2.IsActive());
  992. CHECK_FALSE(lq_debris1.IsActive());
  993. CHECK_FALSE(lq_debris2.IsActive());
  994. // MOVING boxes now also fall through
  995. CHECK_APPROX_EQUAL(moving1.GetPosition(), Vec3(0, 0.5f, 0), slop);
  996. CHECK_APPROX_EQUAL(moving2.GetPosition(), Vec3(0, 0.5f, 0), slop);
  997. // HQ_DEBRIS (now LQ_DEBRIS) boxes have fallen through all but the floor
  998. CHECK_APPROX_EQUAL(hq_debris1.GetPosition(), Vec3(0, 0.5f, 0), slop);
  999. CHECK_APPROX_EQUAL(hq_debris2.GetPosition(), Vec3(0, 0.5f, 0), slop);
  1000. // LQ_DEBRIS should have fallen through all but the floor
  1001. CHECK_APPROX_EQUAL(lq_debris1.GetPosition(), Vec3(0, 0.5f, 0), slop);
  1002. CHECK_APPROX_EQUAL(lq_debris2.GetPosition(), Vec3(0, 0.5f, 0), slop);
  1003. }
  1004. }