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 #include "chipmunk/chipmunk_private.h"
23 #include "chipmunk/chipmunk_unsafe.h"
24
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; \
29 }
30
31 cpShape *
cpShapeInit(cpShape * shape,const cpShapeClass * klass,cpBody * body,struct cpShapeMassInfo massInfo)32 cpShapeInit(cpShape *shape, const cpShapeClass *klass, cpBody *body, struct cpShapeMassInfo massInfo)
33 {
34 shape->klass = klass;
35
36 shape->body = body;
37 shape->massInfo = massInfo;
38
39 shape->sensor = 0;
40
41 shape->e = 0.0f;
42 shape->u = 0.0f;
43 shape->surfaceV = cpvzero;
44
45 shape->type = 0;
46 shape->filter.group = CP_NO_GROUP;
47 shape->filter.categories = CP_ALL_CATEGORIES;
48 shape->filter.mask = CP_ALL_CATEGORIES;
49
50 shape->userData = NULL;
51
52 shape->space = NULL;
53
54 shape->next = NULL;
55 shape->prev = NULL;
56
57 return shape;
58 }
59
60 void
cpShapeDestroy(cpShape * shape)61 cpShapeDestroy(cpShape *shape)
62 {
63 if(shape->klass && shape->klass->destroy) shape->klass->destroy(shape);
64 }
65
66 void
cpShapeFree(cpShape * shape)67 cpShapeFree(cpShape *shape)
68 {
69 if(shape){
70 cpShapeDestroy(shape);
71 cpfree(shape);
72 }
73 }
74
75 cpSpace *
cpShapeGetSpace(const cpShape * shape)76 cpShapeGetSpace(const cpShape *shape)
77 {
78 return shape->space;
79 }
80
81 cpBody *
cpShapeGetBody(const cpShape * shape)82 cpShapeGetBody(const cpShape *shape)
83 {
84 return shape->body;
85 }
86
87 void
cpShapeSetBody(cpShape * shape,cpBody * body)88 cpShapeSetBody(cpShape *shape, cpBody *body)
89 {
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.");
91 shape->body = body;
92 }
93
cpShapeGetMass(cpShape * shape)94 cpFloat cpShapeGetMass(cpShape *shape){ return shape->massInfo.m; }
95
96 void
cpShapeSetMass(cpShape * shape,cpFloat mass)97 cpShapeSetMass(cpShape *shape, cpFloat mass){
98 cpBody *body = shape->body;
99 cpBodyActivate(body);
100
101 shape->massInfo.m = mass;
102 cpBodyAccumulateMassFromShapes(body);
103 }
104
cpShapeGetDensity(cpShape * shape)105 cpFloat cpShapeGetDensity(cpShape *shape){ return shape->massInfo.m/shape->massInfo.area; }
cpShapeSetDensity(cpShape * shape,cpFloat density)106 void cpShapeSetDensity(cpShape *shape, cpFloat density){ cpShapeSetMass(shape, density*shape->massInfo.area); }
107
cpShapeGetMoment(cpShape * shape)108 cpFloat cpShapeGetMoment(cpShape *shape){ return shape->massInfo.m*shape->massInfo.i; }
cpShapeGetArea(cpShape * shape)109 cpFloat cpShapeGetArea(cpShape *shape){ return shape->massInfo.area; }
cpShapeGetCenterOfGravity(cpShape * shape)110 cpVect cpShapeGetCenterOfGravity(cpShape *shape) { return shape->massInfo.cog; }
111
112 cpBB
cpShapeGetBB(const cpShape * shape)113 cpShapeGetBB(const cpShape *shape)
114 {
115 return shape->bb;
116 }
117
118 cpBool
cpShapeGetSensor(const cpShape * shape)119 cpShapeGetSensor(const cpShape *shape)
120 {
121 return shape->sensor;
122 }
123
124 void
cpShapeSetSensor(cpShape * shape,cpBool sensor)125 cpShapeSetSensor(cpShape *shape, cpBool sensor)
126 {
127 cpBodyActivate(shape->body);
128 shape->sensor = sensor;
129 }
130
131 cpFloat
cpShapeGetElasticity(const cpShape * shape)132 cpShapeGetElasticity(const cpShape *shape)
133 {
134 return shape->e;
135 }
136
137 void
cpShapeSetElasticity(cpShape * shape,cpFloat elasticity)138 cpShapeSetElasticity(cpShape *shape, cpFloat elasticity)
139 {
140 cpAssertHard(elasticity >= 0.0f, "Elasticity must be positive and non-zero.");
141 cpBodyActivate(shape->body);
142 shape->e = elasticity;
143 }
144
145 cpFloat
cpShapeGetFriction(const cpShape * shape)146 cpShapeGetFriction(const cpShape *shape)
147 {
148 return shape->u;
149 }
150
151 void
cpShapeSetFriction(cpShape * shape,cpFloat friction)152 cpShapeSetFriction(cpShape *shape, cpFloat friction)
153 {
154 cpAssertHard(friction >= 0.0f, "Friction must be postive and non-zero.");
155 cpBodyActivate(shape->body);
156 shape->u = friction;
157 }
158
159 cpVect
cpShapeGetSurfaceVelocity(const cpShape * shape)160 cpShapeGetSurfaceVelocity(const cpShape *shape)
161 {
162 return shape->surfaceV;
163 }
164
165 void
cpShapeSetSurfaceVelocity(cpShape * shape,cpVect surfaceVelocity)166 cpShapeSetSurfaceVelocity(cpShape *shape, cpVect surfaceVelocity)
167 {
168 cpBodyActivate(shape->body);
169 shape->surfaceV = surfaceVelocity;
170 }
171
172 cpDataPointer
cpShapeGetUserData(const cpShape * shape)173 cpShapeGetUserData(const cpShape *shape)
174 {
175 return shape->userData;
176 }
177
178 void
cpShapeSetUserData(cpShape * shape,cpDataPointer userData)179 cpShapeSetUserData(cpShape *shape, cpDataPointer userData)
180 {
181 shape->userData = userData;
182 }
183
184 cpCollisionType
cpShapeGetCollisionType(const cpShape * shape)185 cpShapeGetCollisionType(const cpShape *shape)
186 {
187 return shape->type;
188 }
189
190 void
cpShapeSetCollisionType(cpShape * shape,cpCollisionType collisionType)191 cpShapeSetCollisionType(cpShape *shape, cpCollisionType collisionType)
192 {
193 cpBodyActivate(shape->body);
194 shape->type = collisionType;
195 }
196
197 cpShapeFilter
cpShapeGetFilter(const cpShape * shape)198 cpShapeGetFilter(const cpShape *shape)
199 {
200 return shape->filter;
201 }
202
203 void
cpShapeSetFilter(cpShape * shape,cpShapeFilter filter)204 cpShapeSetFilter(cpShape *shape, cpShapeFilter filter)
205 {
206 cpBodyActivate(shape->body);
207 shape->filter = filter;
208 }
209
210 cpBB
cpShapeCacheBB(cpShape * shape)211 cpShapeCacheBB(cpShape *shape)
212 {
213 return cpShapeUpdate(shape, shape->body->transform);
214 }
215
216 cpBB
cpShapeUpdate(cpShape * shape,cpTransform transform)217 cpShapeUpdate(cpShape *shape, cpTransform transform)
218 {
219 return (shape->bb = shape->klass->cacheData(shape, transform));
220 }
221
222 cpFloat
cpShapePointQuery(const cpShape * shape,cpVect p,cpPointQueryInfo * info)223 cpShapePointQuery(const cpShape *shape, cpVect p, cpPointQueryInfo *info)
224 {
225 cpPointQueryInfo blank = {NULL, cpvzero, INFINITY, cpvzero};
226 if(info){
227 (*info) = blank;
228 } else {
229 info = ␣
230 }
231
232 shape->klass->pointQuery(shape, p, info);
233 return info->distance;
234 }
235
236
237 cpBool
cpShapeSegmentQuery(const cpShape * shape,cpVect a,cpVect b,cpFloat radius,cpSegmentQueryInfo * info)238 cpShapeSegmentQuery(const cpShape *shape, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info){
239 cpSegmentQueryInfo blank = {NULL, b, cpvzero, 1.0f};
240 if(info){
241 (*info) = blank;
242 } else {
243 info = ␣
244 }
245
246 cpPointQueryInfo nearest;
247 shape->klass->pointQuery(shape, a, &nearest);
248 if(nearest.distance <= radius){
249 info->shape = shape;
250 info->alpha = 0.0;
251 info->normal = cpvnormalize(cpvsub(a, nearest.point));
252 } else {
253 shape->klass->segmentQuery(shape, a, b, radius, info);
254 }
255
256 return (info->shape != NULL);
257 }
258
259 cpContactPointSet
cpShapesCollide(const cpShape * a,const cpShape * b)260 cpShapesCollide(const cpShape *a, const cpShape *b)
261 {
262 struct cpContact contacts[CP_MAX_CONTACTS_PER_ARBITER];
263 struct cpCollisionInfo info = cpCollide(a, b, 0, contacts);
264
265 cpContactPointSet set;
266 set.count = info.count;
267
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);
271
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;
276
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);
280 }
281
282 return set;
283 }
284
285 cpCircleShape *
cpCircleShapeAlloc(void)286 cpCircleShapeAlloc(void)
287 {
288 return (cpCircleShape *)cpcalloc(1, sizeof(cpCircleShape));
289 }
290
291 static cpBB
cpCircleShapeCacheData(cpCircleShape * circle,cpTransform transform)292 cpCircleShapeCacheData(cpCircleShape *circle, cpTransform transform)
293 {
294 cpVect c = circle->tc = cpTransformPoint(transform, circle->c);
295 return cpBBNewForCircle(c, circle->r);
296 }
297
298 static void
cpCircleShapePointQuery(cpCircleShape * circle,cpVect p,cpPointQueryInfo * info)299 cpCircleShapePointQuery(cpCircleShape *circle, cpVect p, cpPointQueryInfo *info)
300 {
301 cpVect delta = cpvsub(p, circle->tc);
302 cpFloat d = cpvlength(delta);
303 cpFloat r = circle->r;
304
305 info->shape = (cpShape *)circle;
306 info->point = cpvadd(circle->tc, cpvmult(delta, r/d)); // TODO: div/0
307 info->distance = d - r;
308
309 // Use up for the gradient if the distance is very small.
310 info->gradient = (d > MAGIC_EPSILON ? cpvmult(delta, 1.0f/d) : cpv(0.0f, 1.0f));
311 }
312
313 static void
cpCircleShapeSegmentQuery(cpCircleShape * circle,cpVect a,cpVect b,cpFloat radius,cpSegmentQueryInfo * info)314 cpCircleShapeSegmentQuery(cpCircleShape *circle, cpVect a, cpVect b, cpFloat radius, cpSegmentQueryInfo *info)
315 {
316 CircleSegmentQuery((cpShape *)circle, circle->tc, circle->r, a, b, radius, info);
317 }
318
319 static struct cpShapeMassInfo
cpCircleShapeMassInfo(cpFloat mass,cpFloat radius,cpVect center)320 cpCircleShapeMassInfo(cpFloat mass, cpFloat radius, cpVect center)
321 {
322 struct cpShapeMassInfo info = {
323 mass, cpMomentForCircle(1.0f, 0.0f, radius, cpvzero),
324 center,
325 cpAreaForCircle(0.0f, radius),
326 };
327
328 return info;
329 }
330
331 static const cpShapeClass cpCircleShapeClass = {
332 CP_CIRCLE_SHAPE,
333 (cpShapeCacheDataImpl)cpCircleShapeCacheData,
334 NULL,
335 (cpShapePointQueryImpl)cpCircleShapePointQuery,
336 (cpShapeSegmentQueryImpl)cpCircleShapeSegmentQuery,
337 };
338
339 cpCircleShape *
cpCircleShapeInit(cpCircleShape * circle,cpBody * body,cpFloat radius,cpVect offset)340 cpCircleShapeInit(cpCircleShape *circle, cpBody *body, cpFloat radius, cpVect offset)
341 {
342 circle->c = offset;
343 circle->r = radius;
344
345 cpShapeInit((cpShape *)circle, &cpCircleShapeClass, body, cpCircleShapeMassInfo(0.0f, radius, offset));
346
347 return circle;
348 }
349
350 cpShape *
cpCircleShapeNew(cpBody * body,cpFloat radius,cpVect offset)351 cpCircleShapeNew(cpBody *body, cpFloat radius, cpVect offset)
352 {
353 return (cpShape *)cpCircleShapeInit(cpCircleShapeAlloc(), body, radius, offset);
354 }
355
356 cpVect
cpCircleShapeGetOffset(const cpShape * shape)357 cpCircleShapeGetOffset(const cpShape *shape)
358 {
359 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
360 return ((cpCircleShape *)shape)->c;
361 }
362
363 cpFloat
cpCircleShapeGetRadius(const cpShape * shape)364 cpCircleShapeGetRadius(const cpShape *shape)
365 {
366 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
367 return ((cpCircleShape *)shape)->r;
368 }
369
370
371 cpSegmentShape *
cpSegmentShapeAlloc(void)372 cpSegmentShapeAlloc(void)
373 {
374 return (cpSegmentShape *)cpcalloc(1, sizeof(cpSegmentShape));
375 }
376
377 static cpBB
cpSegmentShapeCacheData(cpSegmentShape * seg,cpTransform transform)378 cpSegmentShapeCacheData(cpSegmentShape *seg, cpTransform transform)
379 {
380 seg->ta = cpTransformPoint(transform, seg->a);
381 seg->tb = cpTransformPoint(transform, seg->b);
382 seg->tn = cpTransformVect(transform, seg->n);
383
384 cpFloat l,r,b,t;
385
386 if(seg->ta.x < seg->tb.x){
387 l = seg->ta.x;
388 r = seg->tb.x;
389 } else {
390 l = seg->tb.x;
391 r = seg->ta.x;
392 }
393
394 if(seg->ta.y < seg->tb.y){
395 b = seg->ta.y;
396 t = seg->tb.y;
397 } else {
398 b = seg->tb.y;
399 t = seg->ta.y;
400 }
401
402 cpFloat rad = seg->r;
403 return cpBBNew(l - rad, b - rad, r + rad, t + rad);
404 }
405
406 static void
cpSegmentShapePointQuery(cpSegmentShape * seg,cpVect p,cpPointQueryInfo * info)407 cpSegmentShapePointQuery(cpSegmentShape *seg, cpVect p, cpPointQueryInfo *info)
408 {
409 cpVect closest = cpClosetPointOnSegment(p, seg->ta, seg->tb);
410
411 cpVect delta = cpvsub(p, closest);
412 cpFloat d = cpvlength(delta);
413 cpFloat r = seg->r;
414 cpVect g = cpvmult(delta, 1.0f/d);
415
416 info->shape = (cpShape *)seg;
417 info->point = (d ? cpvadd(closest, cpvmult(g, r)) : closest);
418 info->distance = d - r;
419
420 // Use the segment's normal if the distance is very small.
421 info->gradient = (d > MAGIC_EPSILON ? g : seg->n);
422 }
423
424 static void
cpSegmentShapeSegmentQuery(cpSegmentShape * seg,cpVect a,cpVect b,cpFloat r2,cpSegmentQueryInfo * info)425 cpSegmentShapeSegmentQuery(cpSegmentShape *seg, cpVect a, cpVect b, cpFloat r2, cpSegmentQueryInfo *info)
426 {
427 cpVect n = seg->tn;
428 cpFloat d = cpvdot(cpvsub(seg->ta, a), n);
429 cpFloat r = seg->r + r2;
430
431 cpVect flipped_n = (d > 0.0f ? cpvneg(n) : n);
432 cpVect seg_offset = cpvsub(cpvmult(flipped_n, r), a);
433
434 // Make the endpoints relative to 'a' and move them by the thickness of the segment.
435 cpVect seg_a = cpvadd(seg->ta, seg_offset);
436 cpVect seg_b = cpvadd(seg->tb, seg_offset);
437 cpVect delta = cpvsub(b, a);
438
439 if(cpvcross(delta, seg_a)*cpvcross(delta, seg_b) <= 0.0f){
440 cpFloat d_offset = d + (d > 0.0f ? -r : r);
441 cpFloat ad = -d_offset;
442 cpFloat bd = cpvdot(delta, n) - d_offset;
443
444 if(ad*bd < 0.0f){
445 cpFloat t = ad/(ad - bd);
446
447 info->shape = (cpShape *)seg;
448 info->point = cpvsub(cpvlerp(a, b, t), cpvmult(flipped_n, r2));
449 info->normal = flipped_n;
450 info->alpha = t;
451 }
452 } else if(r != 0.0f){
453 cpSegmentQueryInfo info1 = {NULL, b, cpvzero, 1.0f};
454 cpSegmentQueryInfo info2 = {NULL, b, cpvzero, 1.0f};
455 CircleSegmentQuery((cpShape *)seg, seg->ta, seg->r, a, b, r2, &info1);
456 CircleSegmentQuery((cpShape *)seg, seg->tb, seg->r, a, b, r2, &info2);
457
458 if(info1.alpha < info2.alpha){
459 (*info) = info1;
460 } else {
461 (*info) = info2;
462 }
463 }
464 }
465
466 static struct cpShapeMassInfo
cpSegmentShapeMassInfo(cpFloat mass,cpVect a,cpVect b,cpFloat r)467 cpSegmentShapeMassInfo(cpFloat mass, cpVect a, cpVect b, cpFloat r)
468 {
469 struct cpShapeMassInfo info = {
470 mass, cpMomentForBox(1.0f, cpvdist(a, b) + 2.0f*r, 2.0f*r), // TODO is an approximation.
471 cpvlerp(a, b, 0.5f),
472 cpAreaForSegment(a, b, r),
473 };
474
475 return info;
476 }
477
478 static const cpShapeClass cpSegmentShapeClass = {
479 CP_SEGMENT_SHAPE,
480 (cpShapeCacheDataImpl)cpSegmentShapeCacheData,
481 NULL,
482 (cpShapePointQueryImpl)cpSegmentShapePointQuery,
483 (cpShapeSegmentQueryImpl)cpSegmentShapeSegmentQuery,
484 };
485
486 cpSegmentShape *
cpSegmentShapeInit(cpSegmentShape * seg,cpBody * body,cpVect a,cpVect b,cpFloat r)487 cpSegmentShapeInit(cpSegmentShape *seg, cpBody *body, cpVect a, cpVect b, cpFloat r)
488 {
489 seg->a = a;
490 seg->b = b;
491 seg->n = cpvrperp(cpvnormalize(cpvsub(b, a)));
492
493 seg->r = r;
494
495 seg->a_tangent = cpvzero;
496 seg->b_tangent = cpvzero;
497
498 cpShapeInit((cpShape *)seg, &cpSegmentShapeClass, body, cpSegmentShapeMassInfo(0.0f, a, b, r));
499
500 return seg;
501 }
502
503 cpShape*
cpSegmentShapeNew(cpBody * body,cpVect a,cpVect b,cpFloat r)504 cpSegmentShapeNew(cpBody *body, cpVect a, cpVect b, cpFloat r)
505 {
506 return (cpShape *)cpSegmentShapeInit(cpSegmentShapeAlloc(), body, a, b, r);
507 }
508
509 cpVect
cpSegmentShapeGetA(const cpShape * shape)510 cpSegmentShapeGetA(const cpShape *shape)
511 {
512 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
513 return ((cpSegmentShape *)shape)->a;
514 }
515
516 cpVect
cpSegmentShapeGetB(const cpShape * shape)517 cpSegmentShapeGetB(const cpShape *shape)
518 {
519 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
520 return ((cpSegmentShape *)shape)->b;
521 }
522
523 cpVect
cpSegmentShapeGetNormal(const cpShape * shape)524 cpSegmentShapeGetNormal(const cpShape *shape)
525 {
526 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
527 return ((cpSegmentShape *)shape)->n;
528 }
529
530 cpFloat
cpSegmentShapeGetRadius(const cpShape * shape)531 cpSegmentShapeGetRadius(const cpShape *shape)
532 {
533 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
534 return ((cpSegmentShape *)shape)->r;
535 }
536
537 void
cpSegmentShapeSetNeighbors(cpShape * shape,cpVect prev,cpVect next)538 cpSegmentShapeSetNeighbors(cpShape *shape, cpVect prev, cpVect next)
539 {
540 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
541 cpSegmentShape *seg = (cpSegmentShape *)shape;
542
543 seg->a_tangent = cpvsub(prev, seg->a);
544 seg->b_tangent = cpvsub(next, seg->b);
545 }
546
547 // Unsafe API (chipmunk_unsafe.h)
548
549 // TODO setters should wake the shape up?
550
551 void
cpCircleShapeSetRadius(cpShape * shape,cpFloat radius)552 cpCircleShapeSetRadius(cpShape *shape, cpFloat radius)
553 {
554 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
555 cpCircleShape *circle = (cpCircleShape *)shape;
556
557 circle->r = radius;
558
559 cpFloat mass = shape->massInfo.m;
560 shape->massInfo = cpCircleShapeMassInfo(mass, circle->r, circle->c);
561 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
562 }
563
564 void
cpCircleShapeSetOffset(cpShape * shape,cpVect offset)565 cpCircleShapeSetOffset(cpShape *shape, cpVect offset)
566 {
567 cpAssertHard(shape->klass == &cpCircleShapeClass, "Shape is not a circle shape.");
568 cpCircleShape *circle = (cpCircleShape *)shape;
569
570 circle->c = offset;
571
572 cpFloat mass = shape->massInfo.m;
573 shape->massInfo = cpCircleShapeMassInfo(shape->massInfo.m, circle->r, circle->c);
574 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
575 }
576
577 void
cpSegmentShapeSetEndpoints(cpShape * shape,cpVect a,cpVect b)578 cpSegmentShapeSetEndpoints(cpShape *shape, cpVect a, cpVect b)
579 {
580 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
581 cpSegmentShape *seg = (cpSegmentShape *)shape;
582
583 seg->a = a;
584 seg->b = b;
585 seg->n = cpvperp(cpvnormalize(cpvsub(b, a)));
586
587 cpFloat mass = shape->massInfo.m;
588 shape->massInfo = cpSegmentShapeMassInfo(shape->massInfo.m, seg->a, seg->b, seg->r);
589 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
590 }
591
592 void
cpSegmentShapeSetRadius(cpShape * shape,cpFloat radius)593 cpSegmentShapeSetRadius(cpShape *shape, cpFloat radius)
594 {
595 cpAssertHard(shape->klass == &cpSegmentShapeClass, "Shape is not a segment shape.");
596 cpSegmentShape *seg = (cpSegmentShape *)shape;
597
598 seg->r = radius;
599
600 cpFloat mass = shape->massInfo.m;
601 shape->massInfo = cpSegmentShapeMassInfo(shape->massInfo.m, seg->a, seg->b, seg->r);
602 if(mass > 0.0f) cpBodyAccumulateMassFromShapes(shape->body);
603 }
604