MotionQualityLinearCastTests.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "LoggingContactListener.h"
  7. #include "LoggingBodyActivationListener.h"
  8. TEST_SUITE("MotionQualityLinearCastTests")
  9. {
  10. static const float cBoxExtent = 0.5f;
  11. static const float cFrequency = 60.0f;
  12. static const Vec3 cVelocity(2.0f * cFrequency, 0, 0); // High enough velocity to step 2 meters in a single simulation step
  13. static const Vec3 cPos1(-1, 0, 0);
  14. static const Vec3 cPos2(1, 0, 0);
  15. // Two boxes colliding in the center, each has enough velocity to tunnel though in 1 step
  16. TEST_CASE("TestDiscreteBoxVsDiscreteBox")
  17. {
  18. PhysicsTestContext c(1.0f / cFrequency, 1, 1);
  19. c.ZeroGravity();
  20. // Register listener
  21. LoggingContactListener listener;
  22. c.GetSystem()->SetContactListener(&listener);
  23. Body &box1 = c.CreateBox(cPos1, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  24. box1.SetLinearVelocity(cVelocity);
  25. // Test that the inner radius of the box makes sense (used internally by linear cast)
  26. CHECK_APPROX_EQUAL(box1.GetShape()->GetInnerRadius(), cBoxExtent);
  27. Body &box2 = c.CreateBox(cPos2, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  28. box2.SetLinearVelocity(-cVelocity);
  29. c.SimulateSingleStep();
  30. // No collisions should be reported and the bodies should have moved according to their velocity (tunneling through eachother)
  31. CHECK(listener.GetEntryCount() == 0);
  32. CHECK_APPROX_EQUAL(box1.GetPosition(), cPos1 + cVelocity / cFrequency);
  33. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), cVelocity);
  34. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  35. CHECK_APPROX_EQUAL(box2.GetPosition(), cPos2 - cVelocity / cFrequency);
  36. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), -cVelocity);
  37. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  38. }
  39. // Two boxes colliding in the center, each has enough velocity to step over the other in 1 step, restitution = 1
  40. TEST_CASE("TestLinearCastBoxVsLinearCastBoxElastic")
  41. {
  42. PhysicsTestContext c(1.0f / cFrequency, 1, 1);
  43. c.ZeroGravity();
  44. const float cPenetrationSlop = c.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  45. // Register listener
  46. LoggingContactListener listener;
  47. c.GetSystem()->SetContactListener(&listener);
  48. Body &box1 = c.CreateBox(cPos1, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  49. box1.SetLinearVelocity(cVelocity);
  50. box1.SetRestitution(1.0f);
  51. Body &box2 = c.CreateBox(cPos2, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  52. box2.SetLinearVelocity(-cVelocity);
  53. box2.SetRestitution(1.0f);
  54. c.SimulateSingleStep();
  55. // The bodies should have collided and the velocities reversed
  56. CHECK(listener.GetEntryCount() == 2);
  57. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  58. CHECK(listener.Contains(LoggingContactListener::EType::Add, box1.GetID(), box2.GetID()));
  59. CHECK_APPROX_EQUAL(box1.GetPosition(), Vec3(-cBoxExtent, 0, 0), cPenetrationSlop);
  60. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), -cVelocity);
  61. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  62. CHECK_APPROX_EQUAL(box2.GetPosition(), Vec3(cBoxExtent, 0, 0), cPenetrationSlop);
  63. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), cVelocity);
  64. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  65. listener.Clear();
  66. c.SimulateSingleStep();
  67. // In the second step the bodies should have moved away, but since they were initially overlapping we should have a contact persist callback
  68. CHECK(listener.GetEntryCount() == 2);
  69. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  70. CHECK(listener.Contains(LoggingContactListener::EType::Persist, box1.GetID(), box2.GetID()));
  71. CHECK_APPROX_EQUAL(box1.GetPosition(), Vec3(-cBoxExtent, 0, 0) - cVelocity / cFrequency, cPenetrationSlop);
  72. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), -cVelocity);
  73. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  74. CHECK_APPROX_EQUAL(box2.GetPosition(), Vec3(cBoxExtent, 0, 0) + cVelocity / cFrequency, cPenetrationSlop);
  75. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), cVelocity);
  76. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  77. listener.Clear();
  78. c.SimulateSingleStep();
  79. // In the third step the bodies have separated and a contact remove callback should have been received
  80. CHECK(listener.GetEntryCount() == 1);
  81. CHECK(listener.Contains(LoggingContactListener::EType::Remove, box1.GetID(), box2.GetID()));
  82. CHECK_APPROX_EQUAL(box1.GetPosition(), Vec3(-cBoxExtent, 0, 0) - 2.0f * cVelocity / cFrequency, cPenetrationSlop);
  83. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), -cVelocity);
  84. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  85. CHECK_APPROX_EQUAL(box2.GetPosition(), Vec3(cBoxExtent, 0, 0) + 2.0f * cVelocity / cFrequency, cPenetrationSlop);
  86. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), cVelocity);
  87. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  88. }
  89. // Two boxes colliding in the center, each has enough velocity to step over the other in 1 step, restitution = 0
  90. TEST_CASE("TestLinearCastBoxVsLinearCastBoxInelastic")
  91. {
  92. PhysicsTestContext c(1.0f / cFrequency, 1, 1);
  93. c.ZeroGravity();
  94. const float cPenetrationSlop = c.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  95. // Register listener
  96. LoggingContactListener listener;
  97. c.GetSystem()->SetContactListener(&listener);
  98. Body &box1 = c.CreateBox(cPos1, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  99. box1.SetLinearVelocity(cVelocity);
  100. Body &box2 = c.CreateBox(cPos2, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  101. box2.SetLinearVelocity(-cVelocity);
  102. c.SimulateSingleStep();
  103. // The bodies should have collided and both are stopped
  104. CHECK(listener.GetEntryCount() == 2);
  105. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  106. CHECK(listener.Contains(LoggingContactListener::EType::Add, box1.GetID(), box2.GetID()));
  107. CHECK_APPROX_EQUAL(box1.GetPosition(), Vec3(-cBoxExtent, 0, 0), cPenetrationSlop);
  108. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), Vec3::sZero());
  109. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  110. CHECK_APPROX_EQUAL(box2.GetPosition(), Vec3(cBoxExtent, 0, 0), cPenetrationSlop);
  111. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), Vec3::sZero());
  112. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  113. // The bodies should persist to contact as they are not moving
  114. for (int i = 0; i < 10; ++i)
  115. {
  116. listener.Clear();
  117. c.SimulateSingleStep();
  118. if (i == 0)
  119. {
  120. // Only in the first step we will receive a validate callback since after this step the contact cache will be used
  121. CHECK(listener.GetEntryCount() == 2);
  122. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  123. }
  124. else
  125. CHECK(listener.GetEntryCount() == 1);
  126. CHECK(listener.Contains(LoggingContactListener::EType::Persist, box1.GetID(), box2.GetID()));
  127. CHECK_APPROX_EQUAL(box1.GetPosition(), Vec3(-cBoxExtent, 0, 0), cPenetrationSlop);
  128. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), Vec3::sZero());
  129. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  130. CHECK_APPROX_EQUAL(box2.GetPosition(), Vec3(cBoxExtent, 0, 0), cPenetrationSlop);
  131. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), Vec3::sZero());
  132. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  133. }
  134. }
  135. // Two boxes colliding in the center, linear cast vs inactive linear cast
  136. TEST_CASE("TestLinearCastBoxVsInactiveLinearCastBox")
  137. {
  138. PhysicsTestContext c(1.0f / cFrequency, 1, 1);
  139. c.ZeroGravity();
  140. const float cPenetrationSlop = c.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  141. // Register listener
  142. LoggingContactListener listener;
  143. c.GetSystem()->SetContactListener(&listener);
  144. LoggingBodyActivationListener activation;
  145. c.GetSystem()->SetBodyActivationListener(&activation);
  146. Body &box1 = c.CreateBox(cPos1, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  147. box1.SetLinearVelocity(cVelocity);
  148. Body &box2 = c.CreateBox(cPos2, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent), EActivation::DontActivate);
  149. CHECK(!box2.IsActive());
  150. c.SimulateSingleStep();
  151. // The bodies should have collided and body 2 should be activated, have velocity, but not moved in this step
  152. CHECK(listener.GetEntryCount() == 2);
  153. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  154. CHECK(listener.Contains(LoggingContactListener::EType::Add, box1.GetID(), box2.GetID()));
  155. Vec3 new_velocity = 0.5f * cVelocity;
  156. CHECK_APPROX_EQUAL(box1.GetPosition(), cPos2 - Vec3(2.0f * cBoxExtent, 0, 0), cPenetrationSlop);
  157. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), new_velocity);
  158. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  159. CHECK_APPROX_EQUAL(box2.GetPosition(), cPos2);
  160. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), new_velocity);
  161. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  162. CHECK(box2.IsActive());
  163. CHECK(activation.Contains(LoggingBodyActivationListener::EType::Activated, box2.GetID()));
  164. listener.Clear();
  165. c.SimulateSingleStep();
  166. // In the next step body 2 should have started to move
  167. CHECK(listener.GetEntryCount() == 2);
  168. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  169. CHECK(listener.Contains(LoggingContactListener::EType::Persist, box1.GetID(), box2.GetID()));
  170. CHECK_APPROX_EQUAL(box1.GetPosition(), cPos2 - Vec3(2.0f * cBoxExtent, 0, 0) + new_velocity / cFrequency, cPenetrationSlop);
  171. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), new_velocity);
  172. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  173. CHECK_APPROX_EQUAL(box2.GetPosition(), cPos2 + new_velocity / cFrequency);
  174. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), new_velocity);
  175. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  176. }
  177. // Two boxes colliding in the center, linear cast vs inactive discrete
  178. TEST_CASE("TestLinearCastBoxVsInactiveDiscreteBox")
  179. {
  180. PhysicsTestContext c(1.0f / cFrequency, 1, 1);
  181. c.ZeroGravity();
  182. const float cPenetrationSlop = c.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  183. // Register listener
  184. LoggingContactListener listener;
  185. c.GetSystem()->SetContactListener(&listener);
  186. LoggingBodyActivationListener activation;
  187. c.GetSystem()->SetBodyActivationListener(&activation);
  188. Body &box1 = c.CreateBox(cPos1, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  189. box1.SetLinearVelocity(cVelocity);
  190. Body &box2 = c.CreateBox(cPos2, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(cBoxExtent), EActivation::DontActivate);
  191. CHECK(!box2.IsActive());
  192. c.SimulateSingleStep();
  193. // The bodies should have collided and body 2 should be activated, have velocity, but not moved in this step
  194. CHECK(listener.GetEntryCount() == 2);
  195. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  196. CHECK(listener.Contains(LoggingContactListener::EType::Add, box1.GetID(), box2.GetID()));
  197. Vec3 new_velocity = 0.5f * cVelocity;
  198. CHECK_APPROX_EQUAL(box1.GetPosition(), cPos2 - Vec3(2.0f * cBoxExtent, 0, 0), cPenetrationSlop);
  199. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), new_velocity);
  200. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  201. CHECK_APPROX_EQUAL(box2.GetPosition(), cPos2);
  202. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), new_velocity);
  203. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  204. CHECK(box2.IsActive());
  205. CHECK(activation.Contains(LoggingBodyActivationListener::EType::Activated, box2.GetID()));
  206. listener.Clear();
  207. c.SimulateSingleStep();
  208. // In the next step body 2 should have started to move
  209. CHECK(listener.GetEntryCount() == 2);
  210. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  211. CHECK(listener.Contains(LoggingContactListener::EType::Persist, box1.GetID(), box2.GetID()));
  212. CHECK_APPROX_EQUAL(box1.GetPosition(), cPos2 - Vec3(2.0f * cBoxExtent, 0, 0) + new_velocity / cFrequency, cPenetrationSlop);
  213. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), new_velocity);
  214. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  215. CHECK_APPROX_EQUAL(box2.GetPosition(), cPos2 + new_velocity / cFrequency);
  216. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), new_velocity);
  217. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  218. }
  219. // Two boxes colliding under an angle, linear cast vs inactive discrete
  220. TEST_CASE("TestLinearCastBoxVsInactiveDiscreteBoxAngled")
  221. {
  222. const Vec3 cAngledOffset1(1, 0, -2);
  223. const Vec3 cAngledVelocity = -cFrequency * 2 * cAngledOffset1;
  224. PhysicsTestContext c(1.0f / cFrequency, 1, 1);
  225. c.ZeroGravity();
  226. const float cPenetrationSlop = c.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  227. // Register listener
  228. LoggingContactListener listener;
  229. c.GetSystem()->SetContactListener(&listener);
  230. LoggingBodyActivationListener activation;
  231. c.GetSystem()->SetBodyActivationListener(&activation);
  232. // Make sure box1 exactly hits the face of box2 in the center
  233. Vec3 pos1 = Vec3(2.0f * cBoxExtent, 0, 0) + cAngledOffset1;
  234. Body &box1 = c.CreateBox(pos1, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  235. box1.SetLinearVelocity(cAngledVelocity);
  236. box1.SetRestitution(1.0f);
  237. box1.SetFriction(0.0f);
  238. Body &box2 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(cBoxExtent), EActivation::DontActivate);
  239. box2.SetRestitution(1.0f);
  240. box2.SetFriction(0.0f);
  241. CHECK(!box2.IsActive());
  242. c.SimulateSingleStep();
  243. // The bodies should have collided and body 2 should be activated, have inherited the x velocity of body 1, but not moved in this step. Body 1 should have lost all of its velocity in x direction.
  244. CHECK(listener.GetEntryCount() == 2);
  245. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  246. CHECK(listener.Contains(LoggingContactListener::EType::Add, box1.GetID(), box2.GetID()));
  247. Vec3 new_velocity1 = Vec3(0, 0, cAngledVelocity.GetZ());
  248. Vec3 new_velocity2 = Vec3(cAngledVelocity.GetX(), 0, 0);
  249. CHECK_APPROX_EQUAL(box1.GetPosition(), Vec3(2.0f * cBoxExtent, 0, 0), 2.3f * cPenetrationSlop); // We're moving 2x as fast in the z direction and the slop is allowed in x direction: sqrt(1^2 + 2^2) ~ 2.3
  250. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), new_velocity1, 1.0e-4f);
  251. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero(), 2.0e-4f);
  252. CHECK_APPROX_EQUAL(box2.GetPosition(), Vec3::sZero());
  253. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), new_velocity2, 1.0e-4f);
  254. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero(), 2.0e-4f);
  255. CHECK(box2.IsActive());
  256. CHECK(activation.Contains(LoggingBodyActivationListener::EType::Activated, box2.GetID()));
  257. }
  258. // Two boxes colliding in the center, linear cast vs fast moving discrete, should tunnel through because all discrete bodies are moved before linear cast bodies are tested
  259. TEST_CASE("TestLinearCastBoxVsFastDiscreteBox")
  260. {
  261. PhysicsTestContext c(1.0f / cFrequency, 1, 1);
  262. c.ZeroGravity();
  263. // Register listener
  264. LoggingContactListener listener;
  265. c.GetSystem()->SetContactListener(&listener);
  266. Body &box1 = c.CreateBox(cPos1, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  267. box1.SetLinearVelocity(cVelocity);
  268. Body &box2 = c.CreateBox(cPos2, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  269. box2.SetLinearVelocity(-cVelocity);
  270. c.SimulateSingleStep();
  271. // No collisions should be reported and the bodies should have moved according to their velocity (tunneling through eachother)
  272. CHECK(listener.GetEntryCount() == 0);
  273. CHECK_APPROX_EQUAL(box1.GetPosition(), cPos1 + cVelocity / cFrequency);
  274. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), cVelocity);
  275. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  276. CHECK_APPROX_EQUAL(box2.GetPosition(), cPos2 - cVelocity / cFrequency);
  277. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), -cVelocity);
  278. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  279. }
  280. // Two boxes colliding in the center, linear cast vs moving discrete, discrete is slow enough not to tunnel through linear cast body
  281. TEST_CASE("TestLinearCastBoxVsSlowDiscreteBox")
  282. {
  283. PhysicsTestContext c(1.0f / cFrequency, 1, 1);
  284. c.ZeroGravity();
  285. const float cPenetrationSlop = c.GetSystem()->GetPhysicsSettings().mPenetrationSlop;
  286. // Register listener
  287. LoggingContactListener listener;
  288. c.GetSystem()->SetContactListener(&listener);
  289. Body &box1 = c.CreateBox(cPos1, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::LinearCast, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  290. box1.SetLinearVelocity(cVelocity);
  291. // In 1 step it should move -0.1 meter on the X axis
  292. const Vec3 cBox2Velocity = Vec3(-0.1f * cFrequency, 0, 0);
  293. Body &box2 = c.CreateBox(cPos2, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(cBoxExtent));
  294. box2.SetLinearVelocity(cBox2Velocity);
  295. c.SimulateSingleStep();
  296. // The bodies should have collided and body 2 should have moved according to its discrete step
  297. CHECK(listener.GetEntryCount() == 2);
  298. CHECK(listener.Contains(LoggingContactListener::EType::Validate, box1.GetID(), box2.GetID()));
  299. CHECK(listener.Contains(LoggingContactListener::EType::Add, box1.GetID(), box2.GetID()));
  300. Vec3 new_pos2 = cPos2 + cBox2Velocity / cFrequency;
  301. Vec3 new_velocity = 0.5f * (cVelocity + cBox2Velocity);
  302. CHECK_APPROX_EQUAL(box1.GetPosition(), new_pos2 - Vec3(2.0f * cBoxExtent, 0, 0), cPenetrationSlop);
  303. CHECK_APPROX_EQUAL(box1.GetLinearVelocity(), new_velocity);
  304. CHECK_APPROX_EQUAL(box1.GetAngularVelocity(), Vec3::sZero());
  305. CHECK_APPROX_EQUAL(box2.GetPosition(), new_pos2);
  306. CHECK_APPROX_EQUAL(box2.GetLinearVelocity(), new_velocity);
  307. CHECK_APPROX_EQUAL(box2.GetAngularVelocity(), Vec3::sZero());
  308. }
  309. }