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