BodyID.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/Core/HashCombine.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding race conditions.
  8. class BodyID
  9. {
  10. public:
  11. JPH_OVERRIDE_NEW_DELETE
  12. static constexpr uint32 cInvalidBodyID = 0xffffffff; ///< The value for an invalid body ID
  13. static constexpr uint32 cBroadPhaseBit = 0x00800000; ///< This bit is used by the broadphase
  14. static constexpr uint32 cMaxBodyIndex = 0x7fffff; ///< Maximum value for body index (also the maximum amount of bodies supported - 1)
  15. static constexpr uint8 cMaxSequenceNumber = 0xff; ///< Maximum value for the sequence number
  16. /// Construct invalid body ID
  17. BodyID() :
  18. mID(cInvalidBodyID)
  19. {
  20. }
  21. /// Construct from index and sequence number combined in a single uint32 (use with care!)
  22. explicit BodyID(uint32 inID) :
  23. mID(inID)
  24. {
  25. JPH_ASSERT((inID & cBroadPhaseBit) == 0 || inID == cInvalidBodyID); // Check bit used by broadphase
  26. }
  27. /// Construct from index and sequence number
  28. explicit BodyID(uint32 inID, uint8 inSequenceNumber) :
  29. mID((uint32(inSequenceNumber) << 24) | inID)
  30. {
  31. JPH_ASSERT(inID < cMaxBodyIndex); // Should not use bit pattern for invalid ID and should not use the broadphase bit
  32. }
  33. /// Get index in body array
  34. inline uint32 GetIndex() const
  35. {
  36. return mID & cMaxBodyIndex;
  37. }
  38. /// Get sequence number of body.
  39. /// The sequence number can be used to check if a body ID with the same body index has been reused by another body.
  40. /// It is mainly used in multi threaded situations where a body is removed and its body index is immediately reused by a body created from another thread.
  41. /// Functions querying the broadphase can (after aquiring a body lock) detect that the body has been removed (we assume that this won't happen more than 128 times in a row).
  42. inline uint8 GetSequenceNumber() const
  43. {
  44. return uint8(mID >> 24);
  45. }
  46. /// Returns the index and sequence number combined in an uint32
  47. inline uint32 GetIndexAndSequenceNumber() const
  48. {
  49. return mID;
  50. }
  51. /// Check if the ID is valid
  52. inline bool IsInvalid() const
  53. {
  54. return mID == cInvalidBodyID;
  55. }
  56. /// Equals check
  57. inline bool operator == (const BodyID &inRHS) const
  58. {
  59. return mID == inRHS.mID;
  60. }
  61. /// Not equals check
  62. inline bool operator != (const BodyID &inRHS) const
  63. {
  64. return mID != inRHS.mID;
  65. }
  66. /// Smaller than operator, can be used for sorting bodies
  67. inline bool operator < (const BodyID &inRHS) const
  68. {
  69. return mID < inRHS.mID;
  70. }
  71. /// Greater than operator, can be used for sorting bodies
  72. inline bool operator > (const BodyID &inRHS) const
  73. {
  74. return mID > inRHS.mID;
  75. }
  76. private:
  77. uint32 mID;
  78. };
  79. JPH_NAMESPACE_END
  80. // Create a std::hash for BodyID
  81. JPH_MAKE_HASHABLE(JPH::BodyID, t.GetIndexAndSequenceNumber())