id.h (3997B)
1 // SPDX-FileCopyrightText: 2023 Erin Catto 2 // SPDX-License-Identifier: MIT 3 4 #pragma once 5 6 #include "base.h" 7 8 #include <stdint.h> 9 10 /** 11 * @defgroup id Ids 12 * These ids serve as handles to internal Box2D objects. 13 * These should be considered opaque data and passed by value. 14 * Include this header if you need the id types and not the whole Box2D API. 15 * All ids are considered null if initialized to zero. 16 * 17 * For example in C++: 18 * 19 * @code{.cxx} 20 * b2WorldId worldId = {}; 21 * @endcode 22 * 23 * Or in C: 24 * 25 * @code{.c} 26 * b2WorldId worldId = {0}; 27 * @endcode 28 * 29 * These are both considered null. 30 * 31 * @warning Do not use the internals of these ids. They are subject to change. Ids should be treated as opaque objects. 32 * @warning You should use ids to access objects in Box2D. Do not access files within the src folder. Such usage is unsupported. 33 * @{ 34 */ 35 36 /// World id references a world instance. This should be treated as an opaque handle. 37 typedef struct b2WorldId 38 { 39 uint16_t index1; 40 uint16_t generation; 41 } b2WorldId; 42 43 /// Body id references a body instance. This should be treated as an opaque handle. 44 typedef struct b2BodyId 45 { 46 int32_t index1; 47 uint16_t world0; 48 uint16_t generation; 49 } b2BodyId; 50 51 /// Shape id references a shape instance. This should be treated as an opaque handle. 52 typedef struct b2ShapeId 53 { 54 int32_t index1; 55 uint16_t world0; 56 uint16_t generation; 57 } b2ShapeId; 58 59 /// Chain id references a chain instances. This should be treated as an opaque handle. 60 typedef struct b2ChainId 61 { 62 int32_t index1; 63 uint16_t world0; 64 uint16_t generation; 65 } b2ChainId; 66 67 /// Joint id references a joint instance. This should be treated as an opaque handle. 68 typedef struct b2JointId 69 { 70 int32_t index1; 71 uint16_t world0; 72 uint16_t generation; 73 } b2JointId; 74 75 /// Use these to make your identifiers null. 76 /// You may also use zero initialization to get null. 77 static const b2WorldId b2_nullWorldId = B2_ZERO_INIT; 78 static const b2BodyId b2_nullBodyId = B2_ZERO_INIT; 79 static const b2ShapeId b2_nullShapeId = B2_ZERO_INIT; 80 static const b2ChainId b2_nullChainId = B2_ZERO_INIT; 81 static const b2JointId b2_nullJointId = B2_ZERO_INIT; 82 83 /// Macro to determine if any id is null. 84 #define B2_IS_NULL( id ) ( id.index1 == 0 ) 85 86 /// Macro to determine if any id is non-null. 87 #define B2_IS_NON_NULL( id ) ( id.index1 != 0 ) 88 89 /// Compare two ids for equality. Doesn't work for b2WorldId. 90 #define B2_ID_EQUALS( id1, id2 ) ( id1.index1 == id2.index1 && id1.world0 == id2.world0 && id1.generation == id2.generation ) 91 92 /// Store a body id into a uint64_t. 93 B2_INLINE uint64_t b2StoreBodyId( b2BodyId id ) 94 { 95 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation; 96 } 97 98 /// Load a uint64_t into a body id. 99 B2_INLINE b2BodyId b2LoadBodyId( uint64_t x ) 100 { 101 b2BodyId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) }; 102 return id; 103 } 104 105 /// Store a shape id into a uint64_t. 106 B2_INLINE uint64_t b2StoreShapeId( b2ShapeId id ) 107 { 108 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation; 109 } 110 111 /// Load a uint64_t into a shape id. 112 B2_INLINE b2ShapeId b2LoadShapeId( uint64_t x ) 113 { 114 b2ShapeId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) }; 115 return id; 116 } 117 118 /// Store a chain id into a uint64_t. 119 B2_INLINE uint64_t b2StoreChainId( b2ChainId id ) 120 { 121 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation; 122 } 123 124 /// Load a uint64_t into a chain id. 125 B2_INLINE b2ChainId b2LoadChainId( uint64_t x ) 126 { 127 b2ChainId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) }; 128 return id; 129 } 130 131 /// Store a joint id into a uint64_t. 132 B2_INLINE uint64_t b2StoreJointId( b2JointId id ) 133 { 134 return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation; 135 } 136 137 /// Load a uint64_t into a joint id. 138 B2_INLINE b2JointId b2LoadJointId( uint64_t x ) 139 { 140 b2JointId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) }; 141 return id; 142 } 143 144 /**@}*/