Munk2D Documentation  8.x.x
Loading...
Searching...
No Matches
chipmunk_structs.h
1/* Copyright (c) 2025 Victor Blomqvist
2 * Copyright (c) 2007-2024 Scott Lembcke and Howling Moon Software
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21*/
22
23// All of the struct definitions for Chipmunk should be considered part of the private API.
24// However, it is very valuable to know the struct sizes for preallocating memory.
25
26#ifndef CHIPMUNK_STRUCTS_H
27#define CHIPMUNK_STRUCTS_H
28
29#include "chipmunk/chipmunk.h"
30
31struct cpArray {
32 int num, max;
33 void **arr;
34};
35
36struct cpBody {
37 // Integration functions
38 cpBodyVelocityFunc velocity_func;
39 cpBodyPositionFunc position_func;
40
41 // mass and it's inverse
42 cpFloat m;
43 cpFloat m_inv;
44
45 // moment of inertia and it's inverse
46 cpFloat i;
47 cpFloat i_inv;
48
49 // center of gravity
50 cpVect cog;
51
52 // position, velocity, force
53 cpVect p;
54 cpVect v;
55 cpVect f;
56
57 // Angle, angular velocity, torque (radians)
58 cpFloat a;
59 cpFloat w;
60 cpFloat t;
61
62 cpTransform transform;
63
64 cpDataPointer userData;
65
66 // "pseudo-velocities" used for eliminating overlap.
67 // Erin Catto has some papers that talk about what these are.
68 cpVect v_bias;
69 cpFloat w_bias;
70
71 cpSpace *space;
72
73 cpShape *shapeList;
74 cpArbiter *arbiterList;
75 cpConstraint *constraintList;
76
77 struct {
78 cpBody *root;
79 cpBody *next;
80 cpFloat idleTime;
81 } sleeping;
82};
83
84enum cpArbiterState {
85 // Arbiter is active and its the first collision.
86 CP_ARBITER_STATE_FIRST_COLLISION,
87 // Arbiter is active and its not the first collision.
88 CP_ARBITER_STATE_NORMAL,
89 // Collision has been explicitly ignored.
90 CP_ARBITER_STATE_IGNORE,
91 // Collison is no longer active. A space will cache an arbiter for up to cpSpace.collisionPersistence more steps.
92 CP_ARBITER_STATE_CACHED,
93 // Collison arbiter is invalid because one of the shapes was removed.
94 CP_ARBITER_STATE_INVALIDATED,
95};
96
98 struct cpArbiter *next, *prev;
99};
100
101struct cpContact {
102 cpVect r1, r2;
103
104 cpFloat nMass, tMass;
105 cpFloat bounce; // TODO: look for an alternate bounce solution.
106
107 cpFloat jnAcc, jtAcc, jBias;
108 cpFloat bias;
109
110 cpHashValue hash;
111};
112
114 const cpShape *a, *b;
115 cpCollisionID id;
116
117 cpVect n;
118
119 int count;
120 // TODO Should this be a unique struct type?
121 struct cpContact *arr;
122};
123
124struct cpArbiter {
125 cpFloat e;
126 cpFloat u;
127 cpVect surface_vr;
128
129 cpDataPointer data;
130
131 const cpShape *a, *b;
132 cpBody *body_a, *body_b;
133 struct cpArbiterThread thread_a, thread_b;
134
135 int count;
136 struct cpContact *contacts;
137 cpVect n;
138
139 // CollisionHandlers.
140 cpCollisionHandler *handlerAB, *handlerBA, *handlerA, *handlerB;
141 cpBool swapped;
142
143 cpTimestamp stamp;
144 enum cpArbiterState state;
145 cpBool processCollision;
146};
147
149 cpFloat m;
150 cpFloat i;
151 cpVect cog;
152 cpFloat area;
153};
154
155typedef enum cpShapeType{
156 CP_CIRCLE_SHAPE,
157 CP_SEGMENT_SHAPE,
158 CP_POLY_SHAPE,
159 CP_NUM_SHAPES
160} cpShapeType;
161
162typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpTransform transform);
163typedef void (*cpShapeDestroyImpl)(cpShape *shape);
164typedef void (*cpShapePointQueryImpl)(const cpShape *shape, cpVect p, cpPointQueryInfo *info);
165typedef void (*cpShapeSegmentQueryImpl)(const cpShape *shape, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info);
166
167typedef struct cpShapeClass cpShapeClass;
168
170 cpShapeType type;
171
172 cpShapeCacheDataImpl cacheData;
173 cpShapeDestroyImpl destroy;
174 cpShapePointQueryImpl pointQuery;
175 cpShapeSegmentQueryImpl segmentQuery;
176};
177
178struct cpShape {
179 const cpShapeClass *klass;
180
181 cpSpace *space;
182 cpBody *body;
183 struct cpShapeMassInfo massInfo;
184 cpBB bb;
185
186 cpBool sensor;
187
188 cpFloat e;
189 cpFloat u;
190 cpVect surfaceV;
191
192 cpDataPointer userData;
193
194 cpCollisionType type;
195 cpShapeFilter filter;
196
197 cpShape *next;
198 cpShape *prev;
199
200 cpHashValue hashid;
201};
202
204 cpShape shape;
205
206 cpVect c, tc;
207 cpFloat r;
208};
209
211 cpShape shape;
212
213 cpVect a, b, n;
214 cpVect ta, tb, tn;
215 cpFloat r;
216
217 cpVect a_tangent, b_tangent;
218};
219
221 cpVect v0, n;
222};
223
224#define CP_POLY_SHAPE_INLINE_ALLOC 6
225
227 cpShape shape;
228
229 cpFloat r;
230
231 int count;
232 // The untransformed planes are appended at the end of the transformed planes.
233 struct cpSplittingPlane *planes;
234
235 // Allocate a small number of splitting planes internally for simple poly.
236 struct cpSplittingPlane _planes[2*CP_POLY_SHAPE_INLINE_ALLOC];
237};
238
239typedef void (*cpConstraintPreStepImpl)(cpConstraint *constraint, cpFloat dt);
240typedef void (*cpConstraintApplyCachedImpulseImpl)(cpConstraint *constraint, cpFloat dt_coef);
241typedef void (*cpConstraintApplyImpulseImpl)(cpConstraint *constraint, cpFloat dt);
242typedef cpFloat (*cpConstraintGetImpulseImpl)(cpConstraint *constraint);
243
244typedef struct cpConstraintClass {
245 cpConstraintPreStepImpl preStep;
246 cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
247 cpConstraintApplyImpulseImpl applyImpulse;
248 cpConstraintGetImpulseImpl getImpulse;
250
252 const cpConstraintClass *klass;
253
254 cpSpace *space;
255
256 cpBody *a, *b;
257 cpConstraint *next_a, *next_b;
258
259 cpFloat maxForce;
260 cpFloat errorBias;
261 cpFloat maxBias;
262
263 cpBool collideBodies;
264
267
268 cpDataPointer userData;
269};
270
272 cpConstraint constraint;
273 cpVect anchorA, anchorB;
274 cpFloat dist;
275
276 cpVect r1, r2;
277 cpVect n;
278 cpFloat nMass;
279
280 cpFloat jnAcc;
281 cpFloat bias;
282};
283
285 cpConstraint constraint;
286 cpVect anchorA, anchorB;
287 cpFloat min, max;
288
289 cpVect r1, r2;
290 cpVect n;
291 cpFloat nMass;
292
293 cpFloat jnAcc;
294 cpFloat bias;
295};
296
298 cpConstraint constraint;
299 cpVect anchorA, anchorB;
300
301 cpVect r1, r2;
302 cpMat2x2 k;
303
304 cpVect jAcc;
305 cpVect bias;
306};
307
309 cpConstraint constraint;
310 cpVect grv_n, grv_a, grv_b;
311 cpVect anchorB;
312
313 cpVect grv_tn;
314 cpFloat clamp;
315 cpVect r1, r2;
316 cpMat2x2 k;
317
318 cpVect jAcc;
319 cpVect bias;
320};
321
323 cpConstraint constraint;
324 cpVect anchorA, anchorB;
325 cpFloat restLength;
326 cpFloat stiffness;
327 cpFloat damping;
328 cpDampedSpringForceFunc springForceFunc;
329
330 cpFloat target_vrn;
331 cpFloat v_coef;
332
333 cpVect r1, r2;
334 cpFloat nMass;
335 cpVect n;
336
337 cpFloat jAcc;
338};
339
341 cpConstraint constraint;
342 cpFloat restAngle;
343 cpFloat stiffness;
344 cpFloat damping;
345 cpDampedRotarySpringTorqueFunc springTorqueFunc;
346
347 cpFloat target_wrn;
348 cpFloat w_coef;
349
350 cpFloat iSum;
351 cpFloat jAcc;
352};
353
355 cpConstraint constraint;
356 cpFloat min, max;
357
358 cpFloat iSum;
359
360 cpFloat bias;
361 cpFloat jAcc;
362};
363
365 cpConstraint constraint;
366 cpFloat angle, phase, ratchet;
367
368 cpFloat iSum;
369
370 cpFloat bias;
371 cpFloat jAcc;
372};
373
375 cpConstraint constraint;
376 cpFloat phase, ratio;
377 cpFloat ratio_inv;
378
379 cpFloat iSum;
380
381 cpFloat bias;
382 cpFloat jAcc;
383};
384
386 cpConstraint constraint;
387 cpFloat rate;
388
389 cpFloat iSum;
390
391 cpFloat jAcc;
392};
393
394typedef struct cpContactBufferHeader cpContactBufferHeader;
395typedef void (*cpSpaceArbiterApplyImpulseFunc)(cpArbiter *arb);
396
397struct cpSpace {
398 int iterations;
399
400 cpVect gravity;
401 cpFloat damping;
402
403 cpFloat idleSpeedThreshold;
404 cpFloat sleepTimeThreshold;
405
406 cpFloat collisionSlop;
407 cpFloat collisionBias;
408 cpTimestamp collisionPersistence;
409
410 cpDataPointer userData;
411
412 cpTimestamp stamp;
413 cpFloat curr_dt;
414
415 cpArray *dynamicBodies;
416 cpArray *staticBodies;
417 cpArray *rousedBodies;
418 cpArray *sleepingComponents;
419
420 cpHashValue shapeIDCounter;
421 cpSpatialIndex *staticShapes;
422 cpSpatialIndex *dynamicShapes;
423
424 cpArray *constraints;
425
426 cpArray *arbiters;
427 cpContactBufferHeader *contactBuffersHead;
428 cpHashSet *cachedArbiters;
429 cpArray *pooledArbiters;
430
431 cpArray *allocatedBuffers;
432 int locked;
433
434 cpHashSet *collisionHandlers;
435 cpCollisionHandler globalHandler;
436
437 cpBool skipPostStep;
438 cpArray *postStepCallbacks;
439
440 cpBody *staticBody;
441 cpBody _staticBody;
442};
443
444typedef struct cpPostStepCallback {
445 cpPostStepFunc func;
446 void *key;
447 void *data;
449
450#endif
void * cpDataPointer
Type used for user data pointers.
Definition chipmunk_types.h:196
uint32_t cpCollisionID
Type used internally to cache colliding object info for cpCollideShapes().
Definition chipmunk_types.h:172
unsigned int cpTimestamp
Type used for various timestamps in Chipmunk.
Definition chipmunk_types.h:224
unsigned char cpBool
Chipmunk's boolean type.
Definition chipmunk_types.h:179
double cpFloat
Chipmunk's floating point type.
Definition chipmunk_types.h:68
uintptr_t cpHashValue
Hash value type.
Definition chipmunk_types.h:167
uintptr_t cpCollisionType
Type used for cpSpace.collision_type.
Definition chipmunk_types.h:203
void(* cpBodyPositionFunc)(cpBody *body, cpFloat dt)
Rigid body position update function type.
Definition cpBody.h:47
void(* cpBodyVelocityFunc)(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
Rigid body velocity update function type.
Definition cpBody.h:45
void(* cpConstraintPostSolveFunc)(cpConstraint *constraint, cpSpace *space)
Callback function type that gets called after solving a joint.
Definition cpConstraint.h:29
void(* cpConstraintPreSolveFunc)(cpConstraint *constraint, cpSpace *space)
Callback function type that gets called before solving a joint.
Definition cpConstraint.h:27
cpFloat(* cpDampedRotarySpringTorqueFunc)(struct cpConstraint *spring, cpFloat relativeAngle)
Function type used for damped rotary spring force callbacks.
Definition cpDampedRotarySpring.h:30
cpFloat(* cpDampedSpringForceFunc)(cpConstraint *spring, cpFloat dist)
Function type used for damped spring force callbacks.
Definition cpDampedSpring.h:30
void(* cpPostStepFunc)(cpSpace *space, void *key, void *data)
Post Step callback function type.
Definition cpSpace.h:178
Definition chipmunk_structs.h:124
Definition chipmunk_structs.h:97
Definition chipmunk_structs.h:31
Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)
Definition cpBB.h:34
Definition chipmunk_structs.h:36
Definition chipmunk_structs.h:203
Struct that holds function callback pointers to configure custom collision handling.
Definition cpSpace.h:42
Definition chipmunk_structs.h:113
Definition chipmunk_structs.h:244
Definition chipmunk_structs.h:251
Definition chipmunk_structs.h:101
Definition chipmunk_structs.h:340
Definition chipmunk_structs.h:322
Definition chipmunk_structs.h:374
Definition chipmunk_structs.h:308
Definition chipmunk_types.h:264
Definition chipmunk_structs.h:271
Definition chipmunk_structs.h:297
Point query info struct.
Definition cpShape.h:28
Definition chipmunk_structs.h:226
Definition chipmunk_structs.h:444
Definition chipmunk_structs.h:364
Definition chipmunk_structs.h:354
Segment query info struct.
Definition cpShape.h:41
Definition chipmunk_structs.h:210
Definition chipmunk_structs.h:169
Fast collision filtering type that is used to determine if two objects collide before calling collisi...
Definition cpShape.h:53
Definition chipmunk_structs.h:178
Definition chipmunk_structs.h:148
Definition chipmunk_structs.h:385
Definition chipmunk_structs.h:284
Definition chipmunk_structs.h:397
Definition chipmunk_structs.h:220
Column major affine transform.
Definition chipmunk_types.h:258
Definition chipmunk_types.h:251