Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
BodyID.h
Go to the documentation of this file.
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
8
10
12class BodyID
13{
14public:
16
17 static constexpr uint32 cInvalidBodyID = 0xffffffff;
18 static constexpr uint32 cBroadPhaseBit = 0x00800000;
19 static constexpr uint32 cMaxBodyIndex = 0x7fffff;
20 static constexpr uint8 cMaxSequenceNumber = 0xff;
21
25 {
26 }
27
29 explicit BodyID(uint32 inID) :
30 mID(inID)
31 {
32 JPH_ASSERT((inID & cBroadPhaseBit) == 0 || inID == cInvalidBodyID); // Check bit used by broadphase
33 }
34
36 explicit BodyID(uint32 inID, uint8 inSequenceNumber) :
37 mID((uint32(inSequenceNumber) << 24) | inID)
38 {
39 JPH_ASSERT(inID < cMaxBodyIndex); // Should not use bit pattern for invalid ID and should not use the broadphase bit
40 }
41
43 inline uint32 GetIndex() const
44 {
45 return mID & cMaxBodyIndex;
46 }
47
52 inline uint8 GetSequenceNumber() const
53 {
54 return uint8(mID >> 24);
55 }
56
59 {
60 return mID;
61 }
62
64 inline bool IsInvalid() const
65 {
66 return mID == cInvalidBodyID;
67 }
68
70 inline bool operator == (const BodyID &inRHS) const
71 {
72 return mID == inRHS.mID;
73 }
74
76 inline bool operator != (const BodyID &inRHS) const
77 {
78 return mID != inRHS.mID;
79 }
80
82 inline bool operator < (const BodyID &inRHS) const
83 {
84 return mID < inRHS.mID;
85 }
86
88 inline bool operator > (const BodyID &inRHS) const
89 {
90 return mID > inRHS.mID;
91 }
92
93private:
94 uint32 mID;
95};
96
98
99// Create a std::hash for BodyID
100JPH_MAKE_HASHABLE(JPH::BodyID, t.GetIndexAndSequenceNumber())
std::uint8_t uint8
Definition: Core.h:453
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_MAKE_HASHABLE(type,...)
Definition: HashCombine.h:87
#define JPH_ASSERT(...)
Definition: IssueReporting.h:33
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:31
ID of a body. This is a way of reasoning about bodies in a multithreaded simulation while avoiding ra...
Definition: BodyID.h:13
bool IsInvalid() const
Check if the ID is valid.
Definition: BodyID.h:64
bool operator<(const BodyID &inRHS) const
Smaller than operator, can be used for sorting bodies.
Definition: BodyID.h:82
BodyID()
Construct invalid body ID.
Definition: BodyID.h:23
BodyID(uint32 inID)
Construct from index and sequence number combined in a single uint32 (use with care!...
Definition: BodyID.h:29
BodyID(uint32 inID, uint8 inSequenceNumber)
Construct from index and sequence number.
Definition: BodyID.h:36
bool operator==(const BodyID &inRHS) const
Equals check.
Definition: BodyID.h:70
bool operator!=(const BodyID &inRHS) const
Not equals check.
Definition: BodyID.h:76
static constexpr uint32 cMaxBodyIndex
Maximum value for body index (also the maximum amount of bodies supported - 1)
Definition: BodyID.h:19
static constexpr uint32 cBroadPhaseBit
This bit is used by the broadphase.
Definition: BodyID.h:18
bool operator>(const BodyID &inRHS) const
Greater than operator, can be used for sorting bodies.
Definition: BodyID.h:88
uint32 GetIndexAndSequenceNumber() const
Returns the index and sequence number combined in an uint32.
Definition: BodyID.h:58
static constexpr uint8 cMaxSequenceNumber
Maximum value for the sequence number.
Definition: BodyID.h:20
uint32 GetIndex() const
Get index in body array.
Definition: BodyID.h:43
uint8 GetSequenceNumber() const
Definition: BodyID.h:52
static JPH_OVERRIDE_NEW_DELETE constexpr uint32 cInvalidBodyID
The value for an invalid body ID.
Definition: BodyID.h:17