ContactListenerImpl.h 1.5 KB

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