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;
65 // "pseudo-velocities" used for eliminating overlap.
66 // Erin Catto has some papers that talk about what these are.
73 cpArbiter *arbiterList;
74 cpConstraint *constraintList;
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,
97 struct cpArbiterThread {
98 struct cpArbiter *next, *prev;
104 cpFloat nMass, tMass;
105 cpFloat bounce; // TODO: look for an alternate bounce solution.
107 cpFloat jnAcc, jtAcc, jBias;
113 struct cpCollisionInfo {
114 const cpShape *a, *b;
120 // TODO Should this be a unique struct type?
121 struct cpContact *arr;
131 const cpShape *a, *b;
132 cpBody *body_a, *body_b;
133 struct cpArbiterThread thread_a, thread_b;
136 struct cpContact *contacts;
139 // Regular, wildcard A and wildcard B collision handlers.
140 cpCollisionHandler *handler, *handlerA, *handlerB;
144 enum cpArbiterState state;
147 struct cpShapeMassInfo {
154 typedef enum cpShapeType{
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);
166 typedef struct cpShapeClass cpShapeClass;
168 struct cpShapeClass {
171 cpShapeCacheDataImpl cacheData;
172 cpShapeDestroyImpl destroy;
173 cpShapePointQueryImpl pointQuery;
174 cpShapeSegmentQueryImpl segmentQuery;
178 const cpShapeClass *klass;
182 struct cpShapeMassInfo massInfo;
191 cpDataPointer userData;
193 cpCollisionType type;
194 cpShapeFilter filter;
202 struct cpCircleShape {
209 struct cpSegmentShape {
216 cpVect a_tangent, b_tangent;
219 struct cpSplittingPlane {
223 #define CP_POLY_SHAPE_INLINE_ALLOC 6
231 // The untransformed planes are appended at the end of the transformed planes.
232 struct cpSplittingPlane *planes;
234 // Allocate a small number of splitting planes internally for simple poly.
235 struct cpSplittingPlane _planes[2*CP_POLY_SHAPE_INLINE_ALLOC];
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);
243 typedef struct cpConstraintClass {
244 cpConstraintPreStepImpl preStep;
245 cpConstraintApplyCachedImpulseImpl applyCachedImpulse;
246 cpConstraintApplyImpulseImpl applyImpulse;
247 cpConstraintGetImpulseImpl getImpulse;
250 struct cpConstraint {
251 const cpConstraintClass *klass;
256 cpConstraint *next_a, *next_b;
262 cpBool collideBodies;
264 cpConstraintPreSolveFunc preSolve;
265 cpConstraintPostSolveFunc postSolve;
267 cpDataPointer userData;
271 cpConstraint constraint;
272 cpVect anchorA, anchorB;
283 struct cpSlideJoint {
284 cpConstraint constraint;
285 cpVect anchorA, anchorB;
296 struct cpPivotJoint {
297 cpConstraint constraint;
298 cpVect anchorA, anchorB;
307 struct cpGrooveJoint {
308 cpConstraint constraint;
309 cpVect grv_n, grv_a, grv_b;
321 struct cpDampedSpring {
322 cpConstraint constraint;
323 cpVect anchorA, anchorB;
327 cpDampedSpringForceFunc springForceFunc;
339 struct cpDampedRotarySpring {
340 cpConstraint constraint;
344 cpDampedRotarySpringTorqueFunc springTorqueFunc;
353 struct cpRotaryLimitJoint {
354 cpConstraint constraint;
363 struct cpRatchetJoint {
364 cpConstraint constraint;
365 cpFloat angle, phase, ratchet;
374 cpConstraint constraint;
375 cpFloat phase, ratio;
384 struct cpSimpleMotor {
385 cpConstraint constraint;
393 typedef struct cpContactBufferHeader cpContactBufferHeader;
394 typedef void (*cpSpaceArbiterApplyImpulseFunc)(cpArbiter *arb);
402 cpFloat idleSpeedThreshold;
403 cpFloat sleepTimeThreshold;
405 cpFloat collisionSlop;
406 cpFloat collisionBias;
407 cpTimestamp collisionPersistence;
409 cpDataPointer userData;
414 cpArray *dynamicBodies;
415 cpArray *staticBodies;
416 cpArray *rousedBodies;
417 cpArray *sleepingComponents;
419 cpHashValue shapeIDCounter;
420 cpSpatialIndex *staticShapes;
421 cpSpatialIndex *dynamicShapes;
423 cpArray *constraints;
426 cpContactBufferHeader *contactBuffersHead;
427 cpHashSet *cachedArbiters;
428 cpArray *pooledArbiters;
430 cpArray *allocatedBuffers;
433 cpBool usesWildcards;
434 cpHashSet *collisionHandlers;
435 cpCollisionHandler defaultHandler;
438 cpArray *postStepCallbacks;
444 typedef struct cpPostStepCallback {
448 } cpPostStepCallback;