PhysicsTests.cpp 52 KB

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