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 = 0x80000000;
19 static constexpr uint32 cMaxBodyIndex = 0x7fffff;
20 static constexpr uint8 cMaxSequenceNumber = 0xff;
21 static constexpr uint cSequenceNumberShift = 23;
22
26 {
27 }
28
30 explicit BodyID(uint32 inID) :
31 mID(inID)
32 {
33 JPH_ASSERT((inID & cBroadPhaseBit) == 0 || inID == cInvalidBodyID); // Check bit used by broadphase
34 }
35
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
44 inline uint32 GetIndex() const
45 {
46 return mID & cMaxBodyIndex;
47 }
48
53 inline uint8 GetSequenceNumber() const
54 {
55 return uint8(mID >> cSequenceNumberShift);
56 }
57
60 {
61 return mID;
62 }
63
65 inline bool IsInvalid() const
66 {
67 return mID == cInvalidBodyID;
68 }
69
71 inline bool operator == (const BodyID &inRHS) const
72 {
73 return mID == inRHS.mID;
74 }
75
77 inline bool operator != (const BodyID &inRHS) const
78 {
79 return mID != inRHS.mID;
80 }
81
83 inline bool operator < (const BodyID &inRHS) const
84 {
85 return mID < inRHS.mID;
86 }
87
89 inline bool operator > (const BodyID &inRHS) const
90 {
91 return mID > inRHS.mID;
92 }
93
94private:
95 uint32 mID;
96};
97
99
100// Create a std::hash/JPH::Hash for BodyID
101JPH_MAKE_HASHABLE(JPH::BodyID, t.GetIndexAndSequenceNumber())
std::uint8_t uint8
Definition Core.h:487
unsigned int uint
Definition Core.h:486
#define JPH_NAMESPACE_END
Definition Core.h:418
std::uint32_t uint32
Definition Core.h:489
#define JPH_NAMESPACE_BEGIN
Definition Core.h:412
#define JPH_MAKE_HASHABLE(type,...)
Definition HashCombine.h:223
#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:65
bool operator<(const BodyID &inRHS) const
Smaller than operator, can be used for sorting bodies.
Definition BodyID.h:83
BodyID()
Construct invalid body ID.
Definition BodyID.h:24
BodyID(uint32 inID)
Construct from index and sequence number combined in a single uint32 (use with care!...
Definition BodyID.h:30
BodyID(uint32 inID, uint8 inSequenceNumber)
Construct from index and sequence number.
Definition BodyID.h:37
bool operator==(const BodyID &inRHS) const
Equals check.
Definition BodyID.h:71
bool operator!=(const BodyID &inRHS) const
Not equals check.
Definition BodyID.h:77
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:89
uint32 GetIndexAndSequenceNumber() const
Returns the index and sequence number combined in an uint32.
Definition BodyID.h:59
static constexpr uint8 cMaxSequenceNumber
Maximum value for the sequence number.
Definition BodyID.h:20
static constexpr uint cSequenceNumberShift
Number of bits to shift to get the sequence number.
Definition BodyID.h:21
uint32 GetIndex() const
Get index in body array.
Definition BodyID.h:44
uint8 GetSequenceNumber() const
Definition BodyID.h:53
static JPH_OVERRIDE_NEW_DELETE constexpr uint32 cInvalidBodyID
The value for an invalid body ID.
Definition BodyID.h:17