BodyID.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = 0x80000000; ///< 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. static constexpr uint cSequenceNumberShift = 23; ///< Number of bits to shift to get the sequence number
  17. /// Construct invalid body ID
  18. BodyID() :
  19. mID(cInvalidBodyID)
  20. {
  21. }
  22. /// Construct from index and sequence number combined in a single uint32 (use with care!)
  23. explicit BodyID(uint32 inID) :
  24. mID(inID)
  25. {
  26. JPH_ASSERT((inID & cBroadPhaseBit) == 0 || inID == cInvalidBodyID); // Check bit used by broadphase
  27. }
  28. /// Construct from index and sequence number
  29. explicit BodyID(uint32 inID, uint8 inSequenceNumber) :
  30. mID((uint32(inSequenceNumber) << cSequenceNumberShift) | inID)
  31. {
  32. JPH_ASSERT(inID <= cMaxBodyIndex); // Should not overlap with broadphase bit or sequence number
  33. }
  34. /// Get index in body array
  35. inline uint32 GetIndex() const
  36. {
  37. return mID & cMaxBodyIndex;
  38. }
  39. /// Get sequence number of body.
  40. /// The sequence number can be used to check if a body ID with the same body index has been reused by another body.
  41. /// 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.
  42. /// Functions querying the broadphase can (after acquiring a body lock) detect that the body has been removed (we assume that this won't happen more than 128 times in a row).
  43. inline uint8 GetSequenceNumber() const
  44. {
  45. return uint8(mID >> cSequenceNumberShift);
  46. }
  47. /// Returns the index and sequence number combined in an uint32
  48. inline uint32 GetIndexAndSequenceNumber() const
  49. {
  50. return mID;
  51. }
  52. /// Check if the ID is valid
  53. inline bool IsInvalid() const
  54. {
  55. return mID == cInvalidBodyID;
  56. }
  57. /// Equals check
  58. inline bool operator == (const BodyID &inRHS) const
  59. {
  60. return mID == inRHS.mID;
  61. }
  62. /// Not equals check
  63. inline bool operator != (const BodyID &inRHS) const
  64. {
  65. return mID != inRHS.mID;
  66. }
  67. /// Smaller than operator, can be used for sorting bodies
  68. inline bool operator < (const BodyID &inRHS) const
  69. {
  70. return mID < inRHS.mID;
  71. }
  72. /// Greater than operator, can be used for sorting bodies
  73. inline bool operator > (const BodyID &inRHS) const
  74. {
  75. return mID > inRHS.mID;
  76. }
  77. private:
  78. uint32 mID;
  79. };
  80. JPH_NAMESPACE_END
  81. // Create a std::hash/JPH::Hash for BodyID
  82. JPH_MAKE_HASHABLE(JPH::BodyID, t.GetIndexAndSequenceNumber())