| 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 | |
| 24 | #ifndef CP_SPACE_DISABLE_DEBUG_API |
| 25 | |
| 26 | static void |
| 27 | cpSpaceDebugDrawShape(cpShape *shape, cpSpaceDebugDrawOptions *options) |
| 28 | { |
| 29 | cpBody *body = shape->body; |
| 30 | cpDataPointer data = options->data; |
| 31 | |
| 32 | cpSpaceDebugColor outline_color = options->shapeOutlineColor; |
| 33 | cpSpaceDebugColor fill_color = options->colorForShape(shape, data); |
| 34 | |
| 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); |
| 39 | break; |
| 40 | } |
| 41 | case CP_SEGMENT_SHAPE: { |
| 42 | cpSegmentShape *seg = (cpSegmentShape *)shape; |
| 43 | options->drawFatSegment(seg->ta, seg->tb, seg->r, outline_color, fill_color, data); |
| 44 | break; |
| 45 | } |
| 46 | case CP_POLY_SHAPE: { |
| 47 | cpPolyShape *poly = (cpPolyShape *)shape; |
| 48 | |
| 49 | int count = poly->count; |
| 50 | struct cpSplittingPlane *planes = poly->planes; |
| 51 | cpVect *verts = (cpVect *)alloca(count*sizeof(cpVect)); |
| 52 | |
| 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); |
| 55 | break; |
| 56 | } |
| 57 | default: break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static const cpVect spring_verts[] = { |
| 62 | {0.00f, 0.0f}, |
| 63 | {0.20f, 0.0f}, |
| 64 | {0.25f, 3.0f}, |
| 65 | {0.30f,-6.0f}, |
| 66 | {0.35f, 6.0f}, |
| 67 | {0.40f,-6.0f}, |
| 68 | {0.45f, 6.0f}, |
| 69 | {0.50f,-6.0f}, |
| 70 | {0.55f, 6.0f}, |
| 71 | {0.60f,-6.0f}, |
| 72 | {0.65f, 6.0f}, |
| 73 | {0.70f,-3.0f}, |
| 74 | {0.75f, 6.0f}, |
| 75 | {0.80f, 0.0f}, |
| 76 | {1.00f, 0.0f}, |
| 77 | }; |
| 78 | static const int spring_count = sizeof(spring_verts)/sizeof(cpVect); |
| 79 | |
| 80 | static void |
| 81 | cpSpaceDebugDrawConstraint(cpConstraint *constraint, cpSpaceDebugDrawOptions *options) |
| 82 | { |
| 83 | cpDataPointer data = options->data; |
| 84 | cpSpaceDebugColor color = options->constraintColor; |
| 85 | |
| 86 | cpBody *body_a = constraint->a; |
| 87 | cpBody *body_b = constraint->b; |
| 88 | |
| 89 | if(cpConstraintIsPinJoint(constraint)){ |
| 90 | cpPinJoint *joint = (cpPinJoint *)constraint; |
| 91 | |
| 92 | cpVect a = cpTransformPoint(body_a->transform, joint->anchorA); |
| 93 | cpVect b = cpTransformPoint(body_b->transform, joint->anchorB); |
| 94 | |
| 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; |
| 100 | |
| 101 | cpVect a = cpTransformPoint(body_a->transform, joint->anchorA); |
| 102 | cpVect b = cpTransformPoint(body_b->transform, joint->anchorB); |
| 103 | |
| 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; |
| 109 | |
| 110 | cpVect a = cpTransformPoint(body_a->transform, joint->anchorA); |
| 111 | cpVect b = cpTransformPoint(body_b->transform, joint->anchorB); |
| 112 | |
| 113 | options->drawDot(5, a, color, data); |
| 114 | options->drawDot(5, b, color, data); |
| 115 | } else if(cpConstraintIsGrooveJoint(constraint)){ |
| 116 | cpGrooveJoint *joint = (cpGrooveJoint *)constraint; |
| 117 | |
| 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); |
| 121 | |
| 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; |
| 128 | |
| 129 | cpVect a = cpTransformPoint(body_a->transform, spring->anchorA); |
| 130 | cpVect b = cpTransformPoint(body_b->transform, spring->anchorB); |
| 131 | |
| 132 | options->drawDot(5, a, color, data); |
| 133 | options->drawDot(5, b, color, data); |
| 134 | |
| 135 | cpVect delta = cpvsub(b, a); |
| 136 | cpFloat cos = delta.x; |
| 137 | cpFloat sin = delta.y; |
| 138 | cpFloat s = 1.0f/cpvlength(delta); |
| 139 | |
| 140 | cpVect r1 = cpv(cos, -sin*s); |
| 141 | cpVect r2 = cpv(sin, cos*s); |
| 142 | |
| 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); |
| 147 | } |
| 148 | |
| 149 | for(int i=0; i<spring_count-1; i++){ |
| 150 | options->drawSegment(verts[i], verts[i + 1], color, data); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | cpSpaceDebugDraw(cpSpace *space, cpSpaceDebugDrawOptions *options) |
| 157 | { |
| 158 | if(options->flags & CP_SPACE_DEBUG_DRAW_SHAPES){ |
| 159 | cpSpaceEachShape(space, (cpSpaceShapeIteratorFunc)cpSpaceDebugDrawShape, options); |
| 160 | } |
| 161 | |
| 162 | if(options->flags & CP_SPACE_DEBUG_DRAW_CONSTRAINTS){ |
| 163 | cpSpaceEachConstraint(space, (cpSpaceConstraintIteratorFunc)cpSpaceDebugDrawConstraint, options); |
| 164 | } |
| 165 | |
| 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; |
| 171 | |
| 172 | for(int i=0; i<arbiters->num; i++){ |
| 173 | cpArbiter *arb = (cpArbiter*)arbiters->arr[i]; |
| 174 | cpVect n = arb->n; |
| 175 | |
| 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); |
| 179 | |
| 180 | cpFloat d = 2.0f; |
| 181 | cpVect a = cpvadd(p1, cpvmult(n, -d)); |
| 182 | cpVect b = cpvadd(p2, cpvmult(n, d)); |
| 183 | draw_seg(a, b, color, data); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | #endif |