Box3D 0.1.0
A 3D physics engine for games
Loading...
Searching...
No Matches
id.h
1// SPDX-FileCopyrightText: 2026 Erin Catto
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <stdint.h>
7
8// Note: this file should be stand-alone
9
35
37typedef struct b3WorldId
38{
39 uint16_t index1;
40 uint16_t generation;
41} b3WorldId;
42
44typedef struct b3BodyId
45{
46 int32_t index1;
47 uint16_t world0;
48 uint16_t generation;
49} b3BodyId;
50
52typedef struct b3ShapeId
53{
54 int32_t index1;
55 uint16_t world0;
56 uint16_t generation;
57} b3ShapeId;
58
60typedef struct b3JointId
61{
62 int32_t index1;
63 uint16_t world0;
64 uint16_t generation;
65} b3JointId;
66
68typedef struct b3ContactId
69{
70 int32_t index1;
71 uint16_t world0;
72 int16_t padding;
73 uint32_t generation;
75
76// clang-format off
77#ifdef __cplusplus
79 #define B3_NULL_ID {}
80 #define B3_ID_INLINE inline
81#else
83 #define B3_NULL_ID { 0 }
84
86 #define B3_ID_INLINE static inline
87#endif
88// clang-format on
89
92static const b3WorldId b3_nullWorldId = B3_NULL_ID;
93static const b3BodyId b3_nullBodyId = B3_NULL_ID;
94static const b3ShapeId b3_nullShapeId = B3_NULL_ID;
95static const b3JointId b3_nullJointId = B3_NULL_ID;
96static const b3ContactId b3_nullContactId = B3_NULL_ID;
97
99#define B3_IS_NULL( id ) ( id.index1 == 0 )
100
102#define B3_IS_NON_NULL( id ) ( id.index1 != 0 )
103
105#define B3_ID_EQUALS( id1, id2 ) ( id1.index1 == id2.index1 && id1.world0 == id2.world0 && id1.generation == id2.generation )
106
109{
110 return ( (uint32_t)id.index1 << 16 ) | (uint32_t)id.generation;
111}
112
115{
116 b3WorldId id = { (uint16_t)( x >> 16 ), (uint16_t)( x ) };
117 return id;
118}
119
122{
123 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
124}
125
128{
129 b3BodyId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
130 return id;
131}
132
135{
136 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
137}
138
141{
142 b3ShapeId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
143 return id;
144}
145
148{
149 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
150}
151
154{
155 b3JointId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
156 return id;
157}
158
160B3_ID_INLINE void b3StoreContactId( b3ContactId id, uint32_t values[3] )
161{
162 values[0] = (uint32_t)id.index1;
163 values[1] = (uint32_t)id.world0;
164 values[2] = (uint32_t)id.generation;
165}
166
169{
170 b3ContactId id;
171 id.index1 = (int32_t)values[0];
172 id.world0 = (uint16_t)values[1];
173 id.padding = 0;
174 id.generation = (uint32_t)values[2];
175 return id;
176}
177
B3_ID_INLINE b3WorldId b3LoadWorldId(uint32_t x)
Load a uint32_t into a world id.
Definition id.h:114
B3_ID_INLINE uint32_t b3StoreWorldId(b3WorldId id)
Store a world id into a uint32_t.
Definition id.h:108
B3_ID_INLINE b3ShapeId b3LoadShapeId(uint64_t x)
Load a uint64_t into a shape id.
Definition id.h:140
B3_ID_INLINE b3ContactId b3LoadContactId(uint32_t values[3])
Load a contact id from three uint32 values.
Definition id.h:168
B3_ID_INLINE uint64_t b3StoreJointId(b3JointId id)
Store a joint id into a uint64_t.
Definition id.h:147
#define B3_ID_INLINE
This macro bridges C and C++ inline functions. C++ has the one definition rule that C lacks.
Definition id.h:86
B3_ID_INLINE uint64_t b3StoreShapeId(b3ShapeId id)
Store a shape id into a uint64_t.
Definition id.h:134
B3_ID_INLINE uint64_t b3StoreBodyId(b3BodyId id)
Store a body id into a uint64_t.
Definition id.h:121
B3_ID_INLINE b3JointId b3LoadJointId(uint64_t x)
Load a uint64_t into a joint id.
Definition id.h:153
B3_ID_INLINE b3BodyId b3LoadBodyId(uint64_t x)
Load a uint64_t into a body id.
Definition id.h:127
#define B3_NULL_ID
A null id. Works for any id type.
Definition id.h:83
B3_ID_INLINE void b3StoreContactId(b3ContactId id, uint32_t values[3])
Store a contact id into three uint32 values.
Definition id.h:160
Body id references a body instance. This should be treated as an opaque handle.
Definition id.h:45
Contact id references a contact instance. This should be treated as an opaque handle.
Definition id.h:69
Joint id references a joint instance. This should be treated as an opaque handle.
Definition id.h:61
Shape id references a shape instance. This should be treated as an opaque handle.
Definition id.h:53
World id references a world instance. This should be treated as an opaque handle.
Definition id.h:38