Box2D 3.2.0
A 2D physics engine for games
Loading...
Searching...
No Matches
id.h
1// SPDX-FileCopyrightText: 2023 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
36
38typedef struct b2WorldId
39{
40 uint16_t index1;
41 uint16_t generation;
42} b2WorldId;
43
45typedef struct b2BodyId
46{
47 int32_t index1;
48 uint16_t world0;
49 uint16_t generation;
50} b2BodyId;
51
53typedef struct b2ShapeId
54{
55 int32_t index1;
56 uint16_t world0;
57 uint16_t generation;
58} b2ShapeId;
59
61typedef struct b2ChainId
62{
63 int32_t index1;
64 uint16_t world0;
65 uint16_t generation;
66} b2ChainId;
67
69typedef struct b2JointId
70{
71 int32_t index1;
72 uint16_t world0;
73 uint16_t generation;
74} b2JointId;
75
77typedef struct b2ContactId
78{
79 int32_t index1;
80 uint16_t world0;
81 int16_t padding;
82 uint32_t generation;
84
85// clang-format off
86#ifdef __cplusplus
88#define B2_NULL_ID {}
90#define B2_ID_INLINE inline
91#else
93#define B2_NULL_ID { 0 }
95#define B2_ID_INLINE static inline
96#endif
97// clang-format on
98
100static const b2WorldId b2_nullWorldId = B2_NULL_ID;
101static const b2BodyId b2_nullBodyId = B2_NULL_ID;
102static const b2ShapeId b2_nullShapeId = B2_NULL_ID;
103static const b2ChainId b2_nullChainId = B2_NULL_ID;
104static const b2JointId b2_nullJointId = B2_NULL_ID;
105static const b2ContactId b2_nullContactId = B2_NULL_ID;
106
108#define B2_IS_NULL( id ) ( ( id ).index1 == 0 )
109
111#define B2_IS_NON_NULL( id ) ( ( id ).index1 != 0 )
112
114#define B2_ID_EQUALS( id1, id2 ) \
115 ( ( id1 ).index1 == ( id2 ).index1 && ( id1 ).world0 == ( id2 ).world0 && \
116 ( id1 ).generation == ( id2 ).generation )
117
119B2_ID_INLINE uint32_t b2StoreWorldId( b2WorldId id )
120{
121 return ( (uint32_t)id.index1 << 16 ) | (uint32_t)id.generation;
122}
123
125B2_ID_INLINE b2WorldId b2LoadWorldId( uint32_t x )
126{
127 b2WorldId id = { (uint16_t)( x >> 16 ), (uint16_t)( x ) };
128 return id;
129}
130
132B2_ID_INLINE uint64_t b2StoreBodyId( b2BodyId id )
133{
134 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
135}
136
138B2_ID_INLINE b2BodyId b2LoadBodyId( uint64_t x )
139{
140 b2BodyId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
141 return id;
142}
143
145B2_ID_INLINE uint64_t b2StoreShapeId( b2ShapeId id )
146{
147 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
148}
149
151B2_ID_INLINE b2ShapeId b2LoadShapeId( uint64_t x )
152{
153 b2ShapeId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
154 return id;
155}
156
158B2_ID_INLINE uint64_t b2StoreChainId( b2ChainId id )
159{
160 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
161}
162
164B2_ID_INLINE b2ChainId b2LoadChainId( uint64_t x )
165{
166 b2ChainId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
167 return id;
168}
169
171B2_ID_INLINE uint64_t b2StoreJointId( b2JointId id )
172{
173 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
174}
175
177B2_ID_INLINE b2JointId b2LoadJointId( uint64_t x )
178{
179 b2JointId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
180 return id;
181}
182
184B2_ID_INLINE void b2StoreContactId( b2ContactId id, uint32_t values[3] )
185{
186 values[0] = (uint32_t)id.index1;
187 values[1] = (uint32_t)id.world0;
188 values[2] = (uint32_t)id.generation;
189}
190
192B2_ID_INLINE b2ContactId b2LoadContactId( uint32_t values[3] )
193{
194 b2ContactId id;
195 id.index1 = (int32_t)values[0];
196 id.world0 = (uint16_t)values[1];
197 id.padding = 0;
198 id.generation = (uint32_t)values[2];
199 return id;
200}
201
#define B2_NULL_ID
A null id. Works for any id type.
Definition id.h:93
#define B2_ID_INLINE
Inline function.
Definition id.h:95
Body id references a body instance. This should be treated as an opaque handle.
Definition id.h:46
Chain id references a chain instance. This should be treated as an opaque handle.
Definition id.h:62
Contact id references a contact instance. This should be treated as an opaque handle.
Definition id.h:78
Joint id references a joint instance. This should be treated as an opaque handle.
Definition id.h:70
Shape id references a shape instance. This should be treated as an opaque handle.
Definition id.h:54
World id references a world instance. This should be treated as an opaque handle.
Definition id.h:39