Munk2D Documentation  8.x.x
Loading...
Searching...
No Matches
chipmunk.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
23#ifndef CHIPMUNK_H
24#define CHIPMUNK_H
25
26#include <stdlib.h>
27#include <math.h>
28
29#ifndef alloca
30 #ifdef _WIN32
31 #include <malloc.h>
32 #elif defined(__FreeBSD__)
33 /* already included in <stdlib.h> */
34 #else
35 #include <alloca.h>
36 #endif
37#endif
38
39#ifdef _WIN32
40 #ifdef __MINGW32__
41 #define CP_EXPORT
42 #else
43 #define CP_EXPORT __declspec(dllexport)
44 #endif
45#else
46 #define CP_EXPORT
47#endif
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53CP_EXPORT void cpMessage(const char *condition, const char *file, int line, int isError, int isHardError, const char *message, ...);
54//#define DBGMSG(...) cpMessage("xxx", __FILE__, __LINE__, 0, 0, __VA_ARGS__)
55#ifdef NDEBUG
56 #define cpAssertWarn(__condition__, ...)
57 #define cpAssertSoft(__condition__, ...)
58#else
59 #define cpAssertSoft(__condition__, ...) if(!(__condition__)){cpMessage(#__condition__, __FILE__, __LINE__, 1, 0, __VA_ARGS__); abort();}
60 #define cpAssertWarn(__condition__, ...) if(!(__condition__)) cpMessage(#__condition__, __FILE__, __LINE__, 0, 0, __VA_ARGS__)
61#endif
62
63// Hard assertions are used in situations where the program definitely will crash anyway, and the reason is inexpensive to detect.
64#define cpAssertHard(__condition__, ...) if(!(__condition__)){cpMessage(#__condition__, __FILE__, __LINE__, 1, 1, __VA_ARGS__); abort();}
65
66#include "chipmunk_types.h"
67
70
72#ifndef CP_BUFFER_BYTES
73 #define CP_BUFFER_BYTES (32*1024)
74#endif
75
76#ifndef cpcalloc
78 #define cpcalloc calloc
79#endif
80
81#ifndef cprealloc
83 #define cprealloc realloc
84#endif
85
86#ifndef cpfree
88 #define cpfree free
89#endif
90
91typedef struct cpArray cpArray;
92typedef struct cpHashSet cpHashSet;
93
94typedef struct cpBody cpBody;
95
96typedef struct cpShape cpShape;
97typedef struct cpCircleShape cpCircleShape;
98typedef struct cpSegmentShape cpSegmentShape;
99typedef struct cpPolyShape cpPolyShape;
100
101typedef struct cpConstraint cpConstraint;
102typedef struct cpPinJoint cpPinJoint;
103typedef struct cpSlideJoint cpSlideJoint;
104typedef struct cpPivotJoint cpPivotJoint;
105typedef struct cpGrooveJoint cpGrooveJoint;
106typedef struct cpDampedSpring cpDampedSpring;
109typedef struct cpRatchetJoint cpRatchetJoint;
110typedef struct cpGearJoint cpGearJoint;
111typedef struct cpSimpleMotorJoint cpSimpleMotorJoint;
112
115typedef struct cpArbiter cpArbiter;
116
117typedef struct cpSpace cpSpace;
118
119#include "cpVect.h"
120#include "cpBB.h"
121#include "cpTransform.h"
122#include "cpSpatialIndex.h"
123
124#include "cpArbiter.h"
125
126#include "cpBody.h"
127#include "cpShape.h"
128#include "cpPolyShape.h"
129
130#include "cpConstraint.h"
131
132#include "cpSpace.h"
133
134// Munk2D 2.0.0
135#define CP_VERSION_MAJOR 2
136#define CP_VERSION_MINOR 0
137#define CP_VERSION_RELEASE 1
138
140CP_EXPORT extern const char *cpVersionString;
141
145
149
153
156
158CP_EXPORT cpFloat cpMomentForPoly(cpFloat m, int count, const cpVect *verts, cpVect offset, cpFloat radius);
159
162CP_EXPORT cpFloat cpAreaForPoly(const int count, const cpVect *verts, cpFloat radius);
163
165CP_EXPORT cpVect cpCentroidForPoly(const int count, const cpVect *verts);
166
168CP_EXPORT cpFloat cpMomentForBox(cpFloat m, cpFloat width, cpFloat height);
169
172
177CP_EXPORT int cpConvexHull(int count, const cpVect *verts, cpVect *result, int *first, cpFloat tol);
178
183#define CP_CONVEX_HULL(__count__, __verts__, __count_var__, __verts_var__) \
184cpVect *__verts_var__ = (cpVect *)alloca(__count__*sizeof(cpVect)); \
185int __count_var__ = cpConvexHull(__count__, __verts__, __verts_var__, NULL, 0.0); \
186
188static inline cpVect
189cpClosetPointOnSegment(const cpVect p, const cpVect a, const cpVect b)
190{
191 cpVect delta = cpvsub(a, b);
192 cpFloat t = cpfclamp01(cpvdot(delta, cpvsub(p, b))/cpvlengthsq(delta));
193 return cpvadd(b, cpvmult(delta, t));
194}
195
196#if defined(__has_extension)
197#if __has_extension(blocks)
198// Define alternate block based alternatives for a few of the callback heavy functions.
199// Collision handlers are post-step callbacks are not included to avoid memory management issues.
200// If you want to use blocks for those and are aware of how to correctly manage the memory, the implementation is trivial.
201
202void cpSpaceEachBody_b(cpSpace *space, void (^block)(cpBody *body));
203void cpSpaceEachShape_b(cpSpace *space, void (^block)(cpShape *shape));
204void cpSpaceEachConstraint_b(cpSpace *space, void (^block)(cpConstraint *constraint));
205
206void cpBodyEachShape_b(cpBody *body, void (^block)(cpShape *shape));
207void cpBodyEachConstraint_b(cpBody *body, void (^block)(cpConstraint *constraint));
208void cpBodyEachArbiter_b(cpBody *body, void (^block)(cpArbiter *arbiter));
209
210typedef void (^cpSpacePointQueryBlock)(cpShape *shape, cpVect point, cpFloat distance, cpVect gradient);
211void cpSpacePointQuery_b(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpSpacePointQueryBlock block);
212
213typedef void (^cpSpaceSegmentQueryBlock)(cpShape *shape, cpVect point, cpVect normal, cpFloat alpha);
214void cpSpaceSegmentQuery_b(cpSpace *space, cpVect start, cpVect end, cpFloat radius, cpShapeFilter filter, cpSpaceSegmentQueryBlock block);
215
216typedef void (^cpSpaceBBQueryBlock)(cpShape *shape);
217void cpSpaceBBQuery_b(cpSpace *space, cpBB bb, cpShapeFilter filter, cpSpaceBBQueryBlock block);
218
219typedef void (^cpSpaceShapeQueryBlock)(cpShape *shape, cpContactPointSet *points);
220cpBool cpSpaceShapeQuery_b(cpSpace *space, cpShape *shape, cpSpaceShapeQueryBlock block);
221
222#endif
223#endif
224
225
227
228#ifdef __cplusplus
229}
230
231static inline cpVect operator *(const cpVect v, const cpFloat s){return cpvmult(v, s);}
232static inline cpVect operator +(const cpVect v1, const cpVect v2){return cpvadd(v1, v2);}
233static inline cpVect operator -(const cpVect v1, const cpVect v2){return cpvsub(v1, v2);}
234static inline cpBool operator ==(const cpVect v1, const cpVect v2){return cpveql(v1, v2);}
235static inline cpVect operator -(const cpVect v){return cpvneg(v);}
236
237#endif
238#endif
static cpFloat cpfclamp01(cpFloat f)
Clamp f to be between 0 and 1.
Definition chipmunk_types.h:144
unsigned char cpBool
Chipmunk's boolean type.
Definition chipmunk_types.h:179
double cpFloat
Chipmunk's floating point type.
Definition chipmunk_types.h:68
static cpVect cpvadd(const cpVect v1, const cpVect v2)
Add two vectors.
Definition cpVect.h:49
static cpFloat cpvdot(const cpVect v1, const cpVect v2)
Vector dot product.
Definition cpVect.h:73
static cpVect cpvmult(const cpVect v, const cpFloat s)
Scalar multiplication.
Definition cpVect.h:67
static cpVect cpvneg(const cpVect v)
Negate a vector.
Definition cpVect.h:61
static cpFloat cpvlengthsq(const cpVect v)
Returns the squared length of v. Faster than cpvlength() when you only need to compare lengths.
Definition cpVect.h:129
static cpBool cpveql(const cpVect v1, const cpVect v2)
Check if two vectors are equal. (Be careful when comparing floating point numbers!...
Definition cpVect.h:43
static cpVect cpvsub(const cpVect v1, const cpVect v2)
Subtract two vectors.
Definition cpVect.h:55
cpFloat cpMomentForPoly(cpFloat m, int count, const cpVect *verts, cpVect offset, cpFloat radius)
Calculate the moment of inertia for a solid polygon shape assuming it's center of gravity is at it's ...
cpFloat cpMomentForSegment(cpFloat m, cpVect a, cpVect b, cpFloat radius)
Calculate the moment of inertia for a line segment.
cpFloat cpMomentForBox2(cpFloat m, cpBB box)
Calculate the moment of inertia for a solid box.
int cpConvexHull(int count, const cpVect *verts, cpVect *result, int *first, cpFloat tol)
Calculate the convex hull of a given set of points.
cpFloat cpAreaForSegment(cpVect a, cpVect b, cpFloat radius)
Calculate the area of a fattened (capsule shaped) line segment.
cpFloat cpAreaForPoly(const int count, const cpVect *verts, cpFloat radius)
Calculate the signed area of a polygon.
cpFloat cpAreaForCircle(cpFloat r1, cpFloat r2)
Calculate area of a hollow circle.
static cpVect cpClosetPointOnSegment(const cpVect p, const cpVect a, const cpVect b)
Returns the closest point on the line segment ab, to the point p.
Definition chipmunk.h:189
cpVect cpCentroidForPoly(const int count, const cpVect *verts)
Calculate the natural centroid of a polygon.
const char * cpVersionString
Version string.
cpFloat cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset)
Calculate the moment of inertia for a circle.
cpFloat cpMomentForBox(cpFloat m, cpFloat width, cpFloat height)
Calculate the moment of inertia for a solid box.
Definition chipmunk_structs.h:124
Definition chipmunk_structs.h:31
Chipmunk's axis-aligned 2D bounding box type. (left, bottom, right, top)
Definition cpBB.h:34
Definition chipmunk_structs.h:36
Definition chipmunk_structs.h:203
Struct that holds function callback pointers to configure custom collision handling.
Definition cpSpace.h:42
Definition chipmunk_structs.h:251
A struct that wraps up the important collision data for an arbiter.
Definition cpArbiter.h:88
Definition chipmunk_structs.h:340
Definition chipmunk_structs.h:322
Definition chipmunk_structs.h:374
Definition chipmunk_structs.h:308
Definition chipmunk_structs.h:271
Definition chipmunk_structs.h:297
Definition chipmunk_structs.h:226
Definition chipmunk_structs.h:364
Definition chipmunk_structs.h:354
Definition chipmunk_structs.h:210
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
Definition chipmunk_structs.h:284
Definition chipmunk_structs.h:397
Definition chipmunk_types.h:251