Munk2D Documentation  8.x.x
Loading...
Searching...
No Matches
cpSpace.h
1/* Copyright (c) 2025 Victor Blomqvist
2 * Copyright (c) 2007-2024 Scott Lembcke and Howling Moon Software
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21*/
22
25
26//MARK: Definitions
27
30typedef void (*cpCollisionBeginFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData);
33typedef void (*cpCollisionPreSolveFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData);
35typedef void (*cpCollisionPostSolveFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData);
37typedef void (*cpCollisionSeparateFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData);
38
62
63// TODO: Make timestep a parameter?
64
65//MARK: Memory and Initialization
66
68CP_EXPORT cpSpace *cpSpaceAlloc(void);
70CP_EXPORT cpSpace *cpSpaceInit(cpSpace *space);
72CP_EXPORT cpSpace *cpSpaceNew(void);
73
75CP_EXPORT void cpSpaceDestroy(cpSpace *space);
77CP_EXPORT void cpSpaceFree(cpSpace *space);
78
79//MARK: Properties
80
82CP_EXPORT int cpSpaceGetIterations(const cpSpace *space);
83CP_EXPORT void cpSpaceSetIterations(cpSpace *space, int iterations);
84
86CP_EXPORT cpVect cpSpaceGetGravity(const cpSpace *space);
87CP_EXPORT void cpSpaceSetGravity(cpSpace *space, cpVect gravity);
88
93CP_EXPORT cpFloat cpSpaceGetDamping(const cpSpace *space);
94CP_EXPORT void cpSpaceSetDamping(cpSpace *space, cpFloat damping);
95
99CP_EXPORT void cpSpaceSetIdleSpeedThreshold(cpSpace *space, cpFloat idleSpeedThreshold);
100
105CP_EXPORT void cpSpaceSetSleepTimeThreshold(cpSpace *space, cpFloat sleepTimeThreshold);
106
112CP_EXPORT void cpSpaceSetCollisionSlop(cpSpace *space, cpFloat collisionSlop);
113
118CP_EXPORT void cpSpaceSetCollisionBias(cpSpace *space, cpFloat collisionBias);
119
123CP_EXPORT void cpSpaceSetCollisionPersistence(cpSpace *space, cpTimestamp collisionPersistence);
124
129CP_EXPORT void cpSpaceSetUserData(cpSpace *space, cpDataPointer userData);
130
133CP_EXPORT cpBody *cpSpaceGetStaticBody(const cpSpace *space);
134
138
141
142//MARK: Collision Handlers
143
150
151//MARK: Add/Remove objects
152
155CP_EXPORT cpShape *cpSpaceAddShape(cpSpace *space, cpShape *shape);
157CP_EXPORT cpBody *cpSpaceAddBody(cpSpace *space, cpBody *body);
160
162CP_EXPORT void cpSpaceRemoveShape(cpSpace *space, cpShape *shape);
164CP_EXPORT void cpSpaceRemoveBody(cpSpace *space, cpBody *body);
166CP_EXPORT void cpSpaceRemoveConstraint(cpSpace *space, cpConstraint *constraint);
167
169CP_EXPORT cpBool cpSpaceContainsShape(cpSpace *space, cpShape *shape);
171CP_EXPORT cpBool cpSpaceContainsBody(cpSpace *space, cpBody *body);
174
175//MARK: Post-Step Callbacks
176
178typedef void (*cpPostStepFunc)(cpSpace *space, void *key, void *data);
183CP_EXPORT cpBool cpSpaceAddPostStepCallback(cpSpace *space, cpPostStepFunc func, void *key, void *data);
184
185//MARK: Queries
186
187// TODO: Queries and iterators should take a cpSpace parametery.
188// TODO: They should also be abortable.
189
191typedef void (*cpSpacePointQueryFunc)(cpShape *shape, cpVect point, cpFloat distance, cpVect gradient, void *data);
193CP_EXPORT void cpSpacePointQuery(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpSpacePointQueryFunc func, void *data);
195CP_EXPORT cpShape *cpSpacePointQueryNearest(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpPointQueryInfo *out);
196
198typedef void (*cpSpaceSegmentQueryFunc)(cpShape *shape, cpVect point, cpVect normal, cpFloat alpha, void *data);
200CP_EXPORT void cpSpaceSegmentQuery(cpSpace *space, cpVect start, cpVect end, cpFloat radius, cpShapeFilter filter, cpSpaceSegmentQueryFunc func, void *data);
203
205typedef void (*cpSpaceBBQueryFunc)(cpShape *shape, void *data);
208CP_EXPORT void cpSpaceBBQuery(cpSpace *space, cpBB bb, cpShapeFilter filter, cpSpaceBBQueryFunc func, void *data);
209
211typedef void (*cpSpaceShapeQueryFunc)(cpShape *shape, cpContactPointSet *points, void *data);
213CP_EXPORT cpBool cpSpaceShapeQuery(cpSpace *space, cpShape *shape, cpSpaceShapeQueryFunc func, void *data);
214
215//MARK: Iteration
216
218typedef void (*cpSpaceBodyIteratorFunc)(cpBody *body, void *data);
220CP_EXPORT void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIteratorFunc func, void *data);
221
223typedef void (*cpSpaceShapeIteratorFunc)(cpShape *shape, void *data);
225CP_EXPORT void cpSpaceEachShape(cpSpace *space, cpSpaceShapeIteratorFunc func, void *data);
226
228typedef void (*cpSpaceConstraintIteratorFunc)(cpConstraint *constraint, void *data);
230CP_EXPORT void cpSpaceEachConstraint(cpSpace *space, cpSpaceConstraintIteratorFunc func, void *data);
231
232//MARK: Indexing
233
235CP_EXPORT void cpSpaceReindexStatic(cpSpace *space);
237CP_EXPORT void cpSpaceReindexShape(cpSpace *space, cpShape *shape);
239CP_EXPORT void cpSpaceReindexShapesForBody(cpSpace *space, cpBody *body);
240
242CP_EXPORT void cpSpaceUseSpatialHash(cpSpace *space, cpFloat dim, int count);
243
244//MARK: Time Stepping
245
247CP_EXPORT void cpSpaceStep(cpSpace *space, cpFloat dt);
248
249//MARK: Debug API
250
251#ifndef CP_SPACE_DISABLE_DEBUG_API
252
254typedef struct cpSpaceDebugColor
255{
256 float r, g, b, a;
258
260typedef void (*cpSpaceDebugDrawCircleImpl)(cpVect pos, cpFloat angle, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data);
264typedef void (*cpSpaceDebugDrawFatSegmentImpl)(cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data);
266typedef void (*cpSpaceDebugDrawPolygonImpl)(int count, const cpVect *verts, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data);
271
272typedef enum cpSpaceDebugDrawFlags
273{
274 CP_SPACE_DEBUG_DRAW_SHAPES = 1 << 0,
275 CP_SPACE_DEBUG_DRAW_CONSTRAINTS = 1 << 1,
276 CP_SPACE_DEBUG_DRAW_COLLISION_POINTS = 1 << 2,
277} cpSpaceDebugDrawFlags;
278
309
311CP_EXPORT void cpSpaceDebugDraw(cpSpace *space, cpSpaceDebugDrawOptions *options);
312
313#endif
314
void * cpDataPointer
Type used for user data pointers.
Definition chipmunk_types.h:196
unsigned int cpTimestamp
Type used for various timestamps in Chipmunk.
Definition chipmunk_types.h:224
unsigned char cpBool
Chipmunk's boolean type.
Definition chipmunk_types.h:179
double cpFloat
Chipmunk's floating point type.
Definition chipmunk_types.h:68
uintptr_t cpCollisionType
Type used for cpSpace.collision_type.
Definition chipmunk_types.h:203
void(* cpCollisionSeparateFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData)
Collision separate event function callback type.
Definition cpSpace.h:37
void(* cpSpaceBBQueryFunc)(cpShape *shape, void *data)
Rectangle Query callback function type.
Definition cpSpace.h:205
CP_EXPORT void cpSpaceDebugDraw(cpSpace *space, cpSpaceDebugDrawOptions *options)
Debug draw the current state of the space using the supplied drawing options.
CP_EXPORT void cpSpaceBBQuery(cpSpace *space, cpBB bb, cpShapeFilter filter, cpSpaceBBQueryFunc func, void *data)
Perform a fast rectangle query on the space calling func for each shape found.
void(* cpSpaceConstraintIteratorFunc)(cpConstraint *constraint, void *data)
Space/constraint iterator callback function type.
Definition cpSpace.h:228
CP_EXPORT void cpSpaceEachConstraint(cpSpace *space, cpSpaceConstraintIteratorFunc func, void *data)
Call func for each shape in the space.
CP_EXPORT void cpSpaceSegmentQuery(cpSpace *space, cpVect start, cpVect end, cpFloat radius, cpShapeFilter filter, cpSpaceSegmentQueryFunc func, void *data)
Perform a directed line segment query (like a raycast) against the space calling func for each shape ...
CP_EXPORT cpBody * cpSpaceAddBody(cpSpace *space, cpBody *body)
Add a rigid body to the simulation.
CP_EXPORT cpBool cpSpaceContainsShape(cpSpace *space, cpShape *shape)
Test if a collision shape has been added to the space.
CP_EXPORT cpDataPointer cpSpaceGetUserData(const cpSpace *space)
User definable data pointer.
void(* cpSpaceDebugDrawPolygonImpl)(int count, const cpVect *verts, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data)
Callback type for a function that draws a convex polygon.
Definition cpSpace.h:266
CP_EXPORT cpFloat cpSpaceGetIdleSpeedThreshold(const cpSpace *space)
Speed threshold for a body to be considered idle.
void(* cpSpaceBodyIteratorFunc)(cpBody *body, void *data)
Space/body iterator callback function type.
Definition cpSpace.h:218
CP_EXPORT cpConstraint * cpSpaceAddConstraint(cpSpace *space, cpConstraint *constraint)
Add a constraint to the simulation.
CP_EXPORT cpSpace * cpSpaceNew(void)
Allocate and initialize a cpSpace.
void(* cpCollisionBeginFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData)
Collision begin event function callback type.
Definition cpSpace.h:30
CP_EXPORT void cpSpaceRemoveBody(cpSpace *space, cpBody *body)
Remove a rigid body from the simulation.
CP_EXPORT cpShape * cpSpaceSegmentQueryFirst(cpSpace *space, cpVect start, cpVect end, cpFloat radius, cpShapeFilter filter, cpSegmentQueryInfo *out)
Perform a directed line segment query (like a raycast) against the space and return the first shape h...
CP_EXPORT cpSpace * cpSpaceInit(cpSpace *space)
Initialize a cpSpace.
CP_EXPORT cpCollisionHandler * cpSpaceAddGlobalCollisionHandler(cpSpace *space)
Create or return the existing collision handler that is called for all collisions.
CP_EXPORT cpFloat cpSpaceGetCollisionBias(const cpSpace *space)
Determines how fast overlapping shapes are pushed apart.
CP_EXPORT cpVect cpSpaceGetGravity(const cpSpace *space)
Gravity to pass to rigid bodies when integrating velocity.
CP_EXPORT void cpSpacePointQuery(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpSpacePointQueryFunc func, void *data)
Query the space at a point and call func for each shape found.
void(* cpSpacePointQueryFunc)(cpShape *shape, cpVect point, cpFloat distance, cpVect gradient, void *data)
Nearest point query callback function type.
Definition cpSpace.h:191
CP_EXPORT cpCollisionHandler * cpSpaceAddCollisionHandler(cpSpace *space, cpCollisionType a, cpCollisionType b)
Create or return the existing collision handler for the specified pair of collision types.
CP_EXPORT cpBool cpSpaceAddPostStepCallback(cpSpace *space, cpPostStepFunc func, void *key, void *data)
Schedule a post-step callback to be called when cpSpaceStep() finishes.
CP_EXPORT void cpSpaceReindexStatic(cpSpace *space)
Update the collision detection info for the static shapes in the space.
CP_EXPORT cpCollisionHandler * cpSpaceAddWildcardHandler(cpSpace *space, cpCollisionType type)
Create or return the existing wildcard collision handler for the specified type.
void(* cpCollisionPreSolveFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData)
Collision pre-solve event function callback type.
Definition cpSpace.h:33
CP_EXPORT cpFloat cpSpaceGetCollisionSlop(const cpSpace *space)
Amount of encouraged penetration between colliding shapes.
CP_EXPORT cpBool cpSpaceContainsBody(cpSpace *space, cpBody *body)
Test if a rigid body has been added to the space.
CP_EXPORT int cpSpaceGetIterations(const cpSpace *space)
Number of iterations to use in the impulse solver to solve contacts and other constraints.
CP_EXPORT void cpSpaceEachBody(cpSpace *space, cpSpaceBodyIteratorFunc func, void *data)
Call func for each body in the space.
CP_EXPORT cpBool cpSpaceShapeQuery(cpSpace *space, cpShape *shape, cpSpaceShapeQueryFunc func, void *data)
Query a space for any shapes overlapping the given shape and call func for each shape found.
CP_EXPORT cpShape * cpSpacePointQueryNearest(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpPointQueryInfo *out)
Query the space at a point and return the nearest shape found. Returns NULL if no shapes were found.
void(* cpSpaceSegmentQueryFunc)(cpShape *shape, cpVect point, cpVect normal, cpFloat alpha, void *data)
Segment query callback function type.
Definition cpSpace.h:198
void(* cpPostStepFunc)(cpSpace *space, void *key, void *data)
Post Step callback function type.
Definition cpSpace.h:178
CP_EXPORT void cpSpaceReindexShapesForBody(cpSpace *space, cpBody *body)
Update the collision detection data for all shapes attached to a body.
CP_EXPORT cpSpace * cpSpaceAlloc(void)
Allocate a cpSpace.
CP_EXPORT void cpSpaceDestroy(cpSpace *space)
Destroy a cpSpace.
CP_EXPORT cpBody * cpSpaceGetStaticBody(const cpSpace *space)
The Space provided static body for a given cpSpace.
void(* cpSpaceShapeQueryFunc)(cpShape *shape, cpContactPointSet *points, void *data)
Shape query callback function type.
Definition cpSpace.h:211
CP_EXPORT void cpSpaceFree(cpSpace *space)
Destroy and free a cpSpace.
CP_EXPORT void cpSpaceReindexShape(cpSpace *space, cpShape *shape)
Update the collision detection data for a specific shape in the space.
CP_EXPORT cpFloat cpSpaceGetSleepTimeThreshold(const cpSpace *space)
Time a group of bodies must remain idle in order to fall asleep.
CP_EXPORT void cpSpaceEachShape(cpSpace *space, cpSpaceShapeIteratorFunc func, void *data)
Call func for each shape in the space.
void(* cpSpaceDebugDrawFatSegmentImpl)(cpVect a, cpVect b, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data)
Callback type for a function that draws a thick line segment.
Definition cpSpace.h:264
void(* cpSpaceDebugDrawCircleImpl)(cpVect pos, cpFloat angle, cpFloat radius, cpSpaceDebugColor outlineColor, cpSpaceDebugColor fillColor, cpDataPointer data)
Callback type for a function that draws a filled, stroked circle.
Definition cpSpace.h:260
cpSpaceDebugColor(* cpSpaceDebugDrawColorForShapeImpl)(cpShape *shape, cpDataPointer data)
Callback type for a function that returns a color for a given shape. This gives you an opportunity to...
Definition cpSpace.h:270
CP_EXPORT cpFloat cpSpaceGetDamping(const cpSpace *space)
Damping rate expressed as the fraction of velocity bodies retain each second.
CP_EXPORT cpTimestamp cpSpaceGetCollisionPersistence(const cpSpace *space)
Number of frames that contact information should persist.
void(* cpCollisionPostSolveFunc)(cpArbiter *arb, cpSpace *space, cpDataPointer userData)
Collision post-solve event function callback type.
Definition cpSpace.h:35
CP_EXPORT cpFloat cpSpaceGetCurrentTimeStep(const cpSpace *space)
Returns the current (or most recent) time step used with the given space.
CP_EXPORT cpShape * cpSpaceAddShape(cpSpace *space, cpShape *shape)
Add a collision shape to the simulation.
CP_EXPORT cpBool cpSpaceIsLocked(cpSpace *space)
returns true from inside a callback when objects cannot be added/removed.
CP_EXPORT void cpSpaceRemoveShape(cpSpace *space, cpShape *shape)
Remove a collision shape from the simulation.
void(* cpSpaceDebugDrawDotImpl)(cpFloat size, cpVect pos, cpSpaceDebugColor color, cpDataPointer data)
Callback type for a function that draws a dot.
Definition cpSpace.h:268
CP_EXPORT void cpSpaceStep(cpSpace *space, cpFloat dt)
Step the space forward in time by dt.
void(* cpSpaceDebugDrawSegmentImpl)(cpVect a, cpVect b, cpSpaceDebugColor color, cpDataPointer data)
Callback type for a function that draws a line segment.
Definition cpSpace.h:262
CP_EXPORT void cpSpaceUseSpatialHash(cpSpace *space, cpFloat dim, int count)
Switch the space to use a spatial has as it's spatial index.
CP_EXPORT void cpSpaceRemoveConstraint(cpSpace *space, cpConstraint *constraint)
Remove a constraint from the simulation.
CP_EXPORT cpBool cpSpaceContainsConstraint(cpSpace *space, cpConstraint *constraint)
Test if a constraint has been added to the space.
void(* cpSpaceShapeIteratorFunc)(cpShape *shape, void *data)
Space/body iterator callback function type.
Definition cpSpace.h:223
Definition chipmunk_structs.h:124
Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)
Definition cpBB.h:34
Definition chipmunk_structs.h:36
Struct that holds function callback pointers to configure custom collision handling.
Definition cpSpace.h:42
const cpCollisionType typeA
Collision type identifier of the first shape that this handler recognizes.
Definition cpSpace.h:45
cpCollisionSeparateFunc separateFunc
This function is called when two shapes with types that match this collision handler stop colliding.
Definition cpSpace.h:58
cpCollisionBeginFunc beginFunc
This function is called when two shapes with types that match this collision handler begin colliding.
Definition cpSpace.h:50
const cpCollisionType typeB
Collision type identifier of the second shape that this handler recognizes.
Definition cpSpace.h:48
cpCollisionPostSolveFunc postSolveFunc
This function is called each step when two shapes with types that match this collision handler are co...
Definition cpSpace.h:56
cpDataPointer userData
This is a user definable context pointer that is passed to all of the collision handler functions.
Definition cpSpace.h:60
cpCollisionPreSolveFunc preSolveFunc
This function is called each step when two shapes with types that match this collision handler are co...
Definition cpSpace.h:53
Definition chipmunk_structs.h:251
A struct that wraps up the important collision data for an arbiter.
Definition cpArbiter.h:88
Point query info struct.
Definition cpShape.h:28
Segment query info struct.
Definition cpShape.h:41
Fast collision filtering type that is used to determine if two objects collide before calling collisi...
Definition cpShape.h:53
Definition chipmunk_structs.h:178
Color type to use with the space debug drawing API.
Definition cpSpace.h:255
Struct used with cpSpaceDebugDraw() containing drawing callbacks and other drawing settings.
Definition cpSpace.h:281
cpSpaceDebugDrawFlags flags
Flags that request which things to draw (collision shapes, constraints, contact points).
Definition cpSpace.h:294
cpSpaceDebugDrawDotImpl drawDot
Function that will be invoked to draw dots.
Definition cpSpace.h:291
cpSpaceDebugColor shapeOutlineColor
Outline color passed to the drawing function.
Definition cpSpace.h:296
cpTransform transform
Transform used to transform the things to draw.
Definition cpSpace.h:304
cpSpaceDebugColor constraintColor
Color passed to drawing functions for constraints.
Definition cpSpace.h:300
cpDataPointer data
User defined context pointer passed to all of the callback functions as the 'data' argument.
Definition cpSpace.h:307
cpSpaceDebugDrawColorForShapeImpl colorForShape
Function that decides what fill color to draw shapes using.
Definition cpSpace.h:298
cpSpaceDebugDrawPolygonImpl drawPolygon
Function that will be invoked to draw convex polygons.
Definition cpSpace.h:289
cpSpaceDebugDrawSegmentImpl drawSegment
Function that will be invoked to draw line segments.
Definition cpSpace.h:285
cpSpaceDebugColor collisionPointColor
Color passed to drawing functions for collision points.
Definition cpSpace.h:302
cpSpaceDebugDrawCircleImpl drawCircle
Function that will be invoked to draw circles.
Definition cpSpace.h:283
cpSpaceDebugDrawFatSegmentImpl drawFatSegment
Function that will be invoked to draw thick line segments.
Definition cpSpace.h:287
Definition chipmunk_structs.h:397
Column major affine transform.
Definition chipmunk_types.h:258
Definition chipmunk_types.h:251