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
22 #include "chipmunk/chipmunk_private.h"
23 #include "chipmunk/chipmunk_unsafe.h"
25 #define CP_DefineShapeGetter(struct, type, member, name) \
26 CP_DeclareShapeGetter(struct, type, name){ \
27 cpAssertHard(shape->klass == &struct##Class, "shape is not a "#struct); \
28 return ((struct *)shape)->member; \
32 cpShapeInit(cpShape *shape, const cpShapeClass *klass, cpBody *body, struct cpShapeMassInfo massInfo)
37 shape->massInfo = massInfo;
43 shape->surfaceV = cpvzero;
46 shape->filter.group = CP_NO_GROUP;
47 shape->filter.categories = CP_ALL_CATEGORIES;
48 shape->filter.mask = CP_ALL_CATEGORIES;
50 shape->userData = NULL;
61 cpShapeDestroy(cpShape *shape)
63 if(shape->klass && shape->klass->destroy) shape->klass->destroy(shape);
67 cpShapeFree(cpShape *shape)
70 cpShapeDestroy(shape);
76 cpShapeGetSpace(const cpShape *shape)
82 cpShapeGetBody(const cpShape *shape)
88 cpShapeSetBody(cpShape *shape, cpBody *body)
90 cpAssertHard(!cpShapeActive(shape), "You cannot change the body on an active shape. You must remove the shape from the space before changing the body.");
94 cpFloat cpShapeGetMass(cpShape *shape){ return shape->massInfo.m; }
97 cpShapeSetMass(cpShape *shape, cpFloat mass){
98 cpBody *body = shape->body;
101 shape->massInfo.m = mass;
102 cpBodyAccumulateMassFromShapes(body);
105 cpFloat cpShapeGetDensity(cpShape *shape){ return shape->massInfo.m/shape->massInfo.area; }
106 void cpShapeSetDensity(cpShape *shape, cpFloat density){ cpShapeSetMass(shape, density*shape->massInfo.area); }
108 cpFloat cpShapeGetMoment(cpShape *shape){ return shape->massInfo.m*shape->massInfo.i; }
109 cpFloat cpShapeGetArea(cpShape *shape){ return shape->massInfo.area; }
110 cpVect cpShapeGetCenterOfGravity(cpShape *shape) { return shape->massInfo.cog; }
113 cpShapeGetBB(const cpShape *shape)
119 cpShapeGetSensor(const cpShape *shape)
121 return shape->sensor;
125 cpShapeSetSensor(cpShape *shape, cpBool sensor)
127 cpBodyActivate(shape->body);
128 shape->sensor = sensor;
132 cpShapeGetElasticity(const cpShape *shape)
138 cpShapeSetElasticity(cpShape *shape, cpFloat elasticity)
140 cpAssertHard(elasticity >= 0.0f, "Elasticity must be positive.");
141 cpBodyActivate(shape->body);
142 shape->e = elasticity;
146 cpShapeGetFriction(const cpShape *shape)
152 cpShapeSetFriction(cpShape *shape, cpFloat friction)
154 cpAssertHard(friction >= 0.0f, "Friction must be postive.");
155 cpBodyActivate(shape->body);
160 cpShapeGetSurfaceVelocity(const cpShape *shape)
162 return shape->surfaceV;
166 cpShapeSetSurfaceVelocity(cpShape *shape, cpVect surfaceVelocity)
168 cpBodyActivate(shape->body);
169 shape->surfaceV = surfaceVelocity;
173 cpShapeGetUserData(const cpShape *shape)
175 return shape->userData;
179 cpShapeSetUserData(cpShape *shape, cpDataPointer userData)
181 shape->userData = userData;
185 cpShapeGetCollisionType(const cpShape *shape)
191 cpShapeSetCollisionType(cpShape *shape, cpCollisionType collisionType)
193 cpBodyActivate(shape->body);
194 shape->type = collisionType;
198 cpShapeGetFilter(const cpShape *shape)
200 return shape->filter;
204 cpShapeSetFilter(cpShape *shape, cpShapeFilter filter)
206 cpBodyActivate(shape->body);
207 shape->filter = filter;
211 cpShapeCacheBB(cpShape *shape)
213 return cpShapeUpdate(shape, shape->body->transform);
217 cpShapeUpdate(cpShape *shape, cpTransform transform)
219 return (shape->bb = shape->klass->cacheData(shape, transform));
223 cpShapePointQuery(const cpShape *shape, cpVect p, cpPointQueryInfo *info)
225 cpPointQueryInfo blank = {NULL, cpvzero, INFINITY, cpvzero};
232 shape->klass->pointQuery(shape, p, info);
233 return info->distance;
238 cpShapeSegmentQuery(const cpShape *shape, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info){
239 cpSegmentQueryInfo blank = {NULL, b, cpvzero, 1.0f};
246 cpPointQueryInfo nearest;
247 shape->klass->pointQuery(shape, a, &nearest);
248 if(nearest.distance <= radius){
251 info->normal = cpvnormalize(cpvsub(a, nearest.point));
253 shape->klass->segmentQuery(shape, a, b, radius, info);
256 return (info->shape != NULL);
260 cpShapesCollide(const cpShape *a, const cpShape *b)
262 struct cpContact contacts[CP_MAX_CONTACTS_PER_ARBITER];
263 struct cpCollisionInfo info = cpCollide(a, b, 0, contacts);
265 cpContactPointSet set;
266 set.count = info.count;
268 // cpCollideShapes() may have swapped the contact order. Flip the normal.
269 cpBool swapped = (a != info.a);
270 set.normal = (swapped ? cpvneg(info.n) : info.n);
272 for(int i=0; i<info.count; i++){
273 // cpCollideShapesInfo() returns contacts with absolute positions.
274 cpVect p1 = contacts[i].r1;
275 cpVect p2 = contacts[i].r2;
277 set.points[i].pointA = (swapped ? p2 : p1);
278 set.points[i].pointB = (swapped ? p1 : p2);
279 set.points[i].distance = cpvdot(cpvsub(p2, p1), set.normal);
286 cpCircleShapeAlloc(void)
288 return (cpCircleShape *)cpcalloc(1, sizeof(cpCircleShape));
292 cpCircleShapeCacheData(cpCircleShape *circle, cpTransform transform)
294 cpVect c = circle->tc = cpTransformPoint(transform, circle->c);
295 return cpBBNewForCircle(c, circle->r);
299 cpCircleShapePointQuery(cpCircleShape *circle, cpVect p, cpPointQueryInfo *info)
301 cpVect delta = cpvsub(p, circle->tc);
302 cpFloat d = cpvlength(delta);
303 cpFloat r = circle->r;
305 info->shape = (cpShape *)circle;
306 cpFloat r_over_d = d > 0.0f ? r/d : r;
307 info->point = cpvadd(circle->tc, cpvmult(delta, r_over_d)); // TODO: div/0
308 info->distance = d - r;
310 // Use up for the gradient if the distance is very small.
311 info->gradient = (d > MAGIC_EPSILON ? cpvmult(delta, 1.0f/d) : cpv(0.0f, 1.0f));
315 cpCircleShapeSegmentQuery(cpCircleShape *circle, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info)
317 CircleSegmentQuery((cpShape *)circle, circle->tc, circle->r, a, b, radius, info);
320 static struct cpShapeMassInfo
321 cpCircleShapeMassInfo(cpFloat mass, cpFloat radius, cpVect center)
323 struct cpShapeMassInfo info = {
324 mass, cpMomentForCircle(1.0f, 0.0f, radius, cpvzero),
326 cpAreaForCircle(0.0f, radius),
332 static const cpShapeClass cpCircleShapeClass = {
334 (cpShapeCacheDataImpl)cpCircleShapeCacheData,
336 (cpShapePointQueryImpl)cpCircleShapePointQuery,
337 (cpShapeSegmentQueryImpl)cpCircleShapeSegmentQuery,
341 cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset)
346 cpShapeInit((cpShape *)circle, &cpCircleShapeClass, body, cpCircleShapeMassInfo(0.0f, radius, offset));
352 cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset)
354 return (cpShape *)cpCircleShapeInit(cpCircleShapeAlloc(), body, radius, offset);
358 cpCircleShapeGetOffset(const cpShape *shape)
360 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
361 return ((cpCircleShape *)shape)->c;
365 cpCircleShapeGetRadius(const cpShape *shape)
367 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
368 return ((cpCircleShape *)shape)->r;
373 cpSegmentShapeAlloc(void)
375 return (cpSegmentShape *)cpcalloc(1, sizeof(cpSegmentShape));
379 cpSegmentShapeCacheData(cpSegmentShape *seg, cpTransform transform)
381 seg->ta = cpTransformPoint(transform, seg->a);
382 seg->tb = cpTransformPoint(transform, seg->b);
383 seg->tn = cpTransformVect(transform, seg->n);
387 if(seg->ta.x < seg->tb.x){
395 if(seg->ta.y < seg->tb.y){
403 cpFloat rad = seg->r;
404 return cpBBNew(l - rad, b - rad, r + rad, t + rad);
408 cpSegmentShapePointQuery(cpSegmentShape *seg, cpVect p, cpPointQueryInfo *info)
410 cpVect closest = cpClosetPointOnSegment(p, seg->ta, seg->tb);
412 cpVect delta = cpvsub(p, closest);
413 cpFloat d = cpvlength(delta);
415 cpVect g = cpvmult(delta, 1.0f/d);
417 info->shape = (cpShape *)seg;
418 info->point = (d ? cpvadd(closest, cpvmult(g, r)) : closest);
419 info->distance = d - r;
421 // Use the segment's normal if the distance is very small.
422 info->gradient = (d > MAGIC_EPSILON ? g : seg->n);
426 cpSegmentShapeSegmentQuery(cpSegmentShape *seg, cpVect a, cpVect b, cpFloat r2, cpSegmentQueryInfo *info)
429 cpFloat d = cpvdot(cpvsub(seg->ta, a), n);
430 cpFloat r = seg->r + r2;
432 cpVect flipped_n = (d > 0.0f ? cpvneg(n) : n);
433 cpVect seg_offset = cpvsub(cpvmult(flipped_n, r), a);
435 // Make the endpoints relative to 'a' and move them by the thickness of the segment.
436 cpVect seg_a = cpvadd(seg->ta, seg_offset);
437 cpVect seg_b = cpvadd(seg->tb, seg_offset);
438 cpVect delta = cpvsub(b, a);
440 if(cpvcross(delta, seg_a)*cpvcross(delta, seg_b) <= 0.0f){
441 cpFloat d_offset = d + (d > 0.0f ? -r : r);
442 cpFloat ad = -d_offset;
443 cpFloat bd = cpvdot(delta, n) - d_offset;
446 cpFloat t = ad/(ad - bd);
448 info->shape = (cpShape *)seg;
449 info->point = cpvsub(cpvlerp(a, b, t), cpvmult(flipped_n, r2));
450 info->normal = flipped_n;
453 } else if(r != 0.0f){
454 cpSegmentQueryInfo info1 = {NULL, b, cpvzero, 1.0f};
455 cpSegmentQueryInfo info2 = {NULL, b, cpvzero, 1.0f};
456 CircleSegmentQuery((cpShape *)seg, seg->ta, seg->r, a, b, r2, &info1);
457 CircleSegmentQuery((cpShape *)seg, seg->tb, seg->r, a, b, r2, &info2);
459 if(info1.alpha < info2.alpha){
467 static struct cpShapeMassInfo
468 cpSegmentShapeMassInfo(cpFloat mass, cpVect a, cpVect b, cpFloat r)
470 struct cpShapeMassInfo info = {
471 mass, cpMomentForBox(1.0f, cpvdist(a, b) + 2.0f*r, 2.0f*r), // TODO is an approximation.
473 cpAreaForSegment(a, b, r),
479 static const cpShapeClass cpSegmentShapeClass = {
481 (cpShapeCacheDataImpl)cpSegmentShapeCacheData,
483 (cpShapePointQueryImpl)cpSegmentShapePointQuery,
484 (cpShapeSegmentQueryImpl)cpSegmentShapeSegmentQuery,
488 cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat r)
492 seg->n = cpvrperp(cpvnormalize(cpvsub(b, a)));
496 seg->a_tangent = cpvzero;
497 seg->b_tangent = cpvzero;
499 cpShapeInit((cpShape *)seg, &cpSegmentShapeClass, body, cpSegmentShapeMassInfo(0.0f, a, b, r));
505 cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat r)
507 return (cpShape *)cpSegmentShapeInit(cpSegmentShapeAlloc(), body, a, b, r);
511 cpSegmentShapeGetA(const cpShape *shape)
513 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
514 return ((cpSegmentShape *)shape)->a;
518 cpSegmentShapeGetB(const cpShape *shape)
520 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
521 return ((cpSegmentShape *)shape)->b;
525 cpSegmentShapeGetNormal(const cpShape *shape)
527 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
528 return ((cpSegmentShape *)shape)->n;
532 cpSegmentShapeGetRadius(const cpShape *shape)
534 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
535 return ((cpSegmentShape *)shape)->r;
539 cpSegmentShapeSetNeighbors(cpShape *shape, cpVect prev, cpVect next)
541 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
542 cpSegmentShape *seg = (cpSegmentShape *)shape;
544 seg->a_tangent = cpvsub(prev, seg->a);
545 seg->b_tangent = cpvsub(next, seg->b);
548 // Unsafe API (chipmunk_unsafe.h)
550 // TODO setters should wake the shape up?
553 cpCircleShapeSetRadius(cpShape *shape, cpFloat radius)
555 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
556 cpCircleShape *circle = (cpCircleShape *)shape;
560 cpFloat mass = shape->massInfo.m;
561 shape->massInfo = cpCircleShapeMassInfo(mass, circle->r, circle->c);
562 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
566 cpCircleShapeSetOffset(cpShape *shape, cpVect offset)
568 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
569 cpCircleShape *circle = (cpCircleShape *)shape;
573 cpFloat mass = shape->massInfo.m;
574 shape->massInfo = cpCircleShapeMassInfo(shape->massInfo.m, circle->r, circle->c);
575 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
579 cpSegmentShapeSetEndpoints(cpShape *shape, cpVect a, cpVect b)
581 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
582 cpSegmentShape *seg = (cpSegmentShape *)shape;
586 seg->n = cpvperp(cpvnormalize(cpvsub(b, a)));
588 cpFloat mass = shape->massInfo.m;
589 shape->massInfo = cpSegmentShapeMassInfo(shape->massInfo.m, seg->a, seg->b, seg->r);
590 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
594 cpSegmentShapeSetRadius(cpShape *shape, cpFloat radius)
596 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
597 cpSegmentShape *seg = (cpSegmentShape *)shape;
601 cpFloat mass = shape->massInfo.m;
602 shape->massInfo = cpSegmentShapeMassInfo(shape->massInfo.m, seg->a, seg->b, seg->r);
603 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);