Chipmunk2D Pro API Reference  7.0.0
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
cpSpace.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 
24 
25 //MARK: Definitions
26 
30 typedef cpBool (*cpCollisionBeginFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData);
33 typedef cpBool (*cpCollisionPreSolveFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData);
35 typedef void (*cpCollisionPostSolveFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData);
37 typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData);
38 
60 };
61 
62 // TODO: Make timestep a parameter?
63 
64 
65 //MARK: Memory and Initialization
66 
68 cpSpace* cpSpaceAlloc(void);
70 cpSpace* cpSpaceInit(cpSpace *space);
72 cpSpace* cpSpaceNew(void);
73 
75 void cpSpaceDestroy(cpSpace *space);
77 void cpSpaceFree(cpSpace *space);
78 
79 
80 //MARK: Properties
81 
83 int cpSpaceGetIterations(const cpSpace *space);
84 void cpSpaceSetIterations(cpSpace *space, int iterations);
85 
87 cpVect cpSpaceGetGravity(const cpSpace *space);
88 void cpSpaceSetGravity(cpSpace *space, cpVect gravity);
89 
94 cpFloat cpSpaceGetDamping(const cpSpace *space);
95 void cpSpaceSetDamping(cpSpace *space, cpFloat damping);
96 
100 void cpSpaceSetIdleSpeedThreshold(cpSpace *space, cpFloat idleSpeedThreshold);
101 
106 void cpSpaceSetSleepTimeThreshold(cpSpace *space, cpFloat sleepTimeThreshold);
107 
113 void cpSpaceSetCollisionSlop(cpSpace *space, cpFloat collisionSlop);
114 
119 void cpSpaceSetCollisionBias(cpSpace *space, cpFloat collisionBias);
120 
124 void cpSpaceSetCollisionPersistence(cpSpace *space, cpTimestamp collisionPersistence);
125 
130 void cpSpaceSetUserData(cpSpace *space, cpDataPointer userData);
131 
134 cpBody* cpSpaceGetStaticBody(const cpSpace *space);
135 
139 
142 
143 
144 //MARK: Collision Handlers
145 
153 
154 
155 //MARK: Add/Remove objects
156 
159 cpShape* cpSpaceAddShape(cpSpace *space, cpShape *shape);
161 cpBody* cpSpaceAddBody(cpSpace *space, cpBody *body);
164 
166 void cpSpaceRemoveShape(cpSpace *space, cpShape *shape);
168 void cpSpaceRemoveBody(cpSpace *space, cpBody *body);
170 void cpSpaceRemoveConstraint(cpSpace *space, cpConstraint *constraint);
171 
178 
179 //MARK: Post-Step Callbacks
180 
182 typedef void (*cpPostStepFunc)(cpSpace *space, void *key, void *data);
187 cpBool cpSpaceAddPostStepCallback(cpSpace *space, cpPostStepFunc func, void *key, void *data);
188 
189 
190 //MARK: Queries
191 
192 // TODO: Queries and iterators should take a cpSpace parametery.
193 // TODO: They should also be abortable.
194 
196 typedef void (*cpSpacePointQueryFunc)(cpShape *shape, cpVect point, cpFloat distance, cpVect gradient, void *data);
198 void cpSpacePointQuery(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpSpacePointQueryFunc func, void *data);
200 cpShape *cpSpacePointQueryNearest(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpPointQueryInfo *out);
201 
203 typedef void (*cpSpaceSegmentQueryFunc)(cpShape *shape, cpVect point, cpVect normal, cpFloat alpha, void *data);
205 void cpSpaceSegmentQuery(cpSpace *space, cpVect start, cpVect end, cpFloat radius, cpShapeFilter filter, cpSpaceSegmentQueryFunc func, void *data);
208 
210 typedef void (*cpSpaceBBQueryFunc)(cpShape *shape, void *data);
213 void cpSpaceBBQuery(cpSpace *space, cpBB bb, cpShapeFilter filter, cpSpaceBBQueryFunc func, void *data);
214 
216 typedef void (*cpSpaceShapeQueryFunc)(cpShape *shape, cpContactPointSet *points, void *data);
218 cpBool cpSpaceShapeQuery(cpSpace *space, cpShape *shape, cpSpaceShapeQueryFunc func, void *data);
219 
220 
221 //MARK: Iteration
222 
224 typedef void (*cpSpaceBodyIteratorFunc)(cpBody *body, void *data);
226 void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIteratorFunc func, void *data);
227 
229 typedef void (*cpSpaceShapeIteratorFunc)(cpShape *shape, void *data);
231 void cpSpaceEachShape(cpSpace *space, cpSpaceShapeIteratorFunc func, void *data);
232 
234 typedef void (*cpSpaceConstraintIteratorFunc)(cpConstraint *constraint, void *data);
236 void cpSpaceEachConstraint(cpSpace *space, cpSpaceConstraintIteratorFunc func, void *data);
237 
238 
239 //MARK: Indexing
240 
242 void cpSpaceReindexStatic(cpSpace *space);
244 void cpSpaceReindexShape(cpSpace *space, cpShape *shape);
246 void cpSpaceReindexShapesForBody(cpSpace *space, cpBody *body);
247 
249 void cpSpaceUseSpatialHash(cpSpace *space, cpFloat dim, int count);
250 
251 
252 //MARK: Time Stepping
253 
255 void cpSpaceStep(cpSpace *space, cpFloat dt);
256 
257 
258 //MARK: Debug API
259 
260 #ifndef CP_SPACE_DISABLE_DEBUG_API
261 
263 typedef struct cpSpaceDebugColor {
264  float r, g, b, a;
266 
268 typedef void (*cpSpaceDebugDrawCircleImpl)(cpVect pos, cpFloat angle, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data);
272 typedef void (*cpSpaceDebugDrawFatSegmentImpl)(cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data);
274 typedef void (*cpSpaceDebugDrawPolygonImpl)(int count, const cpVect *verts, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data);
276 typedef void (*cpSpaceDebugDrawDotImpl)(cpFloat size, cpVect pos, cpSpaceDebugColor color, cpDataPointer data);
279 
280 typedef enum cpSpaceDebugDrawFlags {
281  CP_SPACE_DEBUG_DRAW_SHAPES = 1<<0,
282  CP_SPACE_DEBUG_DRAW_CONSTRAINTS = 1<<1,
283  CP_SPACE_DEBUG_DRAW_COLLISION_POINTS = 1<<2,
284 } cpSpaceDebugDrawFlags;
285 
287 typedef struct cpSpaceDebugDrawOptions {
298 
300  cpSpaceDebugDrawFlags flags;
309 
313 
315 void cpSpaceDebugDraw(cpSpace *space, cpSpaceDebugDrawOptions *options);
316 
317 #endif
318