Chipmunk2D Pro API Reference  6.2.1
 All Classes Functions Variables Typedefs Properties Groups Pages
cpSpace.h
1 /* Copyright (c) 2007 Scott Lembcke
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 
24 
25 typedef struct cpContactBufferHeader cpContactBufferHeader;
26 typedef void (*cpSpaceArbiterApplyImpulseFunc)(cpArbiter *arb);
27 
29 struct cpSpace {
32 
35 
41 
45 
50 
56 
61 
65 
69 
74 
79 
80  CP_PRIVATE(cpTimestamp stamp);
81  CP_PRIVATE(cpFloat curr_dt);
82 
83  CP_PRIVATE(cpArray *bodies);
84  CP_PRIVATE(cpArray *rousedBodies);
85  CP_PRIVATE(cpArray *sleepingComponents);
86 
87  CP_PRIVATE(cpSpatialIndex *staticShapes);
88  CP_PRIVATE(cpSpatialIndex *activeShapes);
89 
90  CP_PRIVATE(cpArray *arbiters);
91  CP_PRIVATE(cpContactBufferHeader *contactBuffersHead);
92  CP_PRIVATE(cpHashSet *cachedArbiters);
93  CP_PRIVATE(cpArray *pooledArbiters);
94  CP_PRIVATE(cpArray *constraints);
95 
96  CP_PRIVATE(cpArray *allocatedBuffers);
97  CP_PRIVATE(int locked);
98 
99  CP_PRIVATE(cpHashSet *collisionHandlers);
100  CP_PRIVATE(cpCollisionHandler defaultHandler);
101 
102  CP_PRIVATE(cpBool skipPostStep);
103  CP_PRIVATE(cpArray *postStepCallbacks);
104 
105  CP_PRIVATE(cpBody _staticBody);
106 };
107 
109 cpSpace* cpSpaceAlloc(void);
111 cpSpace* cpSpaceInit(cpSpace *space);
113 cpSpace* cpSpaceNew(void);
114 
116 void cpSpaceDestroy(cpSpace *space);
118 void cpSpaceFree(cpSpace *space);
119 
120 #define CP_DefineSpaceStructGetter(type, member, name) \
121 static inline type cpSpaceGet##name(const cpSpace *space){return space->member;}
122 
123 #define CP_DefineSpaceStructSetter(type, member, name) \
124 static inline void cpSpaceSet##name(cpSpace *space, type value){space->member = value;}
125 
126 #define CP_DefineSpaceStructProperty(type, member, name) \
127 CP_DefineSpaceStructGetter(type, member, name) \
128 CP_DefineSpaceStructSetter(type, member, name)
129 
130 CP_DefineSpaceStructProperty(int, iterations, Iterations)
131 CP_DefineSpaceStructProperty(cpVect, gravity, Gravity)
132 CP_DefineSpaceStructProperty(cpFloat, damping, Damping)
133 CP_DefineSpaceStructProperty(cpFloat, idleSpeedThreshold, IdleSpeedThreshold)
134 CP_DefineSpaceStructProperty(cpFloat, sleepTimeThreshold, SleepTimeThreshold)
135 CP_DefineSpaceStructProperty(cpFloat, collisionSlop, CollisionSlop)
136 CP_DefineSpaceStructProperty(cpFloat, collisionBias, CollisionBias)
137 CP_DefineSpaceStructProperty(cpTimestamp, collisionPersistence, CollisionPersistence)
138 CP_DefineSpaceStructProperty(cpBool, enableContactGraph, EnableContactGraph)
139 CP_DefineSpaceStructProperty(cpDataPointer, data, UserData)
140 CP_DefineSpaceStructGetter(cpBody*, staticBody, StaticBody)
141 CP_DefineSpaceStructGetter(cpFloat, CP_PRIVATE(curr_dt), CurrentTimeStep)
142 
144 static inline cpBool
146 {
147  return space->CP_PRIVATE(locked);
148 }
149 
155  cpSpace *space,
156  cpCollisionBeginFunc begin,
157  cpCollisionPreSolveFunc preSolve,
158  cpCollisionPostSolveFunc postSolve,
159  cpCollisionSeparateFunc separate,
160  void *data
161 );
162 
166  cpSpace *space,
168  cpCollisionBeginFunc begin,
169  cpCollisionPreSolveFunc preSolve,
170  cpCollisionPostSolveFunc postSolve,
171  cpCollisionSeparateFunc separate,
172  void *data
173 );
174 
177 
180 cpShape* cpSpaceAddShape(cpSpace *space, cpShape *shape);
184 cpBody* cpSpaceAddBody(cpSpace *space, cpBody *body);
187 
189 void cpSpaceRemoveShape(cpSpace *space, cpShape *shape);
191 void cpSpaceRemoveStaticShape(cpSpace *space, cpShape *shape);
193 void cpSpaceRemoveBody(cpSpace *space, cpBody *body);
195 void cpSpaceRemoveConstraint(cpSpace *space, cpConstraint *constraint);
196 
203 
206 void cpSpaceConvertBodyToStatic(cpSpace *space, cpBody *body);
209 void cpSpaceConvertBodyToDynamic(cpSpace *space, cpBody *body, cpFloat mass, cpFloat moment);
210 
212 typedef void (*cpPostStepFunc)(cpSpace *space, void *key, void *data);
217 cpBool cpSpaceAddPostStepCallback(cpSpace *space, cpPostStepFunc func, void *key, void *data);
218 
220 typedef void (*cpSpacePointQueryFunc)(cpShape *shape, void *data);
222 void cpSpacePointQuery(cpSpace *space, cpVect point, cpLayers layers, cpGroup group, cpSpacePointQueryFunc func, void *data);
224 cpShape *cpSpacePointQueryFirst(cpSpace *space, cpVect point, cpLayers layers, cpGroup group);
225 
227 typedef void (*cpSpaceNearestPointQueryFunc)(cpShape *shape, cpFloat distance, cpVect point, void *data);
229 void cpSpaceNearestPointQuery(cpSpace *space, cpVect point, cpFloat maxDistance, cpLayers layers, cpGroup group, cpSpaceNearestPointQueryFunc func, void *data);
231 cpShape *cpSpaceNearestPointQueryNearest(cpSpace *space, cpVect point, cpFloat maxDistance, cpLayers layers, cpGroup group, cpNearestPointQueryInfo *out);
232 
234 typedef void (*cpSpaceSegmentQueryFunc)(cpShape *shape, cpFloat t, cpVect n, void *data);
236 void cpSpaceSegmentQuery(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSpaceSegmentQueryFunc func, void *data);
238 cpShape *cpSpaceSegmentQueryFirst(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSegmentQueryInfo *out);
239 
241 typedef void (*cpSpaceBBQueryFunc)(cpShape *shape, void *data);
244 void cpSpaceBBQuery(cpSpace *space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryFunc func, void *data);
245 
247 typedef void (*cpSpaceShapeQueryFunc)(cpShape *shape, cpContactPointSet *points, void *data);
249 cpBool cpSpaceShapeQuery(cpSpace *space, cpShape *shape, cpSpaceShapeQueryFunc func, void *data);
250 
253 
254 
256 typedef void (*cpSpaceBodyIteratorFunc)(cpBody *body, void *data);
258 void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIteratorFunc func, void *data);
259 
261 typedef void (*cpSpaceShapeIteratorFunc)(cpShape *shape, void *data);
263 void cpSpaceEachShape(cpSpace *space, cpSpaceShapeIteratorFunc func, void *data);
264 
266 typedef void (*cpSpaceConstraintIteratorFunc)(cpConstraint *constraint, void *data);
268 void cpSpaceEachConstraint(cpSpace *space, cpSpaceConstraintIteratorFunc func, void *data);
269 
271 void cpSpaceReindexStatic(cpSpace *space);
273 void cpSpaceReindexShape(cpSpace *space, cpShape *shape);
275 void cpSpaceReindexShapesForBody(cpSpace *space, cpBody *body);
276 
278 void cpSpaceUseSpatialHash(cpSpace *space, cpFloat dim, int count);
279 
281 void cpSpaceStep(cpSpace *space, cpFloat dt);
282