SliderConstraintTests.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include "PhysicsTestContext.h"
  5. #include <Physics/Constraints/SliderConstraint.h>
  6. #include <Physics/Collision/GroupFilterTable.h>
  7. #include "Layers.h"
  8. TEST_SUITE("SliderConstraintTests")
  9. {
  10. // Test a box attached to a slider constraint, test that the body doesn't move beyond the min limit
  11. TEST_CASE("TestSliderConstraintLimitMin")
  12. {
  13. const Vec3 cInitialPos(3.0f, 0, 0);
  14. const float cLimitMin = -7.0f;
  15. // Create group filter
  16. Ref<GroupFilterTable> group_filter = new GroupFilterTable;
  17. // Create two boxes
  18. PhysicsTestContext c;
  19. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(1, 1, 1));
  20. Body &body2 = c.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  21. // Give body 2 velocity towards min limit (and ensure that it arrives well before 1 second)
  22. body2.SetLinearVelocity(-Vec3(10.0f, 0, 0));
  23. // Bodies will go through each other, make sure they don't collide
  24. body1.SetCollisionGroup(CollisionGroup(group_filter, 0, 0));
  25. body2.SetCollisionGroup(CollisionGroup(group_filter, 0, 0));
  26. // Create slider constraint
  27. SliderConstraintSettings s;
  28. s.mSliderAxis = Vec3::sAxisX();
  29. s.mLimitsMin = cLimitMin;
  30. s.mLimitsMax = 0.0f;
  31. c.CreateConstraint<SliderConstraint>(body1, body2, s);
  32. // Simulate
  33. c.Simulate(1.0f);
  34. // Test resulting velocity
  35. CHECK_APPROX_EQUAL(Vec3::sZero(), body2.GetLinearVelocity(), 1.0e-4f);
  36. // Test resulting position
  37. CHECK_APPROX_EQUAL(cInitialPos + cLimitMin * s.mSliderAxis, body2.GetPosition(), 1.0e-4f);
  38. }
  39. // Test a box attached to a slider constraint, test that the body doesn't move beyond the max limit
  40. TEST_CASE("TestSliderConstraintLimitMax")
  41. {
  42. const Vec3 cInitialPos(3.0f, 0, 0);
  43. const float cLimitMax = 7.0f;
  44. // Create two boxes
  45. PhysicsTestContext c;
  46. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(1, 1, 1));
  47. Body &body2 = c.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  48. // Give body 2 velocity towards max limit (and ensure that it arrives well before 1 second)
  49. body2.SetLinearVelocity(Vec3(10.0f, 0, 0));
  50. // Create slider constraint
  51. SliderConstraintSettings s;
  52. s.mSliderAxis = Vec3::sAxisX();
  53. s.mLimitsMin = 0.0f;
  54. s.mLimitsMax = cLimitMax;
  55. c.CreateConstraint<SliderConstraint>(body1, body2, s);
  56. // Simulate
  57. c.Simulate(1.0f);
  58. // Test resulting velocity
  59. CHECK_APPROX_EQUAL(Vec3::sZero(), body2.GetLinearVelocity(), 1.0e-4f);
  60. // Test resulting position
  61. CHECK_APPROX_EQUAL(cInitialPos + cLimitMax * s.mSliderAxis, body2.GetPosition(), 1.0e-4f);
  62. }
  63. // Test a box attached to a slider constraint, test that a motor can drive it to a specific velocity
  64. TEST_CASE("TestSliderConstraintDriveVelocityStaticVsDynamic")
  65. {
  66. const Vec3 cInitialPos(3.0f, 0, 0);
  67. const float cMotorAcceleration = 2.0f;
  68. // Create two boxes
  69. PhysicsTestContext c;
  70. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(1, 1, 1));
  71. Body &body2 = c.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  72. // Create slider constraint
  73. SliderConstraintSettings s;
  74. s.mSliderAxis = Vec3::sAxisX();
  75. constexpr float mass = Cubed(2.0f) * 1000.0f; // Density * Volume
  76. s.mMotorSettings = MotorSettings(0.0f, 0.0f, mass * cMotorAcceleration, 0.0f);
  77. SliderConstraint &constraint = c.CreateConstraint<SliderConstraint>(body1, body2, s);
  78. constraint.SetMotorState(EMotorState::Velocity);
  79. constraint.SetTargetVelocity(1.5f * cMotorAcceleration);
  80. // Simulate
  81. c.Simulate(1.0f);
  82. // Test resulting velocity
  83. Vec3 expected_vel = cMotorAcceleration * s.mSliderAxis;
  84. CHECK_APPROX_EQUAL(expected_vel, body2.GetLinearVelocity(), 1.0e-4f);
  85. // Simulate (after 0.5 seconds it should reach the target velocity)
  86. c.Simulate(1.0f);
  87. // Test resulting velocity
  88. expected_vel = 1.5f * cMotorAcceleration * s.mSliderAxis;
  89. CHECK_APPROX_EQUAL(expected_vel, body2.GetLinearVelocity(), 1.0e-4f);
  90. // Test resulting position (1.5s of acceleration + 0.5s of constant speed)
  91. Vec3 expected_pos = c.PredictPosition(cInitialPos, Vec3::sZero(), cMotorAcceleration * s.mSliderAxis, 1.5f) + 0.5f * expected_vel;
  92. CHECK_APPROX_EQUAL(expected_pos, body2.GetPosition(), 1.0e-4f);
  93. }
  94. // Test 2 dynamic boxes attached to a slider constraint, test that a motor can drive it to a specific velocity
  95. TEST_CASE("TestSliderConstraintDriveVelocityDynamicVsDynamic")
  96. {
  97. const Vec3 cInitialPos(3.0f, 0, 0);
  98. const float cMotorAcceleration = 2.0f;
  99. // Create two boxes
  100. PhysicsTestContext c;
  101. c.ZeroGravity();
  102. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  103. Body &body2 = c.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  104. // Create slider constraint
  105. SliderConstraintSettings s;
  106. s.mSliderAxis = Vec3::sAxisX();
  107. constexpr float mass = Cubed(2.0f) * 1000.0f; // Density * Volume
  108. s.mMotorSettings = MotorSettings(0.0f, 0.0f, mass * cMotorAcceleration, 0.0f);
  109. SliderConstraint &constraint = c.CreateConstraint<SliderConstraint>(body1, body2, s);
  110. constraint.SetMotorState(EMotorState::Velocity);
  111. constraint.SetTargetVelocity(3.0f * cMotorAcceleration);
  112. // Simulate
  113. c.Simulate(1.0f);
  114. // Test resulting velocity (both boxes move in opposite directions with the same force, so the resulting velocity difference is 2x as big as the previous test)
  115. Vec3 expected_vel = cMotorAcceleration * s.mSliderAxis;
  116. CHECK_APPROX_EQUAL(-expected_vel, body1.GetLinearVelocity(), 1.0e-4f);
  117. CHECK_APPROX_EQUAL(expected_vel, body2.GetLinearVelocity(), 1.0e-4f);
  118. // Simulate (after 0.5 seconds it should reach the target velocity)
  119. c.Simulate(1.0f);
  120. // Test resulting velocity
  121. expected_vel = 1.5f * cMotorAcceleration * s.mSliderAxis;
  122. CHECK_APPROX_EQUAL(-expected_vel, body1.GetLinearVelocity(), 1.0e-4f);
  123. CHECK_APPROX_EQUAL(expected_vel, body2.GetLinearVelocity(), 1.0e-4f);
  124. // Test resulting position (1.5s of acceleration + 0.5s of constant speed)
  125. Vec3 expected_pos1 = c.PredictPosition(Vec3::sZero(), Vec3::sZero(), -cMotorAcceleration * s.mSliderAxis, 1.5f) - 0.5f * expected_vel;
  126. Vec3 expected_pos2 = c.PredictPosition(cInitialPos, Vec3::sZero(), cMotorAcceleration * s.mSliderAxis, 1.5f) + 0.5f * expected_vel;
  127. CHECK_APPROX_EQUAL(expected_pos1, body1.GetPosition(), 1.0e-4f);
  128. CHECK_APPROX_EQUAL(expected_pos2, body2.GetPosition(), 1.0e-4f);
  129. }
  130. // Test a box attached to a slider constraint, test that a motor can drive it to a specific position
  131. TEST_CASE("TestSliderConstraintDrivePosition")
  132. {
  133. const Vec3 cInitialPos(3.0f, 0, 0);
  134. const Vec3 cMotorPos(10.0f, 0, 0);
  135. // Create two boxes
  136. PhysicsTestContext c;
  137. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(1, 1, 1));
  138. Body &body2 = c.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  139. // Create slider constraint
  140. SliderConstraintSettings s;
  141. s.mSliderAxis = Vec3::sAxisX();
  142. SliderConstraint &constraint = c.CreateConstraint<SliderConstraint>(body1, body2, s);
  143. constraint.SetMotorState(EMotorState::Position);
  144. constraint.SetTargetPosition((cMotorPos - cInitialPos).Dot(s.mSliderAxis));
  145. // Simulate
  146. c.Simulate(2.0f);
  147. // Test resulting velocity
  148. CHECK_APPROX_EQUAL(Vec3::sZero(), body2.GetLinearVelocity(), 1.0e-4f);
  149. // Test resulting position
  150. CHECK_APPROX_EQUAL(cMotorPos, body2.GetPosition(), 1.0e-4f);
  151. }
  152. // Test a box attached to a slider constraint, give it initial velocity and test that the friction provides the correct decelleration
  153. TEST_CASE("TestSliderConstraintFriction")
  154. {
  155. const Vec3 cInitialPos(3.0f, 0, 0);
  156. const Vec3 cInitialVelocity(10.0f, 0, 0);
  157. const float cFrictionAcceleration = 2.0f;
  158. const float cSimulationTime = 2.0f;
  159. // Create two boxes
  160. PhysicsTestContext c;
  161. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(1, 1, 1));
  162. Body &body2 = c.CreateBox(cInitialPos, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1));
  163. body2.SetLinearVelocity(cInitialVelocity);
  164. // Create slider constraint
  165. SliderConstraintSettings s;
  166. s.mSliderAxis = Vec3::sAxisX();
  167. constexpr float mass = Cubed(2.0f) * 1000.0f; // Density * Volume
  168. s.mMaxFrictionForce = mass * cFrictionAcceleration;
  169. c.CreateConstraint<SliderConstraint>(body1, body2, s);
  170. // Simulate while applying friction
  171. c.Simulate(cSimulationTime);
  172. // Test resulting velocity
  173. Vec3 expected_vel = cInitialVelocity - cFrictionAcceleration * cSimulationTime * s.mSliderAxis;
  174. CHECK_APPROX_EQUAL(expected_vel, body2.GetLinearVelocity(), 1.0e-4f);
  175. // Test resulting position
  176. Vec3 expected_pos = c.PredictPosition(cInitialPos, cInitialVelocity, -cFrictionAcceleration * s.mSliderAxis, cSimulationTime);
  177. CHECK_APPROX_EQUAL(expected_pos, body2.GetPosition(), 1.0e-4f);
  178. }
  179. // Test if a slider constraint wakes up connected bodies
  180. TEST_CASE("TestSliderStaticVsKinematic")
  181. {
  182. // Create two boxes far away enough so they are not touching
  183. PhysicsTestContext c;
  184. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  185. Body &body2 = c.CreateBox(Vec3(10, 0, 0), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  186. // Create slider constraint
  187. SliderConstraintSettings s;
  188. s.mSliderAxis = Vec3::sAxisX();
  189. c.CreateConstraint<SliderConstraint>(body1, body2, s);
  190. // Verify they're not active
  191. CHECK(!body1.IsActive());
  192. CHECK(!body2.IsActive());
  193. // After a physics step, the bodies should still not be active
  194. c.SimulateSingleStep();
  195. CHECK(!body1.IsActive());
  196. CHECK(!body2.IsActive());
  197. // Activate the kinematic body
  198. c.GetSystem()->GetBodyInterface().ActivateBody(body2.GetID());
  199. CHECK(!body1.IsActive());
  200. CHECK(body2.IsActive());
  201. // The static body should not become active (it can't)
  202. c.SimulateSingleStep();
  203. CHECK(!body1.IsActive());
  204. CHECK(body2.IsActive());
  205. }
  206. // Test if a slider constraint wakes up connected bodies
  207. TEST_CASE("TestSliderStaticVsDynamic")
  208. {
  209. // Create two boxes far away enough so they are not touching
  210. PhysicsTestContext c;
  211. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Static, EMotionQuality::Discrete, Layers::NON_MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  212. Body &body2 = c.CreateBox(Vec3(10, 0, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  213. // Create slider constraint
  214. SliderConstraintSettings s;
  215. s.mSliderAxis = Vec3::sAxisX();
  216. c.CreateConstraint<SliderConstraint>(body1, body2, s);
  217. // Verify they're not active
  218. CHECK(!body1.IsActive());
  219. CHECK(!body2.IsActive());
  220. // After a physics step, the bodies should still not be active
  221. c.SimulateSingleStep();
  222. CHECK(!body1.IsActive());
  223. CHECK(!body2.IsActive());
  224. // Activate the dynamic body
  225. c.GetSystem()->GetBodyInterface().ActivateBody(body2.GetID());
  226. CHECK(!body1.IsActive());
  227. CHECK(body2.IsActive());
  228. // The static body should not become active (it can't)
  229. c.SimulateSingleStep();
  230. CHECK(!body1.IsActive());
  231. CHECK(body2.IsActive());
  232. }
  233. // Test if a slider constraint wakes up connected bodies
  234. TEST_CASE("TestSliderKinematicVsDynamic")
  235. {
  236. // Create two boxes far away enough so they are not touching
  237. PhysicsTestContext c;
  238. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  239. Body &body2 = c.CreateBox(Vec3(10, 0, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  240. // Create slider constraint
  241. SliderConstraintSettings s;
  242. s.mSliderAxis = Vec3::sAxisX();
  243. c.CreateConstraint<SliderConstraint>(body1, body2, s);
  244. // Verify they're not active
  245. CHECK(!body1.IsActive());
  246. CHECK(!body2.IsActive());
  247. // After a physics step, the bodies should still not be active
  248. c.SimulateSingleStep();
  249. CHECK(!body1.IsActive());
  250. CHECK(!body2.IsActive());
  251. // Activate the keyframed body
  252. c.GetSystem()->GetBodyInterface().ActivateBody(body1.GetID());
  253. CHECK(body1.IsActive());
  254. CHECK(!body2.IsActive());
  255. // After a physics step, both bodies should be active now
  256. c.SimulateSingleStep();
  257. CHECK(body1.IsActive());
  258. CHECK(body2.IsActive());
  259. }
  260. // Test if a slider constraint wakes up connected bodies
  261. TEST_CASE("TestSliderKinematicVsKinematic")
  262. {
  263. // Create two boxes far away enough so they are not touching
  264. PhysicsTestContext c;
  265. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  266. Body &body2 = c.CreateBox(Vec3(10, 0, 0), Quat::sIdentity(), EMotionType::Kinematic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  267. // Create slider constraint
  268. SliderConstraintSettings s;
  269. s.mSliderAxis = Vec3::sAxisX();
  270. c.CreateConstraint<SliderConstraint>(body1, body2, s);
  271. // Verify they're not active
  272. CHECK(!body1.IsActive());
  273. CHECK(!body2.IsActive());
  274. // After a physics step, the bodies should still not be active
  275. c.SimulateSingleStep();
  276. CHECK(!body1.IsActive());
  277. CHECK(!body2.IsActive());
  278. // Activate the first keyframed body
  279. c.GetSystem()->GetBodyInterface().ActivateBody(body1.GetID());
  280. CHECK(body1.IsActive());
  281. CHECK(!body2.IsActive());
  282. // After a physics step, the second keyframed body should not be woken up
  283. c.SimulateSingleStep();
  284. CHECK(body1.IsActive());
  285. CHECK(!body2.IsActive());
  286. }
  287. // Test if a slider constraint wakes up connected bodies
  288. TEST_CASE("TestSliderDynamicVsDynamic")
  289. {
  290. // Create two boxes far away enough so they are not touching
  291. PhysicsTestContext c;
  292. Body &body1 = c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  293. Body &body2 = c.CreateBox(Vec3(10, 0, 0), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3(1, 1, 1), EActivation::DontActivate);
  294. // Create slider constraint
  295. SliderConstraintSettings s;
  296. s.mSliderAxis = Vec3::sAxisX();
  297. c.CreateConstraint<SliderConstraint>(body1, body2, s);
  298. // Verify they're not active
  299. CHECK(!body1.IsActive());
  300. CHECK(!body2.IsActive());
  301. // After a physics step, the bodies should still not be active
  302. c.SimulateSingleStep();
  303. CHECK(!body1.IsActive());
  304. CHECK(!body2.IsActive());
  305. // Activate the first dynamic body
  306. c.GetSystem()->GetBodyInterface().ActivateBody(body1.GetID());
  307. CHECK(body1.IsActive());
  308. CHECK(!body2.IsActive());
  309. // After a physics step, both bodies should be active now
  310. c.SimulateSingleStep();
  311. CHECK(body1.IsActive());
  312. CHECK(body2.IsActive());
  313. }
  314. }