Chipmunk2D Pro API Reference  7.0.0
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
chipmunk.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 
22 #ifndef CHIPMUNK_H
23 #define CHIPMUNK_H
24 
25 #ifdef _MSC_VER
26  #define _USE_MATH_DEFINES
27 #endif
28 
29 #include <stdlib.h>
30 #include <math.h>
31 
32 #ifdef WIN32
33  // For alloca().
34  #include <malloc.h>
35 #else
36  #include <alloca.h>
37 #endif
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 // NUKE
44 #ifndef CP_ALLOW_PRIVATE_ACCESS
45  #define CP_ALLOW_PRIVATE_ACCESS 0
46 #endif
47 
48 #if CP_ALLOW_PRIVATE_ACCESS == 1
49  #define CP_PRIVATE(__symbol__) __symbol__
50 #else
51  #define CP_PRIVATE(__symbol__) __symbol__##_private
52 #endif
53 
54 void cpMessage(const char *condition, const char *file, int line, int isError, int isHardError, const char *message, ...);
55 #ifdef NDEBUG
56  #define cpAssertWarn(__condition__, ...)
57 #else
58  #define cpAssertWarn(__condition__, ...) if(!(__condition__)) cpMessage(#__condition__, __FILE__, __LINE__, 0, 0, __VA_ARGS__)
59 #endif
60 
61 #ifdef NDEBUG
62  #define cpAssertSoft(__condition__, ...)
63 #else
64  #define cpAssertSoft(__condition__, ...) if(!(__condition__)){cpMessage(#__condition__, __FILE__, __LINE__, 1, 0, __VA_ARGS__), abort();}
65 #endif
66 
67 // Hard assertions are used in situations where the program definitely will crash anyway, and the reason is inexpensive to detect.
68 #define cpAssertHard(__condition__, ...) if(!(__condition__)){cpMessage(#__condition__, __FILE__, __LINE__, 1, 1, __VA_ARGS__); abort();}
69 
70 #include "chipmunk_types.h"
71 
74 
76 #ifndef CP_BUFFER_BYTES
77  #define CP_BUFFER_BYTES (32*1024)
78 #endif
79 
80 #ifndef cpcalloc
81 
82  #define cpcalloc calloc
83 #endif
84 
85 #ifndef cprealloc
86 
87  #define cprealloc realloc
88 #endif
89 
90 #ifndef cpfree
91 
92  #define cpfree free
93 #endif
94 
95 typedef struct cpArray cpArray;
96 typedef struct cpHashSet cpHashSet;
97 
98 typedef struct cpBody cpBody;
99 
100 typedef struct cpShape cpShape;
101 typedef struct cpCircleShape cpCircleShape;
102 typedef struct cpSegmentShape cpSegmentShape;
103 typedef struct cpPolyShape cpPolyShape;
104 
105 typedef struct cpConstraint cpConstraint;
106 typedef struct cpPinJoint cpPinJoint;
107 typedef struct cpSlideJoint cpSlideJoint;
108 typedef struct cpPivotJoint cpPivotJoint;
109 typedef struct cpGrooveJoint cpGrooveJoint;
110 typedef struct cpDampedSpring cpDampedSpring;
113 typedef struct cpRatchetJoint cpRatchetJoint;
114 typedef struct cpGearJoint cpGearJoint;
115 typedef struct cpSimpleMotorJoint cpSimpleMotorJoint;
116 
118 typedef struct cpContactPointSet cpContactPointSet;
119 typedef struct cpArbiter cpArbiter;
120 
121 typedef struct cpSpace cpSpace;
122 
123 #include "cpVect.h"
124 #include "cpBB.h"
125 #include "cpTransform.h"
126 #include "cpSpatialIndex.h"
127 
128 #include "cpArbiter.h"
129 
130 #include "cpBody.h"
131 #include "cpShape.h"
132 #include "cpPolyShape.h"
133 
134 #include "cpConstraint.h"
135 
136 #include "cpSpace.h"
137 
138 // Chipmunk 7.0.0
139 #define CP_VERSION_MAJOR 7
140 #define CP_VERSION_MINOR 0
141 #define CP_VERSION_RELEASE 0
142 
144 extern const char *cpVersionString;
145 
149 
153 
157 
160 
162 cpFloat cpMomentForPoly(cpFloat m, int count, const cpVect *verts, cpVect offset, cpFloat radius);
163 
166 cpFloat cpAreaForPoly(const int count, const cpVect *verts, cpFloat radius);
167 
169 cpVect cpCentroidForPoly(const int count, const cpVect *verts);
170 
172 cpFloat cpMomentForBox(cpFloat m, cpFloat width, cpFloat height);
173 
176 
181 int cpConvexHull(int count, const cpVect *verts, cpVect *result, int *first, cpFloat tol);
182 
183 #ifdef _MSC_VER
184 #include "malloc.h"
185 #endif
186 
191 #define CP_CONVEX_HULL(__count__, __verts__, __count_var__, __verts_var__) \
192 cpVect *__verts_var__ = (cpVect *)alloca(__count__*sizeof(cpVect)); \
193 int __count_var__ = cpConvexHull(__count__, __verts__, __verts_var__, NULL, 0.0); \
194 
195 
196 static inline cpVect
197 cpClosetPointOnSegment(const cpVect p, const cpVect a, const cpVect b)
198 {
199  cpVect delta = cpvsub(a, b);
200  cpFloat t = cpfclamp01(cpvdot(delta, cpvsub(p, b))/cpvlengthsq(delta));
201  return cpvadd(b, cpvmult(delta, t));
202 }
203 
204 #if defined(__has_extension)
205 #if __has_extension(blocks)
206 // Define alternate block based alternatives for a few of the callback heavy functions.
207 // Collision handlers are post-step callbacks are not included to avoid memory management issues.
208 // If you want to use blocks for those and are aware of how to correctly manage the memory, the implementation is trivial.
209 
210 void cpSpaceEachBody_b(cpSpace *space, void (^block)(cpBody *body));
211 void cpSpaceEachShape_b(cpSpace *space, void (^block)(cpShape *shape));
212 void cpSpaceEachConstraint_b(cpSpace *space, void (^block)(cpConstraint *constraint));
213 
214 void cpBodyEachShape_b(cpBody *body, void (^block)(cpShape *shape));
215 void cpBodyEachConstraint_b(cpBody *body, void (^block)(cpConstraint *constraint));
216 void cpBodyEachArbiter_b(cpBody *body, void (^block)(cpArbiter *arbiter));
217 
218 typedef void (^cpSpacePointQueryBlock)(cpShape *shape, cpVect point, cpFloat distance, cpVect gradient);
219 void cpSpacePointQuery_b(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpSpacePointQueryBlock block);
220 
221 typedef void (^cpSpaceSegmentQueryBlock)(cpShape *shape, cpVect point, cpVect normal, cpFloat alpha);
222 void cpSpaceSegmentQuery_b(cpSpace *space, cpVect start, cpVect end, cpFloat radius, cpShapeFilter filter, cpSpaceSegmentQueryBlock block);
223 
224 typedef void (^cpSpaceBBQueryBlock)(cpShape *shape);
225 void cpSpaceBBQuery_b(cpSpace *space, cpBB bb, cpShapeFilter filter, cpSpaceBBQueryBlock block);
226 
227 typedef void (^cpSpaceShapeQueryBlock)(cpShape *shape, cpContactPointSet *points);
228 cpBool cpSpaceShapeQuery_b(cpSpace *space, cpShape *shape, cpSpaceShapeQueryBlock block);
229 
230 #endif
231 #endif
232 
233 
235 
236 #ifdef __cplusplus
237 }
238 
239 static inline cpVect operator *(const cpVect v, const cpFloat s){return cpvmult(v, s);}
240 static inline cpVect operator +(const cpVect v1, const cpVect v2){return cpvadd(v1, v2);}
241 static inline cpVect operator -(const cpVect v1, const cpVect v2){return cpvsub(v1, v2);}
242 static inline cpBool operator ==(const cpVect v1, const cpVect v2){return cpveql(v1, v2);}
243 static inline cpVect operator -(const cpVect v){return cpvneg(v);}
244 
245 #endif
246 #endif