LoggingContactListener.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Contact listener that just logs the calls made to it for later validation
  7. class LoggingContactListener : public ContactListener
  8. {
  9. public:
  10. // Contact callback type
  11. enum class EType
  12. {
  13. Validate,
  14. Add,
  15. Persist,
  16. Remove
  17. };
  18. // Entry written when a contact callback happens
  19. struct LogEntry
  20. {
  21. EType mType;
  22. BodyID mBody1;
  23. BodyID mBody2;
  24. ContactManifold mManifold;
  25. };
  26. virtual ValidateResult OnContactValidate(const Body &inBody1, const Body &inBody2, RVec3Arg inBaseOffset, const CollideShapeResult &inCollisionResult) override
  27. {
  28. // Check contract that body 1 is dynamic or that body2 is not dynamic
  29. bool contract = inBody1.IsDynamic() || !inBody2.IsDynamic();
  30. CHECK(contract);
  31. lock_guard lock(mLogMutex);
  32. mLog.push_back({ EType::Validate, inBody1.GetID(), inBody2.GetID(), ContactManifold() });
  33. return ValidateResult::AcceptContact;
  34. }
  35. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  36. {
  37. // Check contract that body 1 < body 2
  38. CHECK(inBody1.GetID() < inBody2.GetID());
  39. lock_guard lock(mLogMutex);
  40. SubShapeIDPair key(inBody1.GetID(), inManifold.mSubShapeID1, inBody2.GetID(), inManifold.mSubShapeID2);
  41. CHECK(mExistingContacts.insert(key).second); // Validate that contact does not exist yet
  42. mLog.push_back({ EType::Add, inBody1.GetID(), inBody2.GetID(), inManifold });
  43. }
  44. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  45. {
  46. // Check contract that body 1 < body 2
  47. CHECK(inBody1.GetID() < inBody2.GetID());
  48. lock_guard lock(mLogMutex);
  49. SubShapeIDPair key(inBody1.GetID(), inManifold.mSubShapeID1, inBody2.GetID(), inManifold.mSubShapeID2);
  50. CHECK(mExistingContacts.find(key) != mExistingContacts.end()); // Validate that OnContactAdded was called
  51. mLog.push_back({ EType::Persist, inBody1.GetID(), inBody2.GetID(), inManifold });
  52. }
  53. virtual void OnContactRemoved(const SubShapeIDPair &inSubShapePair) override
  54. {
  55. // Check contract that body 1 < body 2
  56. CHECK(inSubShapePair.GetBody1ID() < inSubShapePair.GetBody2ID());
  57. lock_guard lock(mLogMutex);
  58. CHECK(mExistingContacts.erase(inSubShapePair) == 1); // Validate that OnContactAdded was called
  59. ContactManifold manifold;
  60. manifold.mSubShapeID1 = inSubShapePair.GetSubShapeID1();
  61. manifold.mSubShapeID2 = inSubShapePair.GetSubShapeID2();
  62. mLog.push_back({ EType::Remove, inSubShapePair.GetBody1ID(), inSubShapePair.GetBody2ID(), manifold });
  63. }
  64. void Clear()
  65. {
  66. mLog.clear();
  67. }
  68. size_t GetEntryCount() const
  69. {
  70. return mLog.size();
  71. }
  72. const LogEntry & GetEntry(size_t inIdx) const
  73. {
  74. return mLog[inIdx];
  75. }
  76. // Find first event with a particular type and involving two particular bodies
  77. int Find(EType inType, const BodyID &inBody1, const BodyID &inBody2) const
  78. {
  79. for (size_t i = 0; i < mLog.size(); ++i)
  80. {
  81. const LogEntry &e = mLog[i];
  82. if (e.mType == inType && ((e.mBody1 == inBody1 && e.mBody2 == inBody2) || (e.mBody1 == inBody2 && e.mBody2 == inBody1)))
  83. return int(i);
  84. }
  85. return -1;
  86. }
  87. // Check if event with a particular type and involving two particular bodies exists
  88. bool Contains(EType inType, const BodyID &inBody1, const BodyID &inBody2) const
  89. {
  90. return Find(inType, inBody1, inBody2) >= 0;
  91. }
  92. private:
  93. Mutex mLogMutex; // Callbacks are made from a thread, make sure we don't corrupt the log
  94. Array<LogEntry> mLog;
  95. UnorderedSet<SubShapeIDPair> mExistingContacts; // For validation purposes: the contacts that are currently active
  96. };