EstimateCollisionResponseTest.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. PhysicsTestContext c;
  14. c.ZeroGravity();
  15. const Vec3 cBox1HalfExtents(0.1f, 1, 2);
  16. const Vec3 cBox2HalfExtents(0.2f, 3, 4);
  17. // Test different motion types, restitution, positions and angular velocities
  18. for (EMotionType mt : { EMotionType::Static, EMotionType::Kinematic, EMotionType::Dynamic })
  19. for (float restitution : { 0.0f, 0.3f, 1.0f })
  20. for (float friction : { 0.0f, 0.3f, 1.0f })
  21. for (float y : { 0.0f, 0.5f, cBox2HalfExtents.GetY() })
  22. for (float z : { 0.0f, 0.5f, cBox2HalfExtents.GetZ() })
  23. for (float w : { 0.0f, -1.0f, 1.0f })
  24. {
  25. // Install a listener that predicts the collision response
  26. class MyListener : public ContactListener
  27. {
  28. public:
  29. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  30. {
  31. EstimateCollisionResponse(inBody1, inBody2, inManifold, mResult, ioSettings.mCombinedFriction, ioSettings.mCombinedRestitution);
  32. }
  33. CollisionEstimationResult mResult;
  34. };
  35. MyListener listener;
  36. c.GetSystem()->SetContactListener(&listener);
  37. const RVec3 cBaseOffset(1, 2, 3);
  38. const Real cEpsilon = 0.0001_r;
  39. Body &box1 = c.CreateBox(cBaseOffset, Quat::sIdentity(), EMotionType::Dynamic, EMotionQuality::Discrete, Layers::MOVING, cBox1HalfExtents);
  40. box1.SetFriction(friction);
  41. box1.SetRestitution(restitution);
  42. box1.SetLinearVelocity(Vec3(1, 1, 0));
  43. box1.SetAngularVelocity(Vec3(0, w, 0));
  44. 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);
  45. box2.SetFriction(friction);
  46. box2.SetRestitution(restitution);
  47. if (mt != EMotionType::Static)
  48. box2.SetLinearVelocity(Vec3(-1, 0, 0));
  49. // Step the simulation
  50. c.SimulateSingleStep();
  51. // Check that the predicted velocities are correct
  52. CHECK_APPROX_EQUAL(listener.mResult.mLinearVelocity1, box1.GetLinearVelocity());
  53. CHECK_APPROX_EQUAL(listener.mResult.mAngularVelocity1, box1.GetAngularVelocity());
  54. CHECK_APPROX_EQUAL(listener.mResult.mLinearVelocity2, box2.GetLinearVelocity());
  55. CHECK_APPROX_EQUAL(listener.mResult.mAngularVelocity2, box2.GetAngularVelocity());
  56. // Remove the bodies in reverse order
  57. BodyInterface &bi = c.GetBodyInterface();
  58. bi.RemoveBody(box2.GetID());
  59. bi.RemoveBody(box1.GetID());
  60. bi.DestroyBody(box2.GetID());
  61. bi.DestroyBody(box1.GetID());
  62. }
  63. }
  64. }