ContactListenerTest.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <TestFramework.h>
  4. #include <Tests/General/ContactListenerTest.h>
  5. #include <Jolt/Physics/Collision/Shape/BoxShape.h>
  6. #include <Jolt/Physics/Collision/Shape/CapsuleShape.h>
  7. #include <Jolt/Physics/Collision/Shape/SphereShape.h>
  8. #include <Jolt/Physics/Collision/Shape/StaticCompoundShape.h>
  9. #include <Jolt/Physics/Body/BodyCreationSettings.h>
  10. #include <Layers.h>
  11. #include <Renderer/DebugRendererImp.h>
  12. JPH_IMPLEMENT_RTTI_VIRTUAL(ContactListenerTest)
  13. {
  14. JPH_ADD_BASE_CLASS(ContactListenerTest, Test)
  15. }
  16. void ContactListenerTest::Initialize()
  17. {
  18. // Floor
  19. CreateFloor();
  20. RefConst<Shape> box_shape = new BoxShape(Vec3(0.5f, 1.0f, 2.0f));
  21. // Dynamic body 1
  22. Body &body1 = *mBodyInterface->CreateBody(BodyCreationSettings(box_shape, Vec3(0, 10, 0), Quat::sIdentity(), EMotionType::Dynamic, Layers::MOVING));
  23. body1.SetAllowSleeping(false);
  24. mBodyInterface->AddBody(body1.GetID(), EActivation::Activate);
  25. // Dynamic body 2
  26. Body &body2 = *mBodyInterface->CreateBody(BodyCreationSettings(box_shape, Vec3(5, 10, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  27. body2.SetAllowSleeping(false);
  28. mBodyInterface->AddBody(body2.GetID(), EActivation::Activate);
  29. // Dynamic body 3
  30. Body &body3 = *mBodyInterface->CreateBody(BodyCreationSettings(new SphereShape(2.0f), Vec3(10, 10, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  31. body3.SetAllowSleeping(false);
  32. mBodyInterface->AddBody(body3.GetID(), EActivation::Activate);
  33. // Dynamic body 4
  34. Ref<StaticCompoundShapeSettings> compound_shape = new StaticCompoundShapeSettings;
  35. compound_shape->AddShape(Vec3::sZero(), Quat::sIdentity(), new CapsuleShape(5, 1));
  36. compound_shape->AddShape(Vec3(0, -5, 0), Quat::sIdentity(), new SphereShape(2));
  37. compound_shape->AddShape(Vec3(0, 5, 0), Quat::sIdentity(), new SphereShape(2));
  38. Body &body4 = *mBodyInterface->CreateBody(BodyCreationSettings(compound_shape, Vec3(15, 10, 0), Quat::sRotation(Vec3::sAxisX(), 0.25f * JPH_PI), EMotionType::Dynamic, Layers::MOVING));
  39. body4.SetAllowSleeping(false);
  40. mBodyInterface->AddBody(body4.GetID(), EActivation::Activate);
  41. // Store bodies for later use
  42. mBody[0] = &body1;
  43. mBody[1] = &body2;
  44. mBody[2] = &body3;
  45. mBody[3] = &body4;
  46. }
  47. ValidateResult ContactListenerTest::OnContactValidate(const Body &inBody1, const Body &inBody2, const CollideShapeResult &inCollisionResult)
  48. {
  49. // Body 1 and 2 should never collide
  50. return ((&inBody1 == mBody[0] && &inBody2 == mBody[1]) || (&inBody1 == mBody[1] && &inBody2 == mBody[0]))? ValidateResult::RejectAllContactsForThisBodyPair : ValidateResult::AcceptAllContactsForThisBodyPair;
  51. }
  52. void ContactListenerTest::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  53. {
  54. // Make body 1 bounce only when a new contact point is added but not when it is persisted (its restitution is normally 0)
  55. if (&inBody1 == mBody[0] || &inBody2 == mBody[0])
  56. {
  57. JPH_ASSERT(ioSettings.mCombinedRestitution == 0.0f);
  58. ioSettings.mCombinedRestitution = 1.0f;
  59. }
  60. }