SubShapeIDPair.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/Body/BodyID.h>
  6. #include <Jolt/Physics/Collision/Shape/SubShapeID.h>
  7. #include <Jolt/Core/HashCombine.h>
  8. JPH_NAMESPACE_BEGIN
  9. /// A pair of bodies and their sub shape ID's. Can be used as a key in a map to find a contact point.
  10. class SubShapeIDPair
  11. {
  12. public:
  13. JPH_OVERRIDE_NEW_DELETE
  14. /// Constructor
  15. SubShapeIDPair() = default;
  16. SubShapeIDPair(const BodyID &inBody1ID, const SubShapeID &inSubShapeID1, const BodyID &inBody2ID, const SubShapeID &inSubShapeID2) : mBody1ID(inBody1ID), mSubShapeID1(inSubShapeID1), mBody2ID(inBody2ID), mSubShapeID2(inSubShapeID2) { }
  17. SubShapeIDPair(const SubShapeIDPair &) = default;
  18. /// Equality operator
  19. inline bool operator == (const SubShapeIDPair &inRHS) const
  20. {
  21. return UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(this)) == UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(&inRHS));
  22. }
  23. /// Less than operator, used to consistently order contact points for a deterministic simulation
  24. inline bool operator < (const SubShapeIDPair &inRHS) const
  25. {
  26. if (mBody1ID != inRHS.mBody1ID)
  27. return mBody1ID < inRHS.mBody1ID;
  28. if (mSubShapeID1.GetValue() != inRHS.mSubShapeID1.GetValue())
  29. return mSubShapeID1.GetValue() < inRHS.mSubShapeID1.GetValue();
  30. if (mBody2ID != inRHS.mBody2ID)
  31. return mBody2ID < inRHS.mBody2ID;
  32. return mSubShapeID2.GetValue() < inRHS.mSubShapeID2.GetValue();
  33. }
  34. const BodyID & GetBody1ID() const { return mBody1ID; }
  35. const SubShapeID & GetSubShapeID1() const { return mSubShapeID1; }
  36. const BodyID & GetBody2ID() const { return mBody2ID; }
  37. const SubShapeID & GetSubShapeID2() const { return mSubShapeID2; }
  38. uint64 GetHash() const { return HashBytes(this, sizeof(SubShapeIDPair)); }
  39. private:
  40. BodyID mBody1ID;
  41. SubShapeID mSubShapeID1;
  42. BodyID mBody2ID;
  43. SubShapeID mSubShapeID2;
  44. };
  45. static_assert(sizeof(SubShapeIDPair) == 16, "Unexpected size");
  46. static_assert(alignof(SubShapeIDPair) == 4, "Assuming 4 byte aligned");
  47. JPH_NAMESPACE_END
  48. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  49. namespace std
  50. {
  51. /// 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
  52. template <>
  53. struct hash<JPH::SubShapeIDPair>
  54. {
  55. inline size_t operator () (const JPH::SubShapeIDPair &inRHS) const
  56. {
  57. return static_cast<size_t>(inRHS.GetHash());
  58. }
  59. };
  60. }
  61. JPH_SUPPRESS_WARNINGS_STD_END