1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT
#pragma once
#include "array.h"
#include "bitset.h"
#include "broad_phase.h"
#include "constraint_graph.h"
#include "id_pool.h"
#include "arena_allocator.h"
#include "box2d/types.h"
// Per thread task storage
typedef struct b2TaskContext
{
// Collect per thread sensor continuous hit events.
b2SensorHitArray sensorHits;
// These bits align with the contact id capacity and signal a change in contact status
b2BitSet contactStateBitSet;
// These bits align with the joint id capacity and signal a change in contact status
b2BitSet jointStateBitSet;
// Used to track bodies with shapes that have enlarged AABBs. This avoids having a bit array
// that is very large when there are many static shapes.
b2BitSet enlargedSimBitSet;
// Used to put islands to sleep
b2BitSet awakeIslandBitSet;
// Per worker split island candidate
float splitSleepTime;
int splitIslandId;
} b2TaskContext;
// The world struct manages all physics entities, dynamic simulation, and asynchronous queries.
// The world also contains efficient memory management facilities.
typedef struct b2World
{
b2ArenaAllocator arena;
b2BroadPhase broadPhase;
b2ConstraintGraph constraintGraph;
// The body id pool is used to allocate and recycle body ids. Body ids
// provide a stable identifier for users, but incur caches misses when used
// to access body data. Aligns with b2Body.
b2IdPool bodyIdPool;
// This is a sparse array that maps body ids to the body data
// stored in solver sets. As sims move within a set or across set.
// Indices come from id pool.
b2BodyArray bodies;
// Provides free list for solver sets.
b2IdPool solverSetIdPool;
// Solvers sets allow sims to be stored in contiguous arrays. The first
// set is all static sims. The second set is active sims. The third set is disabled
// sims. The remaining sets are sleeping islands.
b2SolverSetArray solverSets;
// Used to create stable ids for joints
b2IdPool jointIdPool;
// This is a sparse array that maps joint ids to the joint data stored in the constraint graph
// or in the solver sets.
b2JointArray joints;
// Used to create stable ids for contacts
b2IdPool contactIdPool;
// This is a sparse array that maps contact ids to the contact data stored in the constraint graph
// or in the solver sets.
b2ContactArray contacts;
// Used to create stable ids for islands
b2IdPool islandIdPool;
// This is a sparse array that maps island ids to the island data stored in the solver sets.
b2IslandArray islands;
b2IdPool shapeIdPool;
b2IdPool chainIdPool;
// These are sparse arrays that point into the pools above
b2ShapeArray shapes;
b2ChainShapeArray chainShapes;
// This is a dense array of sensor data.
b2SensorArray sensors;
// Per thread storage
b2TaskContextArray taskContexts;
b2SensorTaskContextArray sensorTaskContexts;
b2BodyMoveEventArray bodyMoveEvents;
b2SensorBeginTouchEventArray sensorBeginEvents;
b2ContactBeginTouchEventArray contactBeginEvents;
// End events are double buffered so that the user doesn't need to flush events
b2SensorEndTouchEventArray sensorEndEvents[2];
b2ContactEndTouchEventArray contactEndEvents[2];
int endEventArrayIndex;
b2ContactHitEventArray contactHitEvents;
b2JointEventArray jointEvents;
// todo consider deferred waking and impulses to make it possible
// to apply forces and impulses from multiple threads
// impulses must be deferred because sleeping bodies have no velocity state
// Problems:
// - multiple forces applied to the same body from multiple threads
// Deferred wake
//b2BitSet bodyWakeSet;
//b2ImpulseArray deferredImpulses;
// Used to track debug draw
b2BitSet debugBodySet;
b2BitSet debugJointSet;
b2BitSet debugContactSet;
b2BitSet debugIslandSet;
// Id that is incremented every time step
uint64_t stepIndex;
// Identify islands for splitting as follows:
// - I want to split islands so smaller islands can sleep
// - when a body comes to rest and its sleep timer trips, I can look at the island and flag it for splitting
// if it has removed constraints
// - islands that have removed constraints must be put split first because I don't want to wake bodies incorrectly
// - otherwise I can use the awake islands that have bodies wanting to sleep as the splitting candidates
// - if no bodies want to sleep then there is no reason to perform island splitting
int splitIslandId;
b2Vec2 gravity;
float hitEventThreshold;
float restitutionThreshold;
float maxLinearSpeed;
float contactSpeed;
float contactHertz;
float contactDampingRatio;
b2FrictionCallback* frictionCallback;
b2RestitutionCallback* restitutionCallback;
uint16_t generation;
b2Profile profile;
b2PreSolveFcn* preSolveFcn;
void* preSolveContext;
b2CustomFilterFcn* customFilterFcn;
void* customFilterContext;
int workerCount;
b2EnqueueTaskCallback* enqueueTaskFcn;
b2FinishTaskCallback* finishTaskFcn;
void* userTaskContext;
void* userTreeTask;
void* userData;
// Remember type step used for reporting forces and torques
// inverse sub-step
float inv_h;
// inverse full-step
float inv_dt;
int activeTaskCount;
int taskCount;
uint16_t worldId;
bool enableSleep;
bool locked;
bool enableWarmStarting;
bool enableContactSoftening;
bool enableContinuous;
bool enableSpeculative;
bool inUse;
} b2World;
b2World* b2GetWorldFromId( b2WorldId id );
b2World* b2GetWorld( int index );
b2World* b2GetWorldLocked( int index );
void b2ValidateConnectivity( b2World* world );
void b2ValidateSolverSets( b2World* world );
void b2ValidateContacts( b2World* world );
B2_ARRAY_INLINE( b2BodyMoveEvent, b2BodyMoveEvent )
B2_ARRAY_INLINE( b2ContactBeginTouchEvent, b2ContactBeginTouchEvent )
B2_ARRAY_INLINE( b2ContactEndTouchEvent, b2ContactEndTouchEvent )
B2_ARRAY_INLINE( b2ContactHitEvent, b2ContactHitEvent )
B2_ARRAY_INLINE( b2JointEvent, b2JointEvent )
B2_ARRAY_INLINE( b2SensorBeginTouchEvent, b2SensorBeginTouchEvent )
B2_ARRAY_INLINE( b2SensorEndTouchEvent, b2SensorEndTouchEvent )
B2_ARRAY_INLINE( b2TaskContext, b2TaskContext )