SubShapeIDPair.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Body/BodyID.h>
  5. #include <Physics/Collision/Shape/SubShapeID.h>
  6. #include <Core/HashCombine.h>
  7. namespace JPH {
  8. /// A pair of bodies and their sub shape ID's. Can be used as a key in a map to find a contact point.
  9. class SubShapeIDPair
  10. {
  11. public:
  12. /// Constructor
  13. SubShapeIDPair() = default;
  14. SubShapeIDPair(const BodyID &inBody1ID, const SubShapeID &inSubShapeID1, const BodyID &inBody2ID, const SubShapeID &inSubShapeID2) : mBody1ID(inBody1ID), mSubShapeID1(inSubShapeID1), mBody2ID(inBody2ID), mSubShapeID2(inSubShapeID2) { }
  15. SubShapeIDPair(const SubShapeIDPair &) = default;
  16. /// Equality operator
  17. inline bool operator == (const SubShapeIDPair &inRHS) const
  18. {
  19. return UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(this)) == UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(&inRHS));
  20. }
  21. /// Less than operator, used to consistently order contact points for a deterministic simulation
  22. inline bool operator < (const SubShapeIDPair &inRHS) const
  23. {
  24. if (mBody1ID != inRHS.mBody1ID)
  25. return mBody1ID < inRHS.mBody1ID;
  26. if (mSubShapeID1.GetValue() != inRHS.mSubShapeID1.GetValue())
  27. return mSubShapeID1.GetValue() < inRHS.mSubShapeID1.GetValue();
  28. if (mBody2ID != inRHS.mBody2ID)
  29. return mBody2ID < inRHS.mBody2ID;
  30. return mSubShapeID2.GetValue() < inRHS.mSubShapeID2.GetValue();
  31. }
  32. const BodyID & GetBody1ID() const { return mBody1ID; }
  33. const SubShapeID & GetSubShapeID1() const { return mSubShapeID1; }
  34. const BodyID & GetBody2ID() const { return mBody2ID; }
  35. const SubShapeID & GetSubShapeID2() const { return mSubShapeID2; }
  36. private:
  37. BodyID mBody1ID;
  38. SubShapeID mSubShapeID1;
  39. BodyID mBody2ID;
  40. SubShapeID mSubShapeID2;
  41. };
  42. static_assert(sizeof(SubShapeIDPair) == 16, "Unexpected size");
  43. static_assert(alignof(SubShapeIDPair) == 4, "Assuming 4 byte aligned");
  44. } // JPH
  45. JPH_MAKE_HASHABLE(JPH::SubShapeIDPair, t.GetBody1ID().GetIndexAndSequenceNumber(), t.GetSubShapeID1().GetValue(), t.GetBody2ID().GetIndexAndSequenceNumber(), t.GetSubShapeID2().GetValue())