Debian Codegraph @

thirdparty/jolt_physics/Jolt/Physics/Body/BodyID.h

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