ContactListenerImpl.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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 <Jolt/Physics/Collision/ContactListener.h>
  6. #include <Jolt/Physics/StateRecorder.h>
  7. #include <Jolt/Core/Mutex.h>
  8. #include <Jolt/Core/UnorderedMap.h>
  9. // Tests the contact listener callbacks
  10. class ContactListenerImpl : public ContactListener
  11. {
  12. public:
  13. // See: ContactListener
  14. virtual ValidateResult OnContactValidate(const Body &inBody1, const Body &inBody2, RVec3Arg inBaseOffset, const CollideShapeResult &inCollisionResult) override;
  15. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  16. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override;
  17. virtual void OnContactRemoved(const SubShapeIDPair &inSubShapePair) override;
  18. // Saving / restoring state for replay
  19. void SaveState(StateRecorder &inStream) const;
  20. void RestoreState(StateRecorder &inStream);
  21. // Draw the current contact state
  22. void DrawState();
  23. // Ability to defer to the next contact listener after this one handles the callback
  24. void SetNextListener(ContactListener *inListener) { mNext = inListener; }
  25. private:
  26. // Map that keeps track of the current state of contacts based on the contact listener callbacks
  27. using StatePair = pair<RVec3, ContactPoints>;
  28. using StateMap = UnorderedMap<SubShapeIDPair, StatePair>;
  29. Mutex mStateMutex;
  30. StateMap mState;
  31. ContactListener * mNext = nullptr;
  32. };