SubShapeIDPair.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Body/BodyID.h>
  5. #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
  6. #include <Jolt/Core/HashCombine.h>
  7. JPH_NAMESPACE_BEGIN
  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. JPH_OVERRIDE_NEW_DELETE
  13. /// Constructor
  14. SubShapeIDPair() = default;
  15. SubShapeIDPair(const BodyID &inBody1ID, const SubShapeID &inSubShapeID1, const BodyID &inBody2ID, const SubShapeID &inSubShapeID2) : mBody1ID(inBody1ID), mSubShapeID1(inSubShapeID1), mBody2ID(inBody2ID), mSubShapeID2(inSubShapeID2) { }
  16. SubShapeIDPair(const SubShapeIDPair &) = default;
  17. /// Equality operator
  18. inline bool operator == (const SubShapeIDPair &inRHS) const
  19. {
  20. return UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(this)) == UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(&inRHS));
  21. }
  22. /// Less than operator, used to consistently order contact points for a deterministic simulation
  23. inline bool operator < (const SubShapeIDPair &inRHS) const
  24. {
  25. if (mBody1ID != inRHS.mBody1ID)
  26. return mBody1ID < inRHS.mBody1ID;
  27. if (mSubShapeID1.GetValue() != inRHS.mSubShapeID1.GetValue())
  28. return mSubShapeID1.GetValue() < inRHS.mSubShapeID1.GetValue();
  29. if (mBody2ID != inRHS.mBody2ID)
  30. return mBody2ID < inRHS.mBody2ID;
  31. return mSubShapeID2.GetValue() < inRHS.mSubShapeID2.GetValue();
  32. }
  33. const BodyID & GetBody1ID() const { return mBody1ID; }
  34. const SubShapeID & GetSubShapeID1() const { return mSubShapeID1; }
  35. const BodyID & GetBody2ID() const { return mBody2ID; }
  36. const SubShapeID & GetSubShapeID2() const { return mSubShapeID2; }
  37. uint64 GetHash() const { return HashBytes(this, sizeof(SubShapeIDPair)); }
  38. private:
  39. BodyID mBody1ID;
  40. SubShapeID mSubShapeID1;
  41. BodyID mBody2ID;
  42. SubShapeID mSubShapeID2;
  43. };
  44. static_assert(sizeof(SubShapeIDPair) == 16, "Unexpected size");
  45. static_assert(alignof(SubShapeIDPair) == 4, "Assuming 4 byte aligned");
  46. JPH_NAMESPACE_END
  47. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  48. namespace std
  49. {
  50. /// Declare std::hash for SubShapeIDPair, note that std::hash is platform dependent and we need this one to be consistent because we sort on it in the ContactConstraintManager
  51. template <>
  52. struct hash<JPH::SubShapeIDPair>
  53. {
  54. inline size_t operator () (const JPH::SubShapeIDPair &inRHS) const
  55. {
  56. return static_cast<size_t>(inRHS.GetHash());
  57. }
  58. };
  59. }
  60. JPH_SUPPRESS_WARNINGS_STD_END