PhysicsStepListenerTests.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include "PhysicsTestContext.h"
  6. #include "Layers.h"
  7. #include <Jolt/Physics/PhysicsStepListener.h>
  8. TEST_SUITE("StepListenerTest")
  9. {
  10. // Custom step listener that keeps track how often it has been called
  11. class TestStepListener : public PhysicsStepListener
  12. {
  13. public:
  14. virtual void OnStep(const PhysicsStepListenerContext &inContext) override
  15. {
  16. CHECK(inContext.mDeltaTime == mExpectedDeltaTime);
  17. CHECK(inContext.mIsFirstStep == ((mCount % mExpectedSteps) == 0));
  18. int new_count = mCount.fetch_add(1) + 1;
  19. CHECK(inContext.mIsLastStep == ((new_count % mExpectedSteps) == 0));
  20. }
  21. atomic<int> mCount = 0;
  22. int mExpectedSteps;
  23. float mExpectedDeltaTime = 0.0f;
  24. };
  25. // Perform the actual listener test with a variable amount of collision steps
  26. static void DoTest(int inCollisionSteps)
  27. {
  28. PhysicsTestContext c(1.0f / 60.0f, inCollisionSteps);
  29. // Initialize and add listeners
  30. TestStepListener listeners[10];
  31. for (TestStepListener &l : listeners)
  32. {
  33. l.mExpectedDeltaTime = 1.0f / 60.0f / inCollisionSteps;
  34. l.mExpectedSteps = inCollisionSteps;
  35. }
  36. for (TestStepListener &l : listeners)
  37. c.GetSystem()->AddStepListener(&l);
  38. // Step the simulation
  39. c.SimulateSingleStep();
  40. // There aren't any active bodies so no listeners should have been called
  41. for (TestStepListener &l : listeners)
  42. CHECK(l.mCount == 0);
  43. // Now add an active body
  44. c.CreateBox(RVec3::sZero(), Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, Vec3::sReplicate(1.0f));
  45. // Step the simulation
  46. c.SimulateSingleStep();
  47. for (TestStepListener &l : listeners)
  48. CHECK(l.mCount == inCollisionSteps);
  49. // Step the simulation
  50. c.SimulateSingleStep();
  51. for (TestStepListener &l : listeners)
  52. CHECK(l.mCount == 2 * inCollisionSteps);
  53. // Unregister all listeners
  54. for (TestStepListener &l : listeners)
  55. c.GetSystem()->RemoveStepListener(&l);
  56. // Step the simulation
  57. c.SimulateSingleStep();
  58. // Check that no further callbacks were triggered
  59. for (TestStepListener &l : listeners)
  60. CHECK(l.mCount == 2 * inCollisionSteps);
  61. }
  62. // Test the step listeners with a single collision step
  63. TEST_CASE("TestStepListener1")
  64. {
  65. DoTest(1);
  66. }
  67. // Test the step listeners with two collision steps
  68. TEST_CASE("TestStepListener2")
  69. {
  70. DoTest(2);
  71. }
  72. // Test the step listeners with four collision steps
  73. TEST_CASE("TestStepListener4")
  74. {
  75. DoTest(4);
  76. }
  77. }