Chipmunk2D Pro API Reference  6.2.1
 All Classes Functions Variables Typedefs Properties Groups Pages
chipmunk.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 
22 #ifndef CHIPMUNK_HEADER
23 #define CHIPMUNK_HEADER
24 
25 #ifdef _MSC_VER
26  #define _USE_MATH_DEFINES
27 #endif
28 
29 #include <stdlib.h>
30 #include <math.h>
31 
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35 
36 #ifndef CP_ALLOW_PRIVATE_ACCESS
37  #define CP_ALLOW_PRIVATE_ACCESS 0
38 #endif
39 
40 #if CP_ALLOW_PRIVATE_ACCESS == 1
41  #define CP_PRIVATE(__symbol__) __symbol__
42 #else
43  #define CP_PRIVATE(__symbol__) __symbol__##_private
44 #endif
45 
46 void cpMessage(const char *condition, const char *file, int line, int isError, int isHardError, const char *message, ...);
47 #ifdef NDEBUG
48  #define cpAssertWarn(__condition__, ...)
49 #else
50  #define cpAssertWarn(__condition__, ...) if(!(__condition__)) cpMessage(#__condition__, __FILE__, __LINE__, 0, 0, __VA_ARGS__)
51 #endif
52 
53 #ifdef NDEBUG
54  #define cpAssertSoft(__condition__, ...)
55 #else
56  #define cpAssertSoft(__condition__, ...) if(!(__condition__)) cpMessage(#__condition__, __FILE__, __LINE__, 1, 0, __VA_ARGS__)
57 #endif
58 
59 // Hard assertions are important and cheap to execute. They are not disabled by compiling as debug.
60 #define cpAssertHard(__condition__, ...) if(!(__condition__)) cpMessage(#__condition__, __FILE__, __LINE__, 1, 1, __VA_ARGS__)
61 
62 
63 #include "chipmunk_types.h"
64 
67 
69 #ifndef CP_BUFFER_BYTES
70  #define CP_BUFFER_BYTES (32*1024)
71 #endif
72 
73 #ifndef cpcalloc
74 
75  #define cpcalloc calloc
76 #endif
77 
78 #ifndef cprealloc
79 
80  #define cprealloc realloc
81 #endif
82 
83 #ifndef cpfree
84 
85  #define cpfree free
86 #endif
87 
88 typedef struct cpArray cpArray;
89 typedef struct cpHashSet cpHashSet;
90 
91 typedef struct cpBody cpBody;
92 typedef struct cpShape cpShape;
93 typedef struct cpConstraint cpConstraint;
94 
95 typedef struct cpCollisionHandler cpCollisionHandler;
96 typedef struct cpArbiter cpArbiter;
97 
98 typedef struct cpSpace cpSpace;
99 
100 #include "cpVect.h"
101 #include "cpBB.h"
102 #include "cpSpatialIndex.h"
103 
104 #include "cpBody.h"
105 #include "cpShape.h"
106 #include "cpPolyShape.h"
107 
108 #include "cpArbiter.h"
109 #include "constraints/cpConstraint.h"
110 
111 #include "cpSpace.h"
112 
113 // Chipmunk 6.2.1
114 #define CP_VERSION_MAJOR 6
115 #define CP_VERSION_MINOR 2
116 #define CP_VERSION_RELEASE 1
117 
119 extern const char *cpVersionString;
120 
122 void cpInitChipmunk(void);
123 
126 
127 
131 
135 
139 
142 
144 cpFloat cpMomentForPoly(cpFloat m, int numVerts, const cpVect *verts, cpVect offset);
145 
148 cpFloat cpAreaForPoly(const int numVerts, const cpVect *verts);
149 
151 cpVect cpCentroidForPoly(const int numVerts, const cpVect *verts);
152 
154 void cpRecenterPoly(const int numVerts, cpVect *verts);
155 
157 cpFloat cpMomentForBox(cpFloat m, cpFloat width, cpFloat height);
158 
161 
166 int cpConvexHull(int count, cpVect *verts, cpVect *result, int *first, cpFloat tol);
167 
168 #ifdef _MSC_VER
169 #include "malloc.h"
170 #endif
171 
176 #define CP_CONVEX_HULL(__count__, __verts__, __count_var__, __verts_var__) \
177 cpVect *__verts_var__ = (cpVect *)alloca(__count__*sizeof(cpVect)); \
178 int __count_var__ = cpConvexHull(__count__, __verts__, __verts_var__, NULL, 0.0); \
179 
180 #if defined(__has_extension)
181 #if __has_extension(blocks)
182 // Define alternate block based alternatives for a few of the callback heavy functions.
183 // Collision handlers are post-step callbacks are not included to avoid memory management issues.
184 // If you want to use blocks for those and are aware of how to correctly manage the memory, the implementation is trivial.
185 
186 void cpSpaceEachBody_b(cpSpace *space, void (^block)(cpBody *body));
187 void cpSpaceEachShape_b(cpSpace *space, void (^block)(cpShape *shape));
188 void cpSpaceEachConstraint_b(cpSpace *space, void (^block)(cpConstraint *constraint));
189 
190 void cpBodyEachShape_b(cpBody *body, void (^block)(cpShape *shape));
191 void cpBodyEachConstraint_b(cpBody *body, void (^block)(cpConstraint *constraint));
192 void cpBodyEachArbiter_b(cpBody *body, void (^block)(cpArbiter *arbiter));
193 
194 typedef void (^cpSpaceNearestPointQueryBlock)(cpShape *shape, cpFloat distance, cpVect point);
195 void cpSpaceNearestPointQuery_b(cpSpace *space, cpVect point, cpFloat maxDistance, cpLayers layers, cpGroup group, cpSpaceNearestPointQueryBlock block);
196 
197 typedef void (^cpSpaceSegmentQueryBlock)(cpShape *shape, cpFloat t, cpVect n);
198 void cpSpaceSegmentQuery_b(cpSpace *space, cpVect start, cpVect end, cpLayers layers, cpGroup group, cpSpaceSegmentQueryBlock block);
199 
200 typedef void (^cpSpaceBBQueryBlock)(cpShape *shape);
201 void cpSpaceBBQuery_b(cpSpace *space, cpBB bb, cpLayers layers, cpGroup group, cpSpaceBBQueryBlock block);
202 
203 typedef void (^cpSpaceShapeQueryBlock)(cpShape *shape, cpContactPointSet *points);
204 cpBool cpSpaceShapeQuery_b(cpSpace *space, cpShape *shape, cpSpaceShapeQueryBlock block);
205 
206 #endif
207 #endif
208 
209 
211 
212 #ifdef __cplusplus
213 }
214 
215 static inline cpVect operator *(const cpVect v, const cpFloat s){return cpvmult(v, s);}
216 static inline cpVect operator +(const cpVect v1, const cpVect v2){return cpvadd(v1, v2);}
217 static inline cpVect operator -(const cpVect v1, const cpVect v2){return cpvsub(v1, v2);}
218 static inline cpBool operator ==(const cpVect v1, const cpVect v2){return cpveql(v1, v2);}
219 static inline cpVect operator -(const cpVect v){return cpvneg(v);}
220 
221 #endif
222 #endif