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"
24 #ifndef CP_SPACE_DISABLE_DEBUG_API
27 cpSpaceDebugDrawShape(cpShape *shape, cpSpaceDebugDrawOptions *options)
29 cpBody *body = shape->body;
30 cpDataPointer data = options->data;
32 cpSpaceDebugColor outline_color = options->shapeOutlineColor;
33 cpSpaceDebugColor fill_color = options->colorForShape(shape, data);
35 switch(shape->klass->type){
36 case CP_CIRCLE_SHAPE: {
37 cpCircleShape *circle = (cpCircleShape *)shape;
38 options->drawCircle(circle->tc, body->a, circle->r, outline_color, fill_color, data);
41 case CP_SEGMENT_SHAPE: {
42 cpSegmentShape *seg = (cpSegmentShape *)shape;
43 options->drawFatSegment(seg->ta, seg->tb, seg->r, outline_color, fill_color, data);
47 cpPolyShape *poly = (cpPolyShape *)shape;
49 int count = poly->count;
50 struct cpSplittingPlane *planes = poly->planes;
51 cpVect *verts = (cpVect *)alloca(count*sizeof(cpVect));
53 for(int i=0; i<count; i++) verts[i] = planes[i].v0;
54 options->drawPolygon(count, verts, poly->r, outline_color, fill_color, data);
61 static const cpVect spring_verts[] = {
78 static const int spring_count = sizeof(spring_verts)/sizeof(cpVect);
81 cpSpaceDebugDrawConstraint(cpConstraint *constraint, cpSpaceDebugDrawOptions *options)
83 cpDataPointer data = options->data;
84 cpSpaceDebugColor color = options->constraintColor;
86 cpBody *body_a = constraint->a;
87 cpBody *body_b = constraint->b;
89 if(cpConstraintIsPinJoint(constraint)){
90 cpPinJoint *joint = (cpPinJoint *)constraint;
92 cpVect a = cpTransformPoint(body_a->transform, joint->anchorA);
93 cpVect b = cpTransformPoint(body_b->transform, joint->anchorB);
95 options->drawDot(5, a, color, data);
96 options->drawDot(5, b, color, data);
97 options->drawSegment(a, b, color, data);
98 } else if(cpConstraintIsSlideJoint(constraint)){
99 cpSlideJoint *joint = (cpSlideJoint *)constraint;
101 cpVect a = cpTransformPoint(body_a->transform, joint->anchorA);
102 cpVect b = cpTransformPoint(body_b->transform, joint->anchorB);
104 options->drawDot(5, a, color, data);
105 options->drawDot(5, b, color, data);
106 options->drawSegment(a, b, color, data);
107 } else if(cpConstraintIsPivotJoint(constraint)){
108 cpPivotJoint *joint = (cpPivotJoint *)constraint;
110 cpVect a = cpTransformPoint(body_a->transform, joint->anchorA);
111 cpVect b = cpTransformPoint(body_b->transform, joint->anchorB);
113 options->drawDot(5, a, color, data);
114 options->drawDot(5, b, color, data);
115 } else if(cpConstraintIsGrooveJoint(constraint)){
116 cpGrooveJoint *joint = (cpGrooveJoint *)constraint;
118 cpVect a = cpTransformPoint(body_a->transform, joint->grv_a);
119 cpVect b = cpTransformPoint(body_a->transform, joint->grv_b);
120 cpVect c = cpTransformPoint(body_b->transform, joint->anchorB);
122 options->drawDot(5, c, color, data);
123 options->drawSegment(a, b, color, data);
124 } else if(cpConstraintIsDampedSpring(constraint)){
125 cpDampedSpring *spring = (cpDampedSpring *)constraint;
126 cpDataPointer data = options->data;
127 cpSpaceDebugColor color = options->constraintColor;
129 cpVect a = cpTransformPoint(body_a->transform, spring->anchorA);
130 cpVect b = cpTransformPoint(body_b->transform, spring->anchorB);
132 options->drawDot(5, a, color, data);
133 options->drawDot(5, b, color, data);
135 cpVect delta = cpvsub(b, a);
136 cpFloat cos = delta.x;
137 cpFloat sin = delta.y;
138 cpFloat s = 1.0f/cpvlength(delta);
140 cpVect r1 = cpv(cos, -sin*s);
141 cpVect r2 = cpv(sin, cos*s);
143 cpVect *verts = (cpVect *)alloca(spring_count*sizeof(cpVect));
144 for(int i=0; i<spring_count; i++){
145 cpVect v = spring_verts[i];
146 verts[i] = cpv(cpvdot(v, r1) + a.x, cpvdot(v, r2) + a.y);
149 for(int i=0; i<spring_count-1; i++){
150 options->drawSegment(verts[i], verts[i + 1], color, data);
156 cpSpaceDebugDraw(cpSpace *space, cpSpaceDebugDrawOptions *options)
158 if(options->flags & CP_SPACE_DEBUG_DRAW_SHAPES){
159 cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)cpSpaceDebugDrawShape, options);
162 if(options->flags & CP_SPACE_DEBUG_DRAW_CONSTRAINTS){
163 cpSpaceEachConstraint(space, (cpSpaceConstraintIteratorFunc)cpSpaceDebugDrawConstraint, options);
166 if(options->flags & CP_SPACE_DEBUG_DRAW_COLLISION_POINTS){
167 cpArray *arbiters = space->arbiters;
168 cpSpaceDebugColor color = options->collisionPointColor;
169 cpSpaceDebugDrawSegmentImpl draw_seg = options->drawSegment;
170 cpDataPointer data = options->data;
172 for(int i=0; i<arbiters->num; i++){
173 cpArbiter *arb = (cpArbiter*)arbiters->arr[i];
176 for(int j=0; j<arb->count; j++){
177 cpVect p1 = cpvadd(arb->body_a->p, arb->contacts[j].r1);
178 cpVect p2 = cpvadd(arb->body_b->p, arb->contacts[j].r2);
181 cpVect a = cpvadd(p1, cpvmult(n, -d));
182 cpVect b = cpvadd(p2, cpvmult(n, d));
183 draw_seg(a, b, color, data);