[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / chipmunk2d / include / chipmunk / 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         cpDataPointer userData2;
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
84 enum 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         // Either by returning false from a begin collision handler or calling cpArbiterIgnore().
91         CP_ARBITER_STATE_IGNORE,
92         // Collison is no longer active. A space will cache an arbiter for up to cpSpace.collisionPersistence more steps.
93         CP_ARBITER_STATE_CACHED,
94         // Collison arbiter is invalid because one of the shapes was removed.
95         CP_ARBITER_STATE_INVALIDATED,
96 };
97
98 struct cpArbiterThread {
99         struct cpArbiter *next, *prev;
100 };
101
102 struct cpContact {
103         cpVect r1, r2;
104         
105         cpFloat nMass, tMass;
106         cpFloat bounce; // TODO: look for an alternate bounce solution.
107
108         cpFloat jnAcc, jtAcc, jBias;
109         cpFloat bias;
110         
111         cpHashValue hash;
112 };
113
114 struct cpCollisionInfo {
115         const cpShape *a, *b;
116         cpCollisionID id;
117         
118         cpVect n;
119         
120         int count;
121         // TODO Should this be a unique struct type?
122         struct cpContact *arr;
123 };
124
125 struct cpArbiter {
126         cpFloat e;
127         cpFloat u;
128         cpVect surface_vr;
129         
130         cpDataPointer data;
131         
132         const cpShape *a, *b;
133         cpBody *body_a, *body_b;
134         struct cpArbiterThread thread_a, thread_b;
135         
136         int count;
137         struct cpContact *contacts;
138         cpVect n;
139         
140         // Regular, wildcard A and wildcard B collision handlers.
141         cpCollisionHandler *handler, *handlerA, *handlerB;
142         cpBool swapped;
143         
144         cpTimestamp stamp;
145         enum cpArbiterState state;
146 };
147
148 struct cpShapeMassInfo {
149         cpFloat m;
150         cpFloat i;
151         cpVect cog;
152         cpFloat area;
153 };
154
155 typedef enum cpShapeType{
156         CP_CIRCLE_SHAPE,
157         CP_SEGMENT_SHAPE,
158         CP_POLY_SHAPE,
159         CP_NUM_SHAPES
160 } cpShapeType;
161
162 typedef cpBB (*cpShapeCacheDataImpl)(cpShape *shape, cpTransform transform);
163 typedef void (*cpShapeDestroyImpl)(cpShape *shape);
164 typedef void (*cpShapePointQueryImpl)(const cpShape *shape, cpVect p, cpPointQueryInfo *info);
165 typedef void (*cpShapeSegmentQueryImpl)(const cpShape *shape, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info);
166
167 typedef struct cpShapeClass cpShapeClass;
168
169 struct cpShapeClass {
170         cpShapeType type;
171         
172         cpShapeCacheDataImpl cacheData;
173         cpShapeDestroyImpl destroy;
174         cpShapePointQueryImpl pointQuery;
175         cpShapeSegmentQueryImpl segmentQuery;
176 };
177
178 struct 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
203 struct cpCircleShape {
204         cpShape shape;
205         
206         cpVect c, tc;
207         cpFloat r;
208 };
209
210 struct cpSegmentShape {
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
220 struct cpSplittingPlane {
221         cpVect v0, n;
222 };
223
224 #define CP_POLY_SHAPE_INLINE_ALLOC 6
225
226 struct cpPolyShape {
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
239 typedef void (*cpConstraintPreStepImpl)(cpConstraint *constraint, cpFloat dt);
240 typedef void (*cpConstraintApplyCachedImpulseImpl)(cpConstraint *constraint, cpFloat dt_coef);
241 typedef void (*cpConstraintApplyImpulseImpl)(cpConstraint *constraint, cpFloat dt);
242 typedef cpFloat (*cpConstraintGetImpulseImpl)(cpConstraint *constraint);
243
244 typedef struct cpConstraintClass {
245         cpConstraintPreStepImpl preStep;
246         cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
247         cpConstraintApplyImpulseImpl applyImpulse;
248         cpConstraintGetImpulseImpl getImpulse;
249 } cpConstraintClass;
250
251 struct cpConstraint {
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         
265         cpConstraintPreSolveFunc preSolve;
266         cpConstraintPostSolveFunc postSolve;
267         
268         cpDataPointer userData;
269 };
270
271 struct cpPinJoint {
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
284 struct cpSlideJoint {
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
297 struct cpPivotJoint {
298         cpConstraint constraint;
299         cpVect anchorA, anchorB;
300         
301         cpVect r1, r2;
302         cpMat2x2 k;
303         
304         cpVect jAcc;
305         cpVect bias;
306 };
307
308 struct cpGrooveJoint {
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
322 struct cpDampedSpring {
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
340 struct cpDampedRotarySpring {
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
354 struct cpRotaryLimitJoint {
355         cpConstraint constraint;
356         cpFloat min, max;
357         
358         cpFloat iSum;
359                 
360         cpFloat bias;
361         cpFloat jAcc;
362 };
363
364 struct cpRatchetJoint {
365         cpConstraint constraint;
366         cpFloat angle, phase, ratchet;
367         
368         cpFloat iSum;
369                 
370         cpFloat bias;
371         cpFloat jAcc;
372 };
373
374 struct cpGearJoint {
375         cpConstraint constraint;
376         cpFloat phase, ratio;
377         cpFloat ratio_inv;
378         
379         cpFloat iSum;
380                 
381         cpFloat bias;
382         cpFloat jAcc;
383 };
384
385 struct cpSimpleMotor {
386         cpConstraint constraint;
387         cpFloat rate;
388         
389         cpFloat iSum;
390                 
391         cpFloat jAcc;
392 };
393
394 typedef struct cpContactBufferHeader cpContactBufferHeader;
395 typedef void (*cpSpaceArbiterApplyImpulseFunc)(cpArbiter *arb);
396
397 struct 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         unsigned int locked;
433         
434         cpBool usesWildcards;
435         cpHashSet *collisionHandlers;
436         cpCollisionHandler defaultHandler;
437         
438         cpBool skipPostStep;
439         cpArray *postStepCallbacks;
440         
441         cpBody *staticBody;
442         cpBody _staticBody;
443 };
444
445 typedef struct cpPostStepCallback {
446         cpPostStepFunc func;
447         void *key;
448         void *data;
449 } cpPostStepCallback;
450
451 #endif