BodyID.h 2.8 KB

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