boxddd-sys 0.1.0

Low-level FFI bindings for Box3D built from vendored upstream sources
Documentation
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT

#pragma once

#include "math_internal.h"
#include "solver.h"

#include "box3d/types.h"

typedef struct b3DebugDraw b3DebugDraw;
typedef struct b3StepContext b3StepContext;
typedef struct b3World b3World;

/// A joint edge is used to connect bodies and joints together
/// in a joint graph where each body is a node and each joint
/// is an edge. A joint edge belongs to a doubly linked list
/// maintained in each attached body. Each joint has two joint
/// nodes, one for each attached body.
typedef struct b3JointEdge
{
	int bodyId;
	int prevKey;
	int nextKey;
} b3JointEdge;

// Map from b3JointId to b3Joint in the solver sets
typedef struct b3Joint
{
	void* userData;

	// index of simulation set stored in b3World
	// B3_NULL_INDEX when slot is free
	int setIndex;

	// index into the constraint graph color array, may be B3_NULL_INDEX for sleeping/disabled joints
	// B3_NULL_INDEX when slot is free
	int colorIndex;

	// joint index within set or graph color
	// B3_NULL_INDEX when slot is free
	int localIndex;

	b3JointEdge edges[2];

	int jointId;
	int islandId;

	// Index into the island's joints array for O(1) swap-removal.
	// B3_NULL_INDEX when not in an island.
	int islandIndex;

	float drawScale;

	b3JointType type;

	// This is monotonically advanced when a body is allocated in this slot
	// Used to check for invalid b3JointId
	uint16_t generation;

	bool collideConnected;
} b3Joint;

typedef struct b3DistanceJoint
{
	float length;
	float hertz;
	float dampingRatio;
	float lowerSpringForce;
	float upperSpringForce;
	float minLength;
	float maxLength;

	float maxMotorForce;
	float motorSpeed;

	float impulse;
	float lowerImpulse;
	float upperImpulse;
	float motorImpulse;

	int indexA;
	int indexB;
	b3Vec3 anchorA;
	b3Vec3 anchorB;
	b3Vec3 deltaCenter;
	b3Softness distanceSoftness;
	float axialMass;

	bool enableSpring;
	bool enableLimit;
	bool enableMotor;
} b3DistanceJoint;

typedef struct b3MotorJoint
{
	b3Vec3 linearVelocity;
	b3Vec3 angularVelocity;
	float maxVelocityForce;
	float maxVelocityTorque;
	float linearHertz;
	float linearDampingRatio;
	float maxSpringForce;
	float angularHertz;
	float angularDampingRatio;
	float maxSpringTorque;

	b3Vec3 linearVelocityImpulse;
	b3Vec3 angularVelocityImpulse;
	b3Vec3 linearSpringImpulse;
	b3Vec3 angularSpringImpulse;

	b3Softness linearSpring;
	b3Softness angularSpring;

	int indexA;
	int indexB;
	b3Transform frameA;
	b3Transform frameB;
	b3Vec3 deltaCenter;
	b3Matrix3 angularMass;
} b3MotorJoint;

typedef struct b3ParallelJoint
{
	float hertz;
	float dampingRatio;
	float maxTorque;

	b3Vec2 perpImpulse;
	b3Vec3 perpAxisX;
	b3Vec3 perpAxisY;

	b3Quat quatA;
	b3Quat quatB;
	int indexA;
	int indexB;
	b3Softness softness;
} b3ParallelJoint;

typedef struct b3PrismaticJoint
{
	b3Vec2 perpImpulse;
	b3Vec3 angularImpulse;
	float springImpulse;
	float motorImpulse;
	float lowerImpulse;
	float upperImpulse;
	float hertz;
	float dampingRatio;
	float maxMotorForce;
	float motorSpeed;
	float targetTranslation;
	float lowerTranslation;
	float upperTranslation;

	int indexA;
	int indexB;
	b3Transform frameA;
	b3Transform frameB;
	b3Vec3 jointAxis;
	b3Vec3 perpAxisY;
	b3Vec3 perpAxisZ;
	b3Vec3 deltaCenter;
	float deltaAngle;
	b3Matrix3 rotationMass;
	b3Softness springSoftness;

	bool enableSpring;
	bool enableLimit;
	bool enableMotor;
} b3PrismaticJoint;

typedef struct b3RevoluteJoint
{
	b3Vec3 linearImpulse;
	b3Vec2 perpImpulse;
	float springImpulse;
	float motorImpulse;
	float lowerImpulse;
	float upperImpulse;
	float hertz;
	float dampingRatio;
	float maxMotorTorque;
	float motorSpeed;
	float targetAngle;
	float lowerAngle;
	float upperAngle;

	int indexA;
	int indexB;
	b3Transform frameA;
	b3Transform frameB;
	b3Vec3 rotationAxisZ;
	b3Vec3 perpAxisX;
	b3Vec3 perpAxisY;
	b3Vec3 deltaCenter;
	float deltaAngle;
	float axialMass;
	b3Softness springSoftness;

	bool enableSpring;
	bool enableMotor;
	bool enableLimit;
} b3RevoluteJoint;

typedef struct b3SphericalJoint
{
	b3Vec3 linearImpulse;
	b3Vec3 springImpulse;
	b3Vec3 motorImpulse;
	float lowerTwistImpulse;
	float upperTwistImpulse;
	float swingImpulse;
	float hertz;
	float dampingRatio;
	float maxMotorTorque;
	b3Vec3 motorVelocity;
	float lowerTwistAngle;
	float upperTwistAngle;
	float coneAngle;
	b3Quat targetRotation;

	int indexA;
	int indexB;
	b3Transform frameA;
	b3Transform frameB;
	b3Vec3 deltaCenter;
	b3Vec3 swingAxis;
	b3Vec3 twistJacobian;

	b3Matrix3 rotationMass;
	float swingMass;
	float twistMass;
	b3Softness springSoftness;

	bool enableSpring;
	bool enableMotor;
	bool enableConeLimit;
	bool enableTwistLimit;
} b3SphericalJoint;

typedef struct b3WeldJoint
{
	float linearHertz;
	float linearDampingRatio;
	float angularHertz;
	float angularDampingRatio;

	b3Softness linearSpring;
	b3Softness angularSpring;
	b3Vec3 linearImpulse;
	b3Vec3 angularImpulse;

	int indexA;
	int indexB;
	b3Transform frameA;
	b3Transform frameB;
	b3Vec3 deltaCenter;

	b3Matrix3 angularMass;
} b3WeldJoint;

typedef struct b3WheelJoint
{
	b3Vec2 linearImpulse;
	b3Vec2 angularImpulse;
	float spinImpulse;
	float maxSpinTorque;
	float spinSpeed;
	float suspensionSpringImpulse;
	float lowerSuspensionImpulse;
	float upperSuspensionImpulse;
	float lowerSuspensionLimit;
	float upperSuspensionLimit;
	float suspensionHertz;
	float suspensionDampingRatio;
	float steeringSpringImpulse;
	float lowerSteeringImpulse;
	float upperSteeringImpulse;
	float lowerSteeringLimit;
	float upperSteeringLimit;
	float targetSteeringAngle;
	float maxSteeringTorque;
	float steeringHertz;
	float steeringDampingRatio;

	int indexA;
	int indexB;
	b3Transform frameA;
	b3Transform frameB;
	b3Vec3 deltaCenter;
	float spinMass;
	float suspensionMass;
	float steeringMass;
	b3Softness suspensionSoftness;
	b3Softness steeringSoftness;

	bool enableSpinMotor;
	bool enableSuspensionSpring;
	bool enableSuspensionLimit;
	bool enableSteering;
	bool enableSteeringLimit;
	bool enableSteeringMotor;
} b3WheelJoint;

/// The base joint class. Joints are used to constraint two bodies together in
/// various fashions. Some joints also feature limits and motors.
typedef struct b3JointSim
{
	int jointId;

	int bodyIdA;
	int bodyIdB;

	b3JointType type;

	// Joint frames local to body origin
	b3Transform localFrameA;
	b3Transform localFrameB;

	float invMassA, invMassB;
	b3Matrix3 invIA, invIB;

	float constraintHertz;
	float constraintDampingRatio;

	b3Softness constraintSoftness;

	float forceThreshold;
	float torqueThreshold;

	bool fixedRotation;

	union
	{
		b3DistanceJoint distanceJoint;
		b3MotorJoint motorJoint;
		b3ParallelJoint parallelJoint;
		b3RevoluteJoint revoluteJoint;
		b3SphericalJoint sphericalJoint;
		b3PrismaticJoint prismaticJoint;
		b3WeldJoint weldJoint;
		b3WheelJoint wheelJoint;
	};
} b3JointSim;

void b3DestroyJointInternal( b3World* world, b3Joint* joint, bool wakeBodies );

b3Joint* b3GetJointFullId( b3World* world, b3JointId jointId );
b3JointSim* b3GetJointSim( b3World* world, b3Joint* joint );
b3JointSim* b3GetJointSimCheckType( b3JointId jointId, b3JointType type );

void b3PrepareJoint( b3JointSim* joint, b3StepContext* context );
void b3WarmStartJoint( b3JointSim* joint, b3StepContext* context );
void b3SolveJoint( b3JointSim* joint, b3StepContext* context, bool useBias );

void b3PrepareJoints_Overflow( b3StepContext* context );
void b3WarmStartJoints_Overflow( b3StepContext* context );
void b3SolveJoints_Overflow( b3StepContext* context, bool useBias );

void b3GetJointReaction( b3World* world, b3JointSim* sim, float invTimeStep, float* force, float* torque );

void b3DrawJoint( b3DebugDraw* draw, b3World* world, b3Joint* joint );

b3Vec3 b3GetDistanceJointForce( b3World* world, b3JointSim* base );
b3Vec3 b3GetMotorJointForce( b3World* world, b3JointSim* base );
b3Vec3 b3GetPrismaticJointForce( b3World* world, b3JointSim* base );
b3Vec3 b3GetRevoluteJointForce( b3World* world, b3JointSim* base );
b3Vec3 b3GetSphericalJointForce( b3World* world, b3JointSim* base );
b3Vec3 b3GetWeldJointForce( b3World* world, b3JointSim* base );
b3Vec3 b3GetWheelJointForce( b3World* world, b3JointSim* base );

b3Vec3 b3GetMotorJointTorque( b3World* world, b3JointSim* base );
b3Vec3 b3GetParallelJointTorque( b3World* world, b3JointSim* base );
b3Vec3 b3GetPrismaticJointTorque( b3World* world, b3JointSim* base );
b3Vec3 b3GetRevoluteJointTorque( b3World* world, b3JointSim* base );
b3Vec3 b3GetSphericalJointTorque( b3World* world, b3JointSim* base );
b3Vec3 b3GetWeldJointTorque( b3World* world, b3JointSim* base );
b3Vec3 b3GetWheelJointTorque( b3World* world, b3JointSim* base );

void b3PrepareDistanceJoint( b3JointSim* base, b3StepContext* context );
void b3PrepareMotorJoint( b3JointSim* base, b3StepContext* context );
void b3PrepareParallelJoint( b3JointSim* base, b3StepContext* context );
void b3PreparePrismaticJoint( b3JointSim* base, b3StepContext* context );
void b3PrepareRevoluteJoint( b3JointSim* base, b3StepContext* context );
void b3PrepareSphericalJoint( b3JointSim* base, b3StepContext* context );
void b3PrepareWeldJoint( b3JointSim* base, b3StepContext* context );
void b3PrepareWheelJoint( b3JointSim* base, b3StepContext* context );

void b3WarmStartDistanceJoint( b3JointSim* base, b3StepContext* context );
void b3WarmStartMotorJoint( b3JointSim* base, b3StepContext* context );
void b3WarmStartParallelJoint( b3JointSim* base, b3StepContext* context );
void b3WarmStartPrismaticJoint( b3JointSim* base, b3StepContext* context );
void b3WarmStartRevoluteJoint( b3JointSim* base, b3StepContext* context );
void b3WarmStartSphericalJoint( b3JointSim* base, b3StepContext* context );
void b3WarmStartWeldJoint( b3JointSim* base, b3StepContext* context );
void b3WarmStartWheelJoint( b3JointSim* base, b3StepContext* context );

void b3SolveDistanceJoint( b3JointSim* base, b3StepContext* context, bool useBias );
void b3SolveMotorJoint( b3JointSim* base, b3StepContext* context );
void b3SolveParallelJoint( b3JointSim* base, b3StepContext* context );
void b3SolvePrismaticJoint( b3JointSim* base, b3StepContext* context, bool useBias );
void b3SolveRevoluteJoint( b3JointSim* base, b3StepContext* context, bool useBias );
void b3SolveSphericalJoint( b3JointSim* base, b3StepContext* context, bool useBias );
void b3SolveWeldJoint( b3JointSim* base, b3StepContext* context, bool useBias );
void b3SolveWheelJoint( b3JointSim* base, b3StepContext* context, bool useBias );

void b3DrawDistanceJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB );
void b3DrawParallelJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
void b3DrawPrismaticJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
void b3DrawRevoluteJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
void b3DrawSphericalJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
void b3DrawWeldJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );
void b3DrawWheelJoint( b3DebugDraw* draw, b3JointSim* base, b3WorldTransform transformA, b3WorldTransform transformB, float scale );