EstimateCollisionResponseTest.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include "UnitTestFramework.h"
  4. #include <Jolt/Physics/Collision/EstimateCollisionResponse.h>
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include "PhysicsTestContext.h"
  7. #include "Layers.h"
  8. TEST_SUITE("EstimateCollisionResponseTests")
  9. {
  10. // Test CastShape ordering according to penetration depth
  11. TEST_CASE("TestEstimateCollisionResponse")
  12. {
  13. const Vec3 cBox1HalfExtents(0.1f, 1, 2);
  14. const Vec3 cBox2HalfExtents(0.2f, 3, 4);
  15. // Test different motion types, restitution, positions and angular velocities
  16. for (EMotionType mt : { EMotionType::Static, EMotionType::Kinematic, EMotionType::Dynamic })
  17. for (float restitution : { 0.0f, 0.3f, 1.0f })
  18. for (float y : { 0.0f, 0.5f, cBox2HalfExtents.GetY() })
  19. for (float z : { 0.0f, 0.5f, cBox2HalfExtents.GetZ() })
  20. for (float w : { 0.0f, -1.0f, 1.0f })
  21. {
  22. PhysicsTestContext c;
  23. c.ZeroGravity();
  24. // Install a listener that predicts the collision response
  25. class MyListener : public ContactListener
  26. {
  27. public:
  28. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  29. {
  30. EstimateCollisionResponse(inBody1, inBody2, inManifold, mPredictedV1, mPredictedW1, mPredictedV2, mPredictedW2, mImpulses, ioSettings.mCombinedRestitution);
  31. }
  32. ContactImpulses mImpulses;
  33. Vec3 mPredictedV1 = Vec3::sNaN();
  34. Vec3 mPredictedW1 = Vec3::sNaN();
  35. Vec3 mPredictedV2 = Vec3::sNaN();
  36. Vec3 mPredictedW2 = Vec3::sNaN();
  37. };
  38. MyListener listener;
  39. c.GetSystem()->SetContactListener(&listener);
  40. const RVec3 cBaseOffset(1, 2, 3);
  41. const Real cEpsilon = 0.0001_r;
  42. Body &box1 = c.CreateBox(cBaseOffset, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, cBox1HalfExtents);
  43. box1.SetRestitution(restitution);
  44. box1.SetFriction(0.0f); // No friction, the estimation doesn't handle that
  45. box1.SetLinearVelocity(Vec3(1, 1, 0));
  46. box1.SetAngularVelocity(Vec3(0, w, 0));
  47. Body &box2 = c.CreateBox(cBaseOffset + RVec3(cBox1HalfExtents.GetX() + cBox2HalfExtents.GetX() - cEpsilon, y, z), Quat::sIdentity(), mt, EMotionQuality::Discrete, mt == EMotionType::Static? Layers::NON_MOVING : Layers::MOVING, cBox2HalfExtents);
  48. box2.SetRestitution(restitution);
  49. box2.SetFriction(0.0f);
  50. if (mt != EMotionType::Static)
  51. box2.SetLinearVelocity(Vec3(-1, 0, 0));
  52. c.SimulateSingleStep();
  53. // Check that the predicted velocities are correct
  54. CHECK_APPROX_EQUAL(listener.mPredictedV1, box1.GetLinearVelocity());
  55. CHECK_APPROX_EQUAL(listener.mPredictedW1, box1.GetAngularVelocity());
  56. CHECK_APPROX_EQUAL(listener.mPredictedV2, box2.GetLinearVelocity());
  57. CHECK_APPROX_EQUAL(listener.mPredictedW2, box2.GetAngularVelocity());
  58. }
  59. }
  60. }