SubShapeIDPair.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 & operator = (const SubShapeIDPair &) = default;
  18. SubShapeIDPair(const SubShapeIDPair &) = default;
  19. /// Equality operator
  20. inline bool operator == (const SubShapeIDPair &inRHS) const
  21. {
  22. return UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(this)) == UVec4::sLoadInt4(reinterpret_cast<const uint32 *>(&inRHS));
  23. }
  24. /// Less than operator, used to consistently order contact points for a deterministic simulation
  25. inline bool operator < (const SubShapeIDPair &inRHS) const
  26. {
  27. if (mBody1ID != inRHS.mBody1ID)
  28. return mBody1ID < inRHS.mBody1ID;
  29. if (mSubShapeID1.GetValue() != inRHS.mSubShapeID1.GetValue())
  30. return mSubShapeID1.GetValue() < inRHS.mSubShapeID1.GetValue();
  31. if (mBody2ID != inRHS.mBody2ID)
  32. return mBody2ID < inRHS.mBody2ID;
  33. return mSubShapeID2.GetValue() < inRHS.mSubShapeID2.GetValue();
  34. }
  35. const BodyID & GetBody1ID() const { return mBody1ID; }
  36. const SubShapeID & GetSubShapeID1() const { return mSubShapeID1; }
  37. const BodyID & GetBody2ID() const { return mBody2ID; }
  38. const SubShapeID & GetSubShapeID2() const { return mSubShapeID2; }
  39. uint64 GetHash() const { return HashBytes(this, sizeof(SubShapeIDPair)); }
  40. private:
  41. BodyID mBody1ID;
  42. SubShapeID mSubShapeID1;
  43. BodyID mBody2ID;
  44. SubShapeID mSubShapeID2;
  45. };
  46. static_assert(sizeof(SubShapeIDPair) == 16, "Unexpected size");
  47. static_assert(alignof(SubShapeIDPair) == 4, "Assuming 4 byte aligned");
  48. JPH_NAMESPACE_END
  49. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  50. namespace std
  51. {
  52. /// 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
  53. template <>
  54. struct hash<JPH::SubShapeIDPair>
  55. {
  56. inline size_t operator () (const JPH::SubShapeIDPair &inRHS) const
  57. {
  58. return static_cast<size_t>(inRHS.GetHash());
  59. }
  60. };
  61. }
  62. JPH_SUPPRESS_WARNINGS_STD_END