1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
26 # include <android/log.h>
29 #include "chipmunk/chipmunk_private.h"
32 cpMessage(const char *condition, const char *file, int line, int isError, int isHardError, const char *message, ...)
34 fprintf(stderr, (isError ? "Aborting due to Chipmunk error: " : "Chipmunk warning: "));
37 va_start(vargs, message); {
39 __android_log_print( ANDROID_LOG_INFO, "Chipmunk", "%s(%d)", file, line );
40 __android_log_print( ANDROID_LOG_INFO, "Chipmunk", message, vargs );
42 vfprintf(stderr, message, vargs);
43 fprintf(stderr, "\n");
48 __android_log_print(ANDROID_LOG_INFO, "Chipmunk", "\tFailed condition: %s\n", condition);
49 __android_log_print(ANDROID_LOG_INFO, "Chipmunk", "\tSource:%s:%d\n", file, line);
51 fprintf(stderr, "\tFailed condition: %s\n", condition);
52 fprintf(stderr, "\tSource:%s:%d\n", file, line);
57 #define XSTR(s) STR(s)
59 const char *cpVersionString = XSTR(CP_VERSION_MAJOR) "." XSTR(CP_VERSION_MINOR) "." XSTR(CP_VERSION_RELEASE);
61 //MARK: Misc Functions
64 cpMomentForCircle(cpFloat m, cpFloat r1, cpFloat r2, cpVect offset)
66 return m*(0.5f*(r1*r1 + r2*r2) + cpvlengthsq(offset));
70 cpAreaForCircle(cpFloat r1, cpFloat r2)
72 return (cpFloat)CP_PI*cpfabs(r1*r1 - r2*r2);
76 cpMomentForSegment(cpFloat m, cpVect a, cpVect b, cpFloat r)
78 cpVect offset = cpvlerp(a, b, 0.5f);
80 // This approximates the shape as a box for rounded segments, but it's quite close.
81 cpFloat length = cpvdist(b, a) + 2.0f*r;
82 return m*((length*length + 4.0f*r*r)/12.0f + cpvlengthsq(offset));
86 cpAreaForSegment(cpVect a, cpVect b, cpFloat r)
88 return r*((cpFloat)CP_PI*r + 2.0f*cpvdist(a, b));
92 cpMomentForPoly(cpFloat m, int count, const cpVect *verts, cpVect offset, cpFloat r)
94 // TODO account for radius.
95 if(count == 2) return cpMomentForSegment(m, verts[0], verts[1], 0.0f);
99 for(int i=0; i<count; i++){
100 cpVect v1 = cpvadd(verts[i], offset);
101 cpVect v2 = cpvadd(verts[(i+1)%count], offset);
103 cpFloat a = cpvcross(v2, v1);
104 cpFloat b = cpvdot(v1, v1) + cpvdot(v1, v2) + cpvdot(v2, v2);
110 return (m*sum1)/(6.0f*sum2);
114 cpAreaForPoly(const int count, const cpVect *verts, cpFloat r)
117 cpFloat perimeter = 0.0f;
118 for(int i=0; i<count; i++){
119 cpVect v1 = verts[i];
120 cpVect v2 = verts[(i+1)%count];
122 area += cpvcross(v1, v2);
123 perimeter += cpvdist(v1, v2);
126 return r*(CP_PI*cpfabs(r) + perimeter) + area/2.0f;
130 cpCentroidForPoly(const int count, const cpVect *verts)
133 cpVect vsum = cpvzero;
135 for(int i=0; i<count; i++){
136 cpVect v1 = verts[i];
137 cpVect v2 = verts[(i+1)%count];
138 cpFloat cross = cpvcross(v1, v2);
141 vsum = cpvadd(vsum, cpvmult(cpvadd(v1, v2), cross));
144 return cpvmult(vsum, 1.0f/(3.0f*sum));
148 //cpRecenterPoly(const int count, cpVect *verts){
149 // cpVect centroid = cpCentroidForPoly(count, verts);
151 // for(int i=0; i<count; i++){
152 // verts[i] = cpvsub(verts[i], centroid);
157 cpMomentForBox(cpFloat m, cpFloat width, cpFloat height)
159 return m*(width*width + height*height)/12.0f;
163 cpMomentForBox2(cpFloat m, cpBB box)
165 cpFloat width = box.r - box.l;
166 cpFloat height = box.t - box.b;
167 cpVect offset = cpvmult(cpv(box.l + box.r, box.b + box.t), 0.5f);
169 // TODO: NaN when offset is 0 and m is INFINITY
170 return cpMomentForBox(m, width, height) + m*cpvlengthsq(offset);
176 cpLoopIndexes(const cpVect *verts, int count, int *start, int *end)
178 (*start) = (*end) = 0;
179 cpVect min = verts[0];
182 for(int i=1; i<count; i++){
185 if(v.x < min.x || (v.x == min.x && v.y < min.y)){
188 } else if(v.x > max.x || (v.x == max.x && v.y > max.y)){
195 #define SWAP(__A__, __B__) {cpVect __TMP__ = __A__; __A__ = __B__; __B__ = __TMP__;}
198 QHullPartition(cpVect *verts, int count, cpVect a, cpVect b, cpFloat tol)
200 if(count == 0) return 0;
205 cpVect delta = cpvsub(b, a);
206 cpFloat valueTol = tol*cpvlength(delta);
209 for(int tail = count-1; head <= tail;){
210 cpFloat value = cpvcross(cpvsub(verts[head], a), delta);
211 if(value > valueTol){
219 SWAP(verts[head], verts[tail]);
224 // move the new pivot to the front if it's not already there.
225 if(pivot != 0) SWAP(verts[0], verts[pivot]);
230 QHullReduce(cpFloat tol, cpVect *verts, int count, cpVect a, cpVect pivot, cpVect b, cpVect *result)
234 } else if(count == 0) {
238 int left_count = QHullPartition(verts, count, a, pivot, tol);
239 int index = QHullReduce(tol, verts + 1, left_count - 1, a, verts[0], pivot, result);
241 result[index++] = pivot;
243 int right_count = QHullPartition(verts + left_count, count - left_count, pivot, b, tol);
244 return index + QHullReduce(tol, verts + left_count + 1, right_count - 1, pivot, verts[left_count], b, result + index);
248 // QuickHull seemed like a neat algorithm, and efficient-ish for large input sets.
249 // My implementation performs an in place reduction using the result array as scratch space.
251 cpConvexHull(int count, const cpVect *verts, cpVect *result, int *first, cpFloat tol)
254 // Copy the line vertexes into the empty part of the result polyline to use as a scratch buffer.
255 memcpy(result, verts, count*sizeof(cpVect));
258 // Degenerate case, all points are the same.
260 cpLoopIndexes(verts, count, &start, &end);
262 if(first) (*first) = 0;
266 SWAP(result[0], result[start]);
267 SWAP(result[1], result[end == 0 ? start : end]);
269 cpVect a = result[0];
270 cpVect b = result[1];
272 if(first) (*first) = start;
273 return QHullReduce(tol, result + 2, count - 2, a, b, a, result + 1) + 1;
276 //MARK: Alternate Block Iterators
278 #if defined(__has_extension)
279 #if __has_extension(blocks)
281 static void IteratorFunc(void *ptr, void (^block)(void *ptr)){block(ptr);}
283 void cpSpaceEachBody_b(cpSpace *space, void (^block)(cpBody *body)){
284 cpSpaceEachBody(space, (cpSpaceBodyIteratorFunc)IteratorFunc, block);
287 void cpSpaceEachShape_b(cpSpace *space, void (^block)(cpShape *shape)){
288 cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)IteratorFunc, block);
291 void cpSpaceEachConstraint_b(cpSpace *space, void (^block)(cpConstraint *constraint)){
292 cpSpaceEachConstraint(space, (cpSpaceConstraintIteratorFunc)IteratorFunc, block);
295 static void BodyIteratorFunc(cpBody *body, void *ptr, void (^block)(void *ptr)){block(ptr);}
297 void cpBodyEachShape_b(cpBody *body, void (^block)(cpShape *shape)){
298 cpBodyEachShape(body, (cpBodyShapeIteratorFunc)BodyIteratorFunc, block);
301 void cpBodyEachConstraint_b(cpBody *body, void (^block)(cpConstraint *constraint)){
302 cpBodyEachConstraint(body, (cpBodyConstraintIteratorFunc)BodyIteratorFunc, block);
305 void cpBodyEachArbiter_b(cpBody *body, void (^block)(cpArbiter *arbiter)){
306 cpBodyEachArbiter(body, (cpBodyArbiterIteratorFunc)BodyIteratorFunc, block);
309 static void PointQueryIteratorFunc(cpShape *shape, cpVect p, cpFloat d, cpVect g, cpSpacePointQueryBlock block){block(shape, p, d, g);}
310 void cpSpacePointQuery_b(cpSpace *space, cpVect point, cpFloat maxDistance, cpShapeFilter filter, cpSpacePointQueryBlock block){
311 cpSpacePointQuery(space, point, maxDistance, filter, (cpSpacePointQueryFunc)PointQueryIteratorFunc, block);
314 static void SegmentQueryIteratorFunc(cpShape *shape, cpVect p, cpVect n, cpFloat t, cpSpaceSegmentQueryBlock block){block(shape, p, n, t);}
315 void cpSpaceSegmentQuery_b(cpSpace *space, cpVect start, cpVect end, cpFloat radius, cpShapeFilter filter, cpSpaceSegmentQueryBlock block){
316 cpSpaceSegmentQuery(space, start, end, radius, filter, (cpSpaceSegmentQueryFunc)SegmentQueryIteratorFunc, block);
319 void cpSpaceBBQuery_b(cpSpace *space, cpBB bb, cpShapeFilter filter, cpSpaceBBQueryBlock block){
320 cpSpaceBBQuery(space, bb, filter, (cpSpaceBBQueryFunc)IteratorFunc, block);
323 static void ShapeQueryIteratorFunc(cpShape *shape, cpContactPointSet *points, cpSpaceShapeQueryBlock block){block(shape, points);}
324 cpBool cpSpaceShapeQuery_b(cpSpace *space, cpShape *shape, cpSpaceShapeQueryBlock block){
325 return cpSpaceShapeQuery(space, shape, (cpSpaceShapeQueryFunc)ShapeQueryIteratorFunc, block);
331 #include "chipmunk/chipmunk_ffi.h"