ContactListenerTest.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Tests/Test.h>
  6. #include <Jolt/Physics/Collision/ContactListener.h>
  7. class ContactListenerTest : public Test, public ContactListener
  8. {
  9. public:
  10. JPH_DECLARE_RTTI_VIRTUAL(JPH_NO_EXPORT, ContactListenerTest)
  11. // Description of the test
  12. virtual const char * GetDescription() const override
  13. {
  14. return "Demonstrates how to listen for contact events.\n"
  15. "Leftmost box ignores contacts with the 2nd box and overrides the restitution to 1 for non-persisted contacts.\n"
  16. "Rightmost box contains an inner and an outer shape, the outer shape acts as a sensor.\n"
  17. "The TTY will output estimated post collision velocities.";
  18. }
  19. // See: Test
  20. virtual void Initialize() override;
  21. virtual void PostPhysicsUpdate(float inDeltaTime) override;
  22. // If this test implements a contact listener, it should be returned here
  23. virtual ContactListener *GetContactListener() override { return this; }
  24. // See: ContactListener
  25. virtual ValidateResult OnContactValidate(const Body &inBody1, const Body &inBody2, RVec3Arg inBaseOffset, const CollideShapeResult &inCollisionResult) override;
  26. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  27. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  28. private:
  29. void MakeBody5PartialSensor(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings);
  30. // The 5 bodies that we create
  31. Body * mBody[5];
  32. // Tracks predicted velocities so we can compare them with the actual velocities after time step
  33. struct PredictedVelocity
  34. {
  35. BodyID mBodyID;
  36. Vec3 mLinearVelocity;
  37. Vec3 mAngularVelocity;
  38. };
  39. Mutex mPredictedVelocitiesMutex;
  40. Array<PredictedVelocity> mPredictedVelocities;
  41. };