box2d 3.0.0
A 2D physics engine for games
 
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2023 Erin Catto
2// SPDX-License-Identifier: MIT
3
12#pragma once
13
14#include "box2d/color.h"
15#include "box2d/constants.h"
16#include "box2d/id.h"
17
18#include <stdbool.h>
19#include <stddef.h>
20#include <stdint.h>
21
22#ifdef __cplusplus
23#define B2_LITERAL(T) T
24#define B2_ZERO_INIT \
25 { \
26 }
27#else
29#define B2_LITERAL(T) (T)
30
32#define B2_ZERO_INIT \
33 { \
34 0 \
35 }
36#endif
37
39#define B2_ARRAY_COUNT(A) (int)(sizeof(A) / sizeof(A[0]))
40
42#define B2_MAYBE_UNUSED(x) ((void)(x))
43
46typedef struct b2Vec2
47{
49 float x, y;
50} b2Vec2;
51
53typedef struct b2Rot
54{
56 float s, c;
57} b2Rot;
58
60typedef struct b2Transform
61{
62 b2Vec2 p;
63 b2Rot q;
65
67typedef struct b2Mat22
68{
71} b2Mat22;
72
74typedef struct b2AABB
75{
76 b2Vec2 lowerBound;
77 b2Vec2 upperBound;
78} b2AABB;
79
81typedef struct b2RayCastInput
82{
83 b2Vec2 origin, translation;
84 float maxFraction;
86
88typedef struct b2ShapeCastInput
89{
91 int32_t count;
92 float radius;
93 b2Vec2 translation;
94 float maxFraction;
96
98typedef struct b2CastOutput
99{
100 b2Vec2 normal;
101 b2Vec2 point;
102 float fraction;
103 int32_t iterations;
104 bool hit;
106
112typedef void b2TaskCallback(int32_t startIndex, int32_t endIndex, uint32_t threadIndex, void* taskContext);
113
116typedef void* b2EnqueueTaskCallback(b2TaskCallback* task, int32_t itemCount, int32_t minRange, void* taskContext,
117 void* userContext);
118
120typedef void b2FinishTaskCallback(void* userTask, void* userContext);
121
123typedef struct b2WorldDef
124{
127
131
134
137
140
143
146
149
152
155
160
162 uint32_t workerCount;
163 b2EnqueueTaskCallback* enqueueTask;
164 b2FinishTaskCallback* finishTask;
165 void* userTaskContext;
166} b2WorldDef;
167
169static inline b2WorldDef b2DefaultWorldDef(void)
170{
172 def.gravity = B2_LITERAL(b2Vec2){0.0f, -10.0f};
175 def.contactHertz = 30.0f;
176 def.contactDampingRatio = 1.0f;
177 def.enableSleep = true;
178 def.bodyCapacity = 8;
179 def.shapeCapacity = 8;
180 def.contactCapacity = 8;
181 def.jointCapacity = 8;
182 def.stackAllocatorCapacity = 1024 * 1024;
183 return def;
184}
185
190typedef enum b2BodyType
191{
192 b2_staticBody = 0,
193 b2_kinematicBody = 1,
194 b2_dynamicBody = 2,
195 b2_bodyTypeCount
196} b2BodyType;
197
200typedef struct b2BodyDef
201{
205
209
211 float angle;
212
215
218
223
228
231
233 void* userData;
234
238
241
244
247} b2BodyDef;
248
250static const b2BodyDef b2_defaultBodyDef = {
251 b2_staticBody, {0.0f, 0.0f}, 0.0f, {0.0f, 0.0f}, 0.0f, 0.0f, 0.0f, 1.0f, NULL, true, true, false, true,
252};
253
255typedef struct b2Filter
256{
258 uint32_t categoryBits;
259
262 uint32_t maskBits;
263
267 int32_t groupIndex;
268} b2Filter;
269
271static const b2Filter b2_defaultFilter = {0x00000001, 0xFFFFFFFF, 0};
272
274typedef struct b2QueryFilter
275{
277 uint32_t categoryBits;
278
281 uint32_t maskBits;
283
285static const b2QueryFilter b2_defaultQueryFilter = {0x00000001, 0xFFFFFFFF};
286
288typedef enum b2ShapeType
289{
290 b2_capsuleShape,
291 b2_circleShape,
292 b2_polygonShape,
293 b2_segmentShape,
294 b2_smoothSegmentShape,
295 b2_shapeTypeCount
297
299typedef struct b2ShapeDef
300{
302 void* userData;
303
305 float friction;
306
309
311 float density;
312
315
318
321
324
328
329} b2ShapeDef;
330
332static const b2ShapeDef b2_defaultShapeDef = {
333 NULL, 0.6f, 0.0f, 1.0f, {0x00000001, 0xFFFFFFFF, 0}, false, true, true, false,
334};
335
348typedef struct b2ChainDef
349{
352
354 int32_t count;
355
357 bool loop;
358
360 void* userData;
361
363 float friction;
364
367
370} b2ChainDef;
371
373static const b2ChainDef b2_defaultChainDef = {NULL, 0, false, NULL, 0.6f, 0.0f, {0x00000001, 0xFFFFFFFF, 0}};
#define b2_maxPolygonVertices
Definition constants.h:47
#define b2_lengthUnitsPerMeter
Definition constants.h:22
b2Filter filter
Contact filtering data.
Definition types.h:369
int32_t stackAllocatorCapacity
Definition types.h:159
b2Vec2 linearVelocity
The linear velocity of the body's origin in world co-ordinates.
Definition types.h:214
bool fixedRotation
Should this body be prevented from rotating? Useful for characters.
Definition types.h:243
float restitution
The restitution (bounce) usually in the range [0,1].
Definition types.h:308
bool loop
Indicates a closed chain formed by connecting the first and last points.
Definition types.h:357
float s
Sine and cosine.
Definition types.h:56
int32_t jointCapacity
Capacity for joints.
Definition types.h:154
uint32_t categoryBits
The collision category bits. Normally you would just set one bit.
Definition types.h:277
void * userData
Use this to store application specific shape data.
Definition types.h:360
bool enablePreSolveEvents
Definition types.h:327
int32_t contactCapacity
Capacity for contacts. This may not be exceeded.
Definition types.h:151
float contactHertz
Contact stiffness. Cycles per second.
Definition types.h:136
float friction
The friction coefficient, usually in the range [0,1].
Definition types.h:305
int32_t groupIndex
Definition types.h:267
uint32_t maskBits
Definition types.h:262
b2Vec2 position
Definition types.h:208
bool isSensor
A sensor shape collects contact information but never generates a collision response.
Definition types.h:317
float contactDampingRatio
Contact bounciness. Non-dimensional.
Definition types.h:139
void * userData
Use this to store application specific shape data.
Definition types.h:302
b2BodyType
Definition types.h:191
float x
coordinates
Definition types.h:49
b2ShapeType
Shape type.
Definition types.h:289
uint32_t categoryBits
The collision category bits. Normally you would just set one bit.
Definition types.h:258
#define B2_LITERAL(T)
Used for C literals like (b2Vec2){1.0f, 2.0f} where C++ requires b2Vec2{1.0f, 2.0f}.
Definition types.h:29
const b2Vec2 * points
An array of at least 4 points. These are cloned and may be temporary.
Definition types.h:351
bool isEnabled
Does this body start out enabled?
Definition types.h:246
uint32_t workerCount
task system hookup
Definition types.h:162
b2BodyType type
Definition types.h:204
bool enableContactEvents
Enable contact events for this shape. Only applies to kinematic and dynamic bodies....
Definition types.h:323
b2Vec2 gravity
Gravity vector. Box2D has no up-vector defined.
Definition types.h:126
float restitution
The restitution (elasticity) usually in the range [0,1].
Definition types.h:366
float linearDamping
Definition types.h:222
bool enableSleep
Can bodies go to sleep to improve performance.
Definition types.h:142
float angularVelocity
The angular velocity of the body.
Definition types.h:217
float friction
The friction coefficient, usually in the range [0,1].
Definition types.h:363
float restitutionThreshold
Definition types.h:130
b2Filter filter
Contact filtering data.
Definition types.h:314
float density
The density, usually in kg/m^2.
Definition types.h:311
int32_t bodyCapacity
Capacity for bodies. This may not be exceeded.
Definition types.h:145
b2Vec2 cx
columns
Definition types.h:70
uint32_t maskBits
Definition types.h:281
float angle
The world angle of the body in radians.
Definition types.h:211
void * b2EnqueueTaskCallback(b2TaskCallback *task, int32_t itemCount, int32_t minRange, void *taskContext, void *userContext)
Definition types.h:116
#define B2_ZERO_INIT
Used for C zero initialization, such as b2Vec2 v = {0} where C++ requires b2Vec2 v = {}.
Definition types.h:32
float gravityScale
Scale the gravity applied to this body.
Definition types.h:230
float contactPushoutVelocity
This parameter controls how fast overlap is resolved and has units of meters per second.
Definition types.h:133
bool enableSensorEvents
Enable sensor events for this shape. Only applies to kinematic and dynamic bodies....
Definition types.h:320
int32_t count
The point count, must be 4 or more.
Definition types.h:354
void * userData
Use this to store application specific body data.
Definition types.h:233
int32_t shapeCapacity
initial capacity for shapes
Definition types.h:148
bool isAwake
Is this body initially awake or sleeping?
Definition types.h:240
void b2FinishTaskCallback(void *userTask, void *userContext)
Finishes a user task object that wraps a Box2D task.
Definition types.h:120
bool enableSleep
Definition types.h:237
float angularDamping
Definition types.h:227
void b2TaskCallback(int32_t startIndex, int32_t endIndex, uint32_t threadIndex, void *taskContext)
Definition types.h:112
Axis-aligned bounding box.
Definition types.h:75
Definition types.h:201
Definition types.h:349
This holds contact filtering data.
Definition types.h:256
A 2-by-2 Matrix.
Definition types.h:68
This holds contact filtering data.
Definition types.h:275
Low level ray-cast input data.
Definition types.h:82
Low level ray-cast or shape-cast output data.
Definition types.h:99
2D rotation
Definition types.h:54
Low level hape cast input in generic form.
Definition types.h:89
Used to create a shape.
Definition types.h:300
A 2D rigid transform.
Definition types.h:61
Definition types.h:47
World definition used to create a simulation world. Must be initialized using b2DefaultWorldDef.
Definition types.h:124