LoggingContactListener.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 ordering contract between body 1 and body 2
  32. bool contract = inBody1.GetMotionType() >= inBody2.GetMotionType()
  33. || (inBody1.GetMotionType() == inBody2.GetMotionType() && inBody1.GetID() < inBody2.GetID());
  34. CHECK(contract);
  35. lock_guard lock(mLogMutex);
  36. mLog.push_back({ EType::Validate, inBody1.GetID(), inBody2.GetID(), ContactManifold() });
  37. return ValidateResult::AcceptContact;
  38. }
  39. virtual void OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  40. {
  41. // Check contract that body 1 < body 2
  42. CHECK(inBody1.GetID() < inBody2.GetID());
  43. lock_guard lock(mLogMutex);
  44. SubShapeIDPair key(inBody1.GetID(), inManifold.mSubShapeID1, inBody2.GetID(), inManifold.mSubShapeID2);
  45. CHECK(mExistingContacts.insert(key).second); // Validate that contact does not exist yet
  46. mLog.push_back({ EType::Add, inBody1.GetID(), inBody2.GetID(), inManifold });
  47. }
  48. virtual void OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings) override
  49. {
  50. // Check contract that body 1 < body 2
  51. CHECK(inBody1.GetID() < inBody2.GetID());
  52. lock_guard lock(mLogMutex);
  53. SubShapeIDPair key(inBody1.GetID(), inManifold.mSubShapeID1, inBody2.GetID(), inManifold.mSubShapeID2);
  54. CHECK(mExistingContacts.find(key) != mExistingContacts.end()); // Validate that OnContactAdded was called
  55. mLog.push_back({ EType::Persist, inBody1.GetID(), inBody2.GetID(), inManifold });
  56. }
  57. virtual void OnContactRemoved(const SubShapeIDPair &inSubShapePair) override
  58. {
  59. // Check contract that body 1 < body 2
  60. CHECK(inSubShapePair.GetBody1ID() < inSubShapePair.GetBody2ID());
  61. lock_guard lock(mLogMutex);
  62. CHECK(mExistingContacts.erase(inSubShapePair) == 1); // Validate that OnContactAdded was called
  63. ContactManifold manifold;
  64. manifold.mSubShapeID1 = inSubShapePair.GetSubShapeID1();
  65. manifold.mSubShapeID2 = inSubShapePair.GetSubShapeID2();
  66. mLog.push_back({ EType::Remove, inSubShapePair.GetBody1ID(), inSubShapePair.GetBody2ID(), manifold });
  67. }
  68. void Clear()
  69. {
  70. mLog.clear();
  71. }
  72. size_t GetEntryCount() const
  73. {
  74. return mLog.size();
  75. }
  76. const LogEntry & GetEntry(size_t inIdx) const
  77. {
  78. return mLog[inIdx];
  79. }
  80. // Find first event with a particular type and involving two particular bodies
  81. int Find(EType inType, const BodyID &inBody1, const BodyID &inBody2) const
  82. {
  83. for (size_t i = 0; i < mLog.size(); ++i)
  84. {
  85. const LogEntry &e = mLog[i];
  86. if (e.mType == inType && ((e.mBody1 == inBody1 && e.mBody2 == inBody2) || (e.mBody1 == inBody2 && e.mBody2 == inBody1)))
  87. return int(i);
  88. }
  89. return -1;
  90. }
  91. // Check if event with a particular type and involving two particular bodies exists
  92. bool Contains(EType inType, const BodyID &inBody1, const BodyID &inBody2) const
  93. {
  94. return Find(inType, inBody1, inBody2) >= 0;
  95. }
  96. // Find first event with a particular type and involving two particular bodies and sub shape IDs
  97. int Find(EType inType, const BodyID &inBody1, const SubShapeID &inSubShapeID1, const BodyID &inBody2, const SubShapeID &inSubShapeID2) const
  98. {
  99. for (size_t i = 0; i < mLog.size(); ++i)
  100. {
  101. const LogEntry &e = mLog[i];
  102. if (e.mType == inType
  103. && ((e.mBody1 == inBody1 && e.mManifold.mSubShapeID1 == inSubShapeID1 && e.mBody2 == inBody2 && e.mManifold.mSubShapeID2 == inSubShapeID2)
  104. || (e.mBody1 == inBody2 && e.mManifold.mSubShapeID1 == inSubShapeID2 && e.mBody2 == inBody1 && e.mManifold.mSubShapeID2 == inSubShapeID1)))
  105. return int(i);
  106. }
  107. return -1;
  108. }
  109. // Check if event with a particular type and involving two particular bodies and sub shape IDs exists
  110. bool Contains(EType inType, const BodyID &inBody1, const SubShapeID &inSubShapeID1, const BodyID &inBody2, const SubShapeID &inSubShapeID2) const
  111. {
  112. return Find(inType, inBody1, inSubShapeID1, inBody2, inSubShapeID2) >= 0;
  113. }
  114. private:
  115. Mutex mLogMutex; // Callbacks are made from a thread, make sure we don't corrupt the log
  116. Array<LogEntry> mLog;
  117. UnorderedSet<SubShapeIDPair> mExistingContacts; // For validation purposes: the contacts that are currently active
  118. };