ContactListenerImpl.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <TestFramework.h>
  5. #include <Utils/ContactListenerImpl.h>
  6. #include <Renderer/DebugRendererImp.h>
  7. #include <Jolt/Physics/Body/Body.h>
  8. #include <Jolt/Physics/Collision/CollideShape.h>
  9. #include <Jolt/Core/QuickSort.h>
  10. ValidateResult ContactListenerImpl::OnContactValidate(const Body &inBody1, const Body &inBody2, RVec3Arg inBaseOffset, const CollideShapeResult &inCollisionResult)
  11. {
  12. // Expect body 1 to be dynamic (or one of the bodies must be a sensor)
  13. if (!inBody1.IsDynamic() && !inBody1.IsSensor() && !inBody2.IsSensor())
  14. JPH_BREAKPOINT;
  15. ValidateResult result;
  16. if (mNext != nullptr)
  17. result = mNext->OnContactValidate(inBody1, inBody2, inBaseOffset, inCollisionResult);
  18. else
  19. result = ContactListener::OnContactValidate(inBody1, inBody2, inBaseOffset, inCollisionResult);
  20. RVec3 contact_point = inBaseOffset + inCollisionResult.mContactPointOn1;
  21. DebugRenderer::sInstance->DrawArrow(contact_point, contact_point - inCollisionResult.mPenetrationAxis.NormalizedOr(Vec3::sZero()), Color::sBlue, 0.05f);
  22. Trace("Validate %u and %u result %d", inBody1.GetID().GetIndex(), inBody2.GetID().GetIndex(), (int)result);
  23. return result;
  24. }
  25. void ContactListenerImpl::OnContactAdded(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  26. {
  27. // Expect bodies to be sorted
  28. if (!(inBody1.GetID() < inBody2.GetID()))
  29. JPH_BREAKPOINT;
  30. Trace("Contact added %u (%08x) and %u (%08x)", inBody1.GetID().GetIndex(), inManifold.mSubShapeID1.GetValue(), inBody2.GetID().GetIndex(), inManifold.mSubShapeID2.GetValue());
  31. DebugRenderer::sInstance->DrawWirePolygon(RMat44::sTranslation(inManifold.mBaseOffset), inManifold.mRelativeContactPointsOn1, Color::sGreen, 0.05f);
  32. DebugRenderer::sInstance->DrawWirePolygon(RMat44::sTranslation(inManifold.mBaseOffset), inManifold.mRelativeContactPointsOn2, Color::sGreen, 0.05f);
  33. DebugRenderer::sInstance->DrawArrow(inManifold.GetWorldSpaceContactPointOn1(0), inManifold.GetWorldSpaceContactPointOn1(0) + inManifold.mWorldSpaceNormal, Color::sGreen, 0.05f);
  34. // Insert new manifold into state map
  35. {
  36. lock_guard lock(mStateMutex);
  37. SubShapeIDPair key(inBody1.GetID(), inManifold.mSubShapeID1, inBody2.GetID(), inManifold.mSubShapeID2);
  38. if (mState.find(key) != mState.end())
  39. JPH_BREAKPOINT; // Added contact that already existed
  40. mState[key] = StatePair(inManifold.mBaseOffset, inManifold.mRelativeContactPointsOn1);
  41. }
  42. if (mNext != nullptr)
  43. mNext->OnContactAdded(inBody1, inBody2, inManifold, ioSettings);
  44. }
  45. void ContactListenerImpl::OnContactPersisted(const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &ioSettings)
  46. {
  47. // Expect bodies to be sorted
  48. if (!(inBody1.GetID() < inBody2.GetID()))
  49. JPH_BREAKPOINT;
  50. Trace("Contact persisted %u (%08x) and %u (%08x)", inBody1.GetID().GetIndex(), inManifold.mSubShapeID1.GetValue(), inBody2.GetID().GetIndex(), inManifold.mSubShapeID2.GetValue());
  51. DebugRenderer::sInstance->DrawWirePolygon(RMat44::sTranslation(inManifold.mBaseOffset), inManifold.mRelativeContactPointsOn1, Color::sYellow, 0.05f);
  52. DebugRenderer::sInstance->DrawWirePolygon(RMat44::sTranslation(inManifold.mBaseOffset), inManifold.mRelativeContactPointsOn2, Color::sYellow, 0.05f);
  53. DebugRenderer::sInstance->DrawArrow(inManifold.GetWorldSpaceContactPointOn1(0), inManifold.GetWorldSpaceContactPointOn1(0) + inManifold.mWorldSpaceNormal, Color::sYellow, 0.05f);
  54. // Update existing manifold in state map
  55. {
  56. lock_guard lock(mStateMutex);
  57. SubShapeIDPair key(inBody1.GetID(), inManifold.mSubShapeID1, inBody2.GetID(), inManifold.mSubShapeID2);
  58. StateMap::iterator i = mState.find(key);
  59. if (i != mState.end())
  60. i->second = StatePair(inManifold.mBaseOffset, inManifold.mRelativeContactPointsOn1);
  61. else
  62. JPH_BREAKPOINT; // Persisted contact that didn't exist
  63. }
  64. if (mNext != nullptr)
  65. mNext->OnContactPersisted(inBody1, inBody2, inManifold, ioSettings);
  66. }
  67. void ContactListenerImpl::OnContactRemoved(const SubShapeIDPair &inSubShapePair)
  68. {
  69. // Expect bodies to be sorted
  70. if (!(inSubShapePair.GetBody1ID() < inSubShapePair.GetBody2ID()))
  71. JPH_BREAKPOINT;
  72. Trace("Contact removed %u (%08x) and %u (%08x)", inSubShapePair.GetBody1ID().GetIndex(), inSubShapePair.GetSubShapeID1().GetValue(), inSubShapePair.GetBody2ID().GetIndex(), inSubShapePair.GetSubShapeID2().GetValue());
  73. // Update existing manifold in state map
  74. {
  75. lock_guard lock(mStateMutex);
  76. StateMap::iterator i = mState.find(inSubShapePair);
  77. if (i != mState.end())
  78. mState.erase(i);
  79. else
  80. JPH_BREAKPOINT; // Removed contact that didn't exist
  81. }
  82. if (mNext != nullptr)
  83. mNext->OnContactRemoved(inSubShapePair);
  84. }
  85. void ContactListenerImpl::SaveState(StateRecorder &inStream) const
  86. {
  87. // Write length
  88. inStream.Write(mState.size());
  89. // Get and sort keys
  90. Array<SubShapeIDPair> keys;
  91. for (const StateMap::value_type &kv : mState)
  92. keys.push_back(kv.first);
  93. QuickSort(keys.begin(), keys.end());
  94. // Write key value pairs
  95. for (const SubShapeIDPair &k : keys)
  96. {
  97. // Write key
  98. inStream.Write(k);
  99. // Write value
  100. const StatePair &sp = mState.find(k)->second;
  101. inStream.Write(sp.first);
  102. inStream.Write(sp.second.size());
  103. inStream.WriteBytes(sp.second.data(), sp.second.size() * sizeof(Vec3));
  104. }
  105. }
  106. void ContactListenerImpl::RestoreState(StateRecorder &inStream)
  107. {
  108. Trace("Restore Contact State");
  109. // Read length
  110. StateMap::size_type length;
  111. if (inStream.IsValidating())
  112. length = mState.size();
  113. inStream.Read(length);
  114. Array<SubShapeIDPair> keys;
  115. // Clear the state and remember the old state for validation
  116. StateMap old_state;
  117. old_state.swap(mState);
  118. // Prepopulate keys and values with current values if we're validating
  119. if (inStream.IsValidating())
  120. {
  121. // Get and sort keys
  122. for (const StateMap::value_type &kv : old_state)
  123. keys.push_back(kv.first);
  124. QuickSort(keys.begin(), keys.end());
  125. }
  126. // Ensure we have the corect size
  127. keys.resize(length);
  128. for (size_t i = 0; i < length; ++i)
  129. {
  130. // Read key
  131. SubShapeIDPair key;
  132. if (inStream.IsValidating())
  133. key = keys[i];
  134. inStream.Read(key);
  135. StatePair sp;
  136. if (inStream.IsValidating())
  137. sp = old_state[key];
  138. // Read offset
  139. inStream.Read(sp.first);
  140. // Read num contact points
  141. ContactPoints::size_type num_contacts;
  142. if (inStream.IsValidating())
  143. num_contacts = old_state[key].second.size();
  144. inStream.Read(num_contacts);
  145. // Read contact points
  146. sp.second.resize(num_contacts);
  147. inStream.ReadBytes(sp.second.data(), num_contacts * sizeof(Vec3));
  148. // Store the new value
  149. mState[key] = sp;
  150. }
  151. }
  152. void ContactListenerImpl::DrawState()
  153. {
  154. Trace("Draw Contact State");
  155. lock_guard lock(mStateMutex);
  156. for (const StateMap::value_type &kv : mState)
  157. for (Vec3 v : kv.second.second)
  158. DebugRenderer::sInstance->DrawWireSphere(kv.second.first + v, 0.05f, Color::sRed, 1);
  159. }