LoggingContactListener.h 4.9 KB

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