Chipmunk2D Pro API Reference  7.0.3
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
chipmunk_structs.h
1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  * SOFTWARE.
20  */
21 
22 // All of the struct definitions for Chipmunk should be considered part of the private API.
23 // However, it is very valuable to know the struct sizes for preallocating memory.
24 
25 #ifndef CHIPMUNK_STRUCTS_H
26 #define CHIPMUNK_STRUCTS_H
27 
28 #include "chipmunk/chipmunk.h"
29 
30 struct cpArray {
31  int num, max;
32  void **arr;
33 };
34 
35 struct cpBody {
36  // Integration functions
37  cpBodyVelocityFunc velocity_func;
38  cpBodyPositionFunc position_func;
39 
40  // mass and it's inverse
41  cpFloat m;
42  cpFloat m_inv;
43 
44  // moment of inertia and it's inverse
45  cpFloat i;
46  cpFloat i_inv;
47 
48  // center of gravity
49  cpVect cog;
50 
51  // position, velocity, force
52  cpVect p;
53  cpVect v;
54  cpVect f;
55 
56  // Angle, angular velocity, torque (radians)
57  cpFloat a;
58  cpFloat w;
59  cpFloat t;
60 
61  cpTransform transform;
62 
63  cpDataPointer userData;
64 
65  // "pseudo-velocities" used for eliminating overlap.
66  // Erin Catto has some papers that talk about what these are.
67  cpVect v_bias;
68  cpFloat w_bias;
69 
70  cpSpace *space;
71 
72  cpShape *shapeList;
73  cpArbiter *arbiterList;
74  cpConstraint *constraintList;
75 
76  struct {
77  cpBody *root;
78  cpBody *next;
79  cpFloat idleTime;
80  } sleeping;
81 };
82 
83 enum cpArbiterState {
84  // Arbiter is active and its the first collision.
85  CP_ARBITER_STATE_FIRST_COLLISION,
86  // Arbiter is active and its not the first collision.
87  CP_ARBITER_STATE_NORMAL,
88  // Collision has been explicitly ignored.
89  // Either by returning false from a begin collision handler or calling cpArbiterIgnore().
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 
101 struct 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 
124 struct 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  // Regular, wildcard A and wildcard B collision handlers.
140  cpCollisionHandler *handler, *handlerA, *handlerB;
141  cpBool swapped;
142 
143  cpTimestamp stamp;
144  enum cpArbiterState state;
145 };
146 
148  cpFloat m;
149  cpFloat i;
150  cpVect cog;
151  cpFloat area;
152 };
153 
154 typedef enum cpShapeType{
155  CP_CIRCLE_SHAPE,
156  CP_SEGMENT_SHAPE,
157  CP_POLY_SHAPE,
158  CP_NUM_SHAPES
159 } cpShapeType;
160 
161 typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpTransform transform);
162 typedef void (*cpShapeDestroyImpl)(cpShape *shape);
163 typedef void (*cpShapePointQueryImpl)(const cpShape *shape, cpVect p, cpPointQueryInfo *info);
164 typedef void (*cpShapeSegmentQueryImpl)(const cpShape *shape, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info);
165 
166 typedef struct cpShapeClass cpShapeClass;
167 
168 struct cpShapeClass {
169  cpShapeType type;
170 
171  cpShapeCacheDataImpl cacheData;
172  cpShapeDestroyImpl destroy;
173  cpShapePointQueryImpl pointQuery;
174  cpShapeSegmentQueryImpl segmentQuery;
175 };
176 
177 struct cpShape {
178  const cpShapeClass *klass;
179 
180  cpSpace *space;
181  cpBody *body;
182  struct cpShapeMassInfo massInfo;
183  cpBB bb;
184 
185  cpBool sensor;
186 
187  cpFloat e;
188  cpFloat u;
189  cpVect surfaceV;
190 
191  cpDataPointer userData;
192 
193  cpCollisionType type;
194  cpShapeFilter filter;
195 
196  cpShape *next;
197  cpShape *prev;
198 
199  cpHashValue hashid;
200 };
201 
203  cpShape shape;
204 
205  cpVect c, tc;
206  cpFloat r;
207 };
208 
210  cpShape shape;
211 
212  cpVect a, b, n;
213  cpVect ta, tb, tn;
214  cpFloat r;
215 
216  cpVect a_tangent, b_tangent;
217 };
218 
220  cpVect v0, n;
221 };
222 
223 #define CP_POLY_SHAPE_INLINE_ALLOC 6
224 
225 struct cpPolyShape {
226  cpShape shape;
227 
228  cpFloat r;
229 
230  int count;
231  // The untransformed planes are appended at the end of the transformed planes.
232  struct cpSplittingPlane *planes;
233 
234  // Allocate a small number of splitting planes internally for simple poly.
235  struct cpSplittingPlane _planes[2*CP_POLY_SHAPE_INLINE_ALLOC];
236 };
237 
238 typedef void (*cpConstraintPreStepImpl)(cpConstraint *constraint, cpFloat dt);
239 typedef void (*cpConstraintApplyCachedImpulseImpl)(cpConstraint *constraint, cpFloat dt_coef);
240 typedef void (*cpConstraintApplyImpulseImpl)(cpConstraint *constraint, cpFloat dt);
241 typedef cpFloat (*cpConstraintGetImpulseImpl)(cpConstraint *constraint);
242 
243 typedef struct cpConstraintClass {
244  cpConstraintPreStepImpl preStep;
245  cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
246  cpConstraintApplyImpulseImpl applyImpulse;
247  cpConstraintGetImpulseImpl getImpulse;
249 
250 struct cpConstraint {
251  const cpConstraintClass *klass;
252 
253  cpSpace *space;
254 
255  cpBody *a, *b;
256  cpConstraint *next_a, *next_b;
257 
258  cpFloat maxForce;
259  cpFloat errorBias;
260  cpFloat maxBias;
261 
262  cpBool collideBodies;
263 
264  cpConstraintPreSolveFunc preSolve;
265  cpConstraintPostSolveFunc postSolve;
266 
267  cpDataPointer userData;
268 };
269 
270 struct cpPinJoint {
271  cpConstraint constraint;
272  cpVect anchorA, anchorB;
273  cpFloat dist;
274 
275  cpVect r1, r2;
276  cpVect n;
277  cpFloat nMass;
278 
279  cpFloat jnAcc;
280  cpFloat bias;
281 };
282 
283 struct cpSlideJoint {
284  cpConstraint constraint;
285  cpVect anchorA, anchorB;
286  cpFloat min, max;
287 
288  cpVect r1, r2;
289  cpVect n;
290  cpFloat nMass;
291 
292  cpFloat jnAcc;
293  cpFloat bias;
294 };
295 
296 struct cpPivotJoint {
297  cpConstraint constraint;
298  cpVect anchorA, anchorB;
299 
300  cpVect r1, r2;
301  cpMat2x2 k;
302 
303  cpVect jAcc;
304  cpVect bias;
305 };
306 
308  cpConstraint constraint;
309  cpVect grv_n, grv_a, grv_b;
310  cpVect anchorB;
311 
312  cpVect grv_tn;
313  cpFloat clamp;
314  cpVect r1, r2;
315  cpMat2x2 k;
316 
317  cpVect jAcc;
318  cpVect bias;
319 };
320 
322  cpConstraint constraint;
323  cpVect anchorA, anchorB;
324  cpFloat restLength;
325  cpFloat stiffness;
326  cpFloat damping;
327  cpDampedSpringForceFunc springForceFunc;
328 
329  cpFloat target_vrn;
330  cpFloat v_coef;
331 
332  cpVect r1, r2;
333  cpFloat nMass;
334  cpVect n;
335 
336  cpFloat jAcc;
337 };
338 
340  cpConstraint constraint;
341  cpFloat restAngle;
342  cpFloat stiffness;
343  cpFloat damping;
344  cpDampedRotarySpringTorqueFunc springTorqueFunc;
345 
346  cpFloat target_wrn;
347  cpFloat w_coef;
348 
349  cpFloat iSum;
350  cpFloat jAcc;
351 };
352 
354  cpConstraint constraint;
355  cpFloat min, max;
356 
357  cpFloat iSum;
358 
359  cpFloat bias;
360  cpFloat jAcc;
361 };
362 
364  cpConstraint constraint;
365  cpFloat angle, phase, ratchet;
366 
367  cpFloat iSum;
368 
369  cpFloat bias;
370  cpFloat jAcc;
371 };
372 
373 struct cpGearJoint {
374  cpConstraint constraint;
375  cpFloat phase, ratio;
376  cpFloat ratio_inv;
377 
378  cpFloat iSum;
379 
380  cpFloat bias;
381  cpFloat jAcc;
382 };
383 
385  cpConstraint constraint;
386  cpFloat rate;
387 
388  cpFloat iSum;
389 
390  cpFloat jAcc;
391 };
392 
393 typedef struct cpContactBufferHeader cpContactBufferHeader;
394 typedef void (*cpSpaceArbiterApplyImpulseFunc)(cpArbiter *arb);
395 
396 struct cpSpace {
397  int iterations;
398 
399  cpVect gravity;
400  cpFloat damping;
401 
402  cpFloat idleSpeedThreshold;
403  cpFloat sleepTimeThreshold;
404 
405  cpFloat collisionSlop;
406  cpFloat collisionBias;
407  cpTimestamp collisionPersistence;
408 
409  cpDataPointer userData;
410 
411  cpTimestamp stamp;
412  cpFloat curr_dt;
413 
414  cpArray *dynamicBodies;
415  cpArray *staticBodies;
416  cpArray *rousedBodies;
417  cpArray *sleepingComponents;
418 
419  cpHashValue shapeIDCounter;
420  cpSpatialIndex *staticShapes;
421  cpSpatialIndex *dynamicShapes;
422 
423  cpArray *constraints;
424 
425  cpArray *arbiters;
426  cpContactBufferHeader *contactBuffersHead;
427  cpHashSet *cachedArbiters;
428  cpArray *pooledArbiters;
429 
430  cpArray *allocatedBuffers;
431  unsigned int locked;
432 
433  cpBool usesWildcards;
434  cpHashSet *collisionHandlers;
435  cpCollisionHandler defaultHandler;
436 
437  cpBool skipPostStep;
438  cpArray *postStepCallbacks;
439 
440  cpBody *staticBody;
441  cpBody _staticBody;
442 };
443 
444 typedef struct cpPostStepCallback {
445  cpPostStepFunc func;
446  void *key;
447  void *data;
449 
450 #endif