1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
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.
25 #ifndef CHIPMUNK_STRUCTS_H
26 #define CHIPMUNK_STRUCTS_H
28 #include "chipmunk/chipmunk.h"
36 // Integration functions
37 cpBodyVelocityFunc velocity_func;
38 cpBodyPositionFunc position_func;
40 // mass and it's inverse
44 // moment of inertia and it's inverse
51 // position, velocity, force
56 // Angle, angular velocity, torque (radians)
61 cpTransform transform;
63 cpDataPointer userData;
64 cpDataPointer userData2;
66 // "pseudo-velocities" used for eliminating overlap.
67 // Erin Catto has some papers that talk about what these are.
74 cpArbiter *arbiterList;
75 cpConstraint *constraintList;
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,
98 struct cpArbiterThread {
99 struct cpArbiter *next, *prev;
105 cpFloat nMass, tMass;
106 cpFloat bounce; // TODO: look for an alternate bounce solution.
108 cpFloat jnAcc, jtAcc, jBias;
114 struct cpCollisionInfo {
115 const cpShape *a, *b;
121 // TODO Should this be a unique struct type?
122 struct cpContact *arr;
132 const cpShape *a, *b;
133 cpBody *body_a, *body_b;
134 struct cpArbiterThread thread_a, thread_b;
137 struct cpContact *contacts;
140 // Regular, wildcard A and wildcard B collision handlers.
141 cpCollisionHandler *handler, *handlerA, *handlerB;
145 enum cpArbiterState state;
148 struct cpShapeMassInfo {
155 typedef enum cpShapeType{
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);
167 typedef struct cpShapeClass cpShapeClass;
169 struct cpShapeClass {
172 cpShapeCacheDataImpl cacheData;
173 cpShapeDestroyImpl destroy;
174 cpShapePointQueryImpl pointQuery;
175 cpShapeSegmentQueryImpl segmentQuery;
179 const cpShapeClass *klass;
183 struct cpShapeMassInfo massInfo;
192 cpDataPointer userData;
194 cpCollisionType type;
195 cpShapeFilter filter;
203 struct cpCircleShape {
210 struct cpSegmentShape {
217 cpVect a_tangent, b_tangent;
220 struct cpSplittingPlane {
224 #define CP_POLY_SHAPE_INLINE_ALLOC 6
232 // The untransformed planes are appended at the end of the transformed planes.
233 struct cpSplittingPlane *planes;
235 // Allocate a small number of splitting planes internally for simple poly.
236 struct cpSplittingPlane _planes[2*CP_POLY_SHAPE_INLINE_ALLOC];
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);
244 typedef struct cpConstraintClass {
245 cpConstraintPreStepImpl preStep;
246 cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
247 cpConstraintApplyImpulseImpl applyImpulse;
248 cpConstraintGetImpulseImpl getImpulse;
251 struct cpConstraint {
252 const cpConstraintClass *klass;
257 cpConstraint *next_a, *next_b;
263 cpBool collideBodies;
265 cpConstraintPreSolveFunc preSolve;
266 cpConstraintPostSolveFunc postSolve;
268 cpDataPointer userData;
272 cpConstraint constraint;
273 cpVect anchorA, anchorB;
284 struct cpSlideJoint {
285 cpConstraint constraint;
286 cpVect anchorA, anchorB;
297 struct cpPivotJoint {
298 cpConstraint constraint;
299 cpVect anchorA, anchorB;
308 struct cpGrooveJoint {
309 cpConstraint constraint;
310 cpVect grv_n, grv_a, grv_b;
322 struct cpDampedSpring {
323 cpConstraint constraint;
324 cpVect anchorA, anchorB;
328 cpDampedSpringForceFunc springForceFunc;
340 struct cpDampedRotarySpring {
341 cpConstraint constraint;
345 cpDampedRotarySpringTorqueFunc springTorqueFunc;
354 struct cpRotaryLimitJoint {
355 cpConstraint constraint;
364 struct cpRatchetJoint {
365 cpConstraint constraint;
366 cpFloat angle, phase, ratchet;
375 cpConstraint constraint;
376 cpFloat phase, ratio;
385 struct cpSimpleMotor {
386 cpConstraint constraint;
394 typedef struct cpContactBufferHeader cpContactBufferHeader;
395 typedef void (*cpSpaceArbiterApplyImpulseFunc)(cpArbiter *arb);
403 cpFloat idleSpeedThreshold;
404 cpFloat sleepTimeThreshold;
406 cpFloat collisionSlop;
407 cpFloat collisionBias;
408 cpTimestamp collisionPersistence;
410 cpDataPointer userData;
415 cpArray *dynamicBodies;
416 cpArray *staticBodies;
417 cpArray *rousedBodies;
418 cpArray *sleepingComponents;
420 cpHashValue shapeIDCounter;
421 cpSpatialIndex *staticShapes;
422 cpSpatialIndex *dynamicShapes;
424 cpArray *constraints;
427 cpContactBufferHeader *contactBuffersHead;
428 cpHashSet *cachedArbiters;
429 cpArray *pooledArbiters;
431 cpArray *allocatedBuffers;
434 cpBool usesWildcards;
435 cpHashSet *collisionHandlers;
436 cpCollisionHandler defaultHandler;
439 cpArray *postStepCallbacks;
445 typedef struct cpPostStepCallback {
449 } cpPostStepCallback;