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 cpShapeGetHashId(cpShape* shape)
115 return shape->hashid;
119 cpShapeGetBB(const cpShape *shape)
125 cpShapeGetSensor(const cpShape *shape)
127 return shape->sensor;
131 cpShapeSetSensor(cpShape *shape, cpBool sensor)
133 cpBodyActivate(shape->body);
134 shape->sensor = sensor;
138 cpShapeGetElasticity(const cpShape *shape)
144 cpShapeSetElasticity(cpShape *shape, cpFloat elasticity)
146 cpAssertHard(elasticity >= 0.0f, "Elasticity must be positive.");
147 cpBodyActivate(shape->body);
148 shape->e = elasticity;
152 cpShapeGetFriction(const cpShape *shape)
158 cpShapeSetFriction(cpShape *shape, cpFloat friction)
160 cpAssertHard(friction >= 0.0f, "Friction must be postive.");
161 cpBodyActivate(shape->body);
166 cpShapeGetSurfaceVelocity(const cpShape *shape)
168 return shape->surfaceV;
172 cpShapeSetSurfaceVelocity(cpShape *shape, cpVect surfaceVelocity)
174 cpBodyActivate(shape->body);
175 shape->surfaceV = surfaceVelocity;
179 cpShapeGetUserData(const cpShape *shape)
181 return shape->userData;
185 cpShapeSetUserData(cpShape *shape, cpDataPointer userData)
187 shape->userData = userData;
191 cpShapeGetCollisionType(const cpShape *shape)
197 cpShapeSetCollisionType(cpShape *shape, cpCollisionType collisionType)
199 cpBodyActivate(shape->body);
200 shape->type = collisionType;
204 cpShapeGetFilter(const cpShape *shape)
206 return shape->filter;
210 cpShapeSetFilter(cpShape *shape, cpShapeFilter filter)
212 cpBodyActivate(shape->body);
213 shape->filter = filter;
217 cpShapeCacheBB(cpShape *shape)
219 return cpShapeUpdate(shape, shape->body->transform);
223 cpShapeUpdate(cpShape *shape, cpTransform transform)
225 return (shape->bb = shape->klass->cacheData(shape, transform));
229 cpShapePointQuery(const cpShape *shape, cpVect p, cpPointQueryInfo *info)
231 cpPointQueryInfo blank = {NULL, cpvzero, INFINITY, cpvzero};
238 shape->klass->pointQuery(shape, p, info);
239 return info->distance;
244 cpShapeSegmentQuery(const cpShape *shape, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info){
245 cpSegmentQueryInfo blank = {NULL, b, cpvzero, 1.0f};
252 cpPointQueryInfo nearest;
253 shape->klass->pointQuery(shape, a, &nearest);
254 if(nearest.distance <= radius){
257 info->normal = cpvnormalize(cpvsub(a, nearest.point));
259 shape->klass->segmentQuery(shape, a, b, radius, info);
262 return (info->shape != NULL);
266 cpShapesCollide(const cpShape *a, const cpShape *b)
268 struct cpContact contacts[CP_MAX_CONTACTS_PER_ARBITER];
269 struct cpCollisionInfo info = cpCollide(a, b, 0, contacts);
271 cpContactPointSet set;
272 set.count = info.count;
274 // cpCollideShapes() may have swapped the contact order. Flip the normal.
275 cpBool swapped = (a != info.a);
276 set.normal = (swapped ? cpvneg(info.n) : info.n);
278 for(int i=0; i<info.count; i++){
279 // cpCollideShapesInfo() returns contacts with absolute positions.
280 cpVect p1 = contacts[i].r1;
281 cpVect p2 = contacts[i].r2;
283 set.points[i].pointA = (swapped ? p2 : p1);
284 set.points[i].pointB = (swapped ? p1 : p2);
285 set.points[i].distance = cpvdot(cpvsub(p2, p1), set.normal);
292 cpCircleShapeAlloc(void)
294 return (cpCircleShape *)cpcalloc(1, sizeof(cpCircleShape));
298 cpCircleShapeCacheData(cpCircleShape *circle, cpTransform transform)
300 cpVect c = circle->tc = cpTransformPoint(transform, circle->c);
301 return cpBBNewForCircle(c, circle->r);
305 cpCircleShapePointQuery(cpCircleShape *circle, cpVect p, cpPointQueryInfo *info)
307 cpVect delta = cpvsub(p, circle->tc);
308 cpFloat d = cpvlength(delta);
309 cpFloat r = circle->r;
311 info->shape = (cpShape *)circle;
312 cpFloat r_over_d = d > 0.0f ? r/d : r;
313 info->point = cpvadd(circle->tc, cpvmult(delta, r_over_d)); // TODO: div/0
314 info->distance = d - r;
316 // Use up for the gradient if the distance is very small.
317 info->gradient = (d > MAGIC_EPSILON ? cpvmult(delta, 1.0f/d) : cpv(0.0f, 1.0f));
321 cpCircleShapeSegmentQuery(cpCircleShape *circle, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info)
323 CircleSegmentQuery((cpShape *)circle, circle->tc, circle->r, a, b, radius, info);
326 static struct cpShapeMassInfo
327 cpCircleShapeMassInfo(cpFloat mass, cpFloat radius, cpVect center)
329 struct cpShapeMassInfo info = {
330 mass, cpMomentForCircle(1.0f, 0.0f, radius, cpvzero),
332 cpAreaForCircle(0.0f, radius),
338 static const cpShapeClass cpCircleShapeClass = {
340 (cpShapeCacheDataImpl)cpCircleShapeCacheData,
342 (cpShapePointQueryImpl)cpCircleShapePointQuery,
343 (cpShapeSegmentQueryImpl)cpCircleShapeSegmentQuery,
347 cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset)
352 cpShapeInit((cpShape *)circle, &cpCircleShapeClass, body, cpCircleShapeMassInfo(0.0f, radius, offset));
358 cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset)
360 return (cpShape *)cpCircleShapeInit(cpCircleShapeAlloc(), body, radius, offset);
364 cpCircleShapeGetOffset(const cpShape *shape)
366 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
367 return ((cpCircleShape *)shape)->c;
371 cpCircleShapeGetRadius(const cpShape *shape)
373 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
374 return ((cpCircleShape *)shape)->r;
379 cpSegmentShapeAlloc(void)
381 return (cpSegmentShape *)cpcalloc(1, sizeof(cpSegmentShape));
385 cpSegmentShapeCacheData(cpSegmentShape *seg, cpTransform transform)
387 seg->ta = cpTransformPoint(transform, seg->a);
388 seg->tb = cpTransformPoint(transform, seg->b);
389 seg->tn = cpTransformVect(transform, seg->n);
393 if(seg->ta.x < seg->tb.x){
401 if(seg->ta.y < seg->tb.y){
409 cpFloat rad = seg->r;
410 return cpBBNew(l - rad, b - rad, r + rad, t + rad);
414 cpSegmentShapePointQuery(cpSegmentShape *seg, cpVect p, cpPointQueryInfo *info)
416 cpVect closest = cpClosetPointOnSegment(p, seg->ta, seg->tb);
418 cpVect delta = cpvsub(p, closest);
419 cpFloat d = cpvlength(delta);
421 cpVect g = cpvmult(delta, 1.0f/d);
423 info->shape = (cpShape *)seg;
424 info->point = (d ? cpvadd(closest, cpvmult(g, r)) : closest);
425 info->distance = d - r;
427 // Use the segment's normal if the distance is very small.
428 info->gradient = (d > MAGIC_EPSILON ? g : seg->n);
432 cpSegmentShapeSegmentQuery(cpSegmentShape *seg, cpVect a, cpVect b, cpFloat r2, cpSegmentQueryInfo *info)
435 cpFloat d = cpvdot(cpvsub(seg->ta, a), n);
436 cpFloat r = seg->r + r2;
438 cpVect flipped_n = (d > 0.0f ? cpvneg(n) : n);
439 cpVect seg_offset = cpvsub(cpvmult(flipped_n, r), a);
441 // Make the endpoints relative to 'a' and move them by the thickness of the segment.
442 cpVect seg_a = cpvadd(seg->ta, seg_offset);
443 cpVect seg_b = cpvadd(seg->tb, seg_offset);
444 cpVect delta = cpvsub(b, a);
446 if(cpvcross(delta, seg_a)*cpvcross(delta, seg_b) <= 0.0f){
447 cpFloat d_offset = d + (d > 0.0f ? -r : r);
448 cpFloat ad = -d_offset;
449 cpFloat bd = cpvdot(delta, n) - d_offset;
452 cpFloat t = ad/(ad - bd);
454 info->shape = (cpShape *)seg;
455 info->point = cpvsub(cpvlerp(a, b, t), cpvmult(flipped_n, r2));
456 info->normal = flipped_n;
459 } else if(r != 0.0f){
460 cpSegmentQueryInfo info1 = {NULL, b, cpvzero, 1.0f};
461 cpSegmentQueryInfo info2 = {NULL, b, cpvzero, 1.0f};
462 CircleSegmentQuery((cpShape *)seg, seg->ta, seg->r, a, b, r2, &info1);
463 CircleSegmentQuery((cpShape *)seg, seg->tb, seg->r, a, b, r2, &info2);
465 if(info1.alpha < info2.alpha){
473 static struct cpShapeMassInfo
474 cpSegmentShapeMassInfo(cpFloat mass, cpVect a, cpVect b, cpFloat r)
476 struct cpShapeMassInfo info = {
477 mass, cpMomentForBox(1.0f, cpvdist(a, b) + 2.0f*r, 2.0f*r), // TODO is an approximation.
479 cpAreaForSegment(a, b, r),
485 static const cpShapeClass cpSegmentShapeClass = {
487 (cpShapeCacheDataImpl)cpSegmentShapeCacheData,
489 (cpShapePointQueryImpl)cpSegmentShapePointQuery,
490 (cpShapeSegmentQueryImpl)cpSegmentShapeSegmentQuery,
494 cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat r)
498 seg->n = cpvrperp(cpvnormalize(cpvsub(b, a)));
502 seg->a_tangent = cpvzero;
503 seg->b_tangent = cpvzero;
505 cpShapeInit((cpShape *)seg, &cpSegmentShapeClass, body, cpSegmentShapeMassInfo(0.0f, a, b, r));
511 cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat r)
513 return (cpShape *)cpSegmentShapeInit(cpSegmentShapeAlloc(), body, a, b, r);
517 cpSegmentShapeGetA(const cpShape *shape)
519 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
520 return ((cpSegmentShape *)shape)->a;
524 cpSegmentShapeGetB(const cpShape *shape)
526 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
527 return ((cpSegmentShape *)shape)->b;
531 cpSegmentShapeGetNormal(const cpShape *shape)
533 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
534 return ((cpSegmentShape *)shape)->n;
538 cpSegmentShapeGetRadius(const cpShape *shape)
540 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
541 return ((cpSegmentShape *)shape)->r;
545 cpSegmentShapeSetNeighbors(cpShape *shape, cpVect prev, cpVect next)
547 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
548 cpSegmentShape *seg = (cpSegmentShape *)shape;
550 seg->a_tangent = cpvsub(prev, seg->a);
551 seg->b_tangent = cpvsub(next, seg->b);
554 // Unsafe API (chipmunk_unsafe.h)
556 // TODO setters should wake the shape up?
559 cpCircleShapeSetRadius(cpShape *shape, cpFloat radius)
561 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
562 cpCircleShape *circle = (cpCircleShape *)shape;
566 cpFloat mass = shape->massInfo.m;
567 shape->massInfo = cpCircleShapeMassInfo(mass, circle->r, circle->c);
568 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
572 cpCircleShapeSetOffset(cpShape *shape, cpVect offset)
574 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
575 cpCircleShape *circle = (cpCircleShape *)shape;
579 cpFloat mass = shape->massInfo.m;
580 shape->massInfo = cpCircleShapeMassInfo(shape->massInfo.m, circle->r, circle->c);
581 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
585 cpSegmentShapeSetEndpoints(cpShape *shape, cpVect a, cpVect b)
587 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
588 cpSegmentShape *seg = (cpSegmentShape *)shape;
592 seg->n = cpvperp(cpvnormalize(cpvsub(b, a)));
594 cpFloat mass = shape->massInfo.m;
595 shape->massInfo = cpSegmentShapeMassInfo(shape->massInfo.m, seg->a, seg->b, seg->r);
596 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
600 cpSegmentShapeSetRadius(cpShape *shape, cpFloat radius)
602 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
603 cpSegmentShape *seg = (cpSegmentShape *)shape;
607 cpFloat mass = shape->massInfo.m;
608 shape->massInfo = cpSegmentShapeMassInfo(shape->massInfo.m, seg->a, seg->b, seg->r);
609 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);