PhysicsStepListenerTests.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <Jolt/Physics/PhysicsStepListener.h>
  7. TEST_SUITE("StepListenerTest")
  8. {
  9. // Custom step listener that keeps track how often it has been called
  10. class TestStepListener : public PhysicsStepListener
  11. {
  12. public:
  13. virtual void OnStep(float inDeltaTime, PhysicsSystem &inPhysicsSystem) override
  14. {
  15. CHECK(inDeltaTime == mExpectedDeltaTime);
  16. ++mCount;
  17. }
  18. atomic<int> mCount = 0;
  19. float mExpectedDeltaTime = 0.0f;
  20. };
  21. // Perform the actual listener test with a variable amount of collision steps
  22. static void DoTest(int inCollisionSteps)
  23. {
  24. PhysicsTestContext c(1.0f / 60.0f, inCollisionSteps, 1);
  25. // Initialize and add listeners
  26. TestStepListener listeners[10];
  27. for (TestStepListener &l : listeners)
  28. l.mExpectedDeltaTime = 1.0f / 60.0f / inCollisionSteps;
  29. for (TestStepListener &l : listeners)
  30. c.GetSystem()->AddStepListener(&l);
  31. // Step the simulation
  32. c.SimulateSingleStep();
  33. // There aren't any active bodies so no listeners should have been called
  34. for (TestStepListener &l : listeners)
  35. CHECK(l.mCount == 0);
  36. // Now add an active body
  37. c.CreateBox(Vec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(1.0f));
  38. // Step the simulation
  39. c.SimulateSingleStep();
  40. for (TestStepListener &l : listeners)
  41. CHECK(l.mCount == inCollisionSteps);
  42. // Step the simulation
  43. c.SimulateSingleStep();
  44. for (TestStepListener &l : listeners)
  45. CHECK(l.mCount == 2 * inCollisionSteps);
  46. // Unregister all listeners
  47. for (TestStepListener &l : listeners)
  48. c.GetSystem()->RemoveStepListener(&l);
  49. }
  50. // Test the step listeners with a single collision step
  51. TEST_CASE("TestStepListener1")
  52. {
  53. DoTest(1);
  54. }
  55. // Test the step listeners with two collision steps
  56. TEST_CASE("TestStepListener2")
  57. {
  58. DoTest(2);
  59. }
  60. }