| 1 | /* Copyright (c) 2007 Scott Lembcke |
| 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.h" |
| 23 | #include "ChipmunkDemo.h" |
| 24 | |
| 25 | static cpVect QUERY_START = {0,0}; |
| 26 | |
| 27 | static void |
| 28 | update(cpSpace *space, double dt) |
| 29 | { |
| 30 | cpSpaceStep(space, dt); |
| 31 | |
| 32 | if(ChipmunkDemoRightClick){ |
| 33 | QUERY_START = ChipmunkDemoMouse; |
| 34 | } |
| 35 | |
| 36 | cpVect start = QUERY_START; |
| 37 | cpVect end = ChipmunkDemoMouse; |
| 38 | cpFloat radius = 10.0; |
| 39 | ChipmunkDebugDrawSegment(start, end, RGBAColor(0,1,0,1)); |
| 40 | |
| 41 | ChipmunkDemoPrintString("Query: Dist(%f) Point(%5.2f, %5.2f), ", cpvdist(start, end), end.x, end.y); |
| 42 | |
| 43 | cpSegmentQueryInfo segInfo = {0}; |
| 44 | if(cpSpaceSegmentQueryFirst(space, start, end, radius, CP_SHAPE_FILTER_ALL, &segInfo)){ |
| 45 | cpVect point = segInfo.point; |
| 46 | cpVect n = segInfo.normal; |
| 47 | |
| 48 | // Draw blue over the occluded part of the query |
| 49 | ChipmunkDebugDrawSegment(cpvlerp(start, end, segInfo.alpha), end, RGBAColor(0,0,1,1)); |
| 50 | |
| 51 | // Draw a little red surface normal |
| 52 | ChipmunkDebugDrawSegment(point, cpvadd(point, cpvmult(n, 16)), RGBAColor(1,0,0,1)); |
| 53 | |
| 54 | // Draw a little red dot on the hit point. |
| 55 | ChipmunkDebugDrawDot(3, point, RGBAColor(1,0,0,1)); |
| 56 | |
| 57 | |
| 58 | ChipmunkDemoPrintString("Segment Query: Dist(%f) Normal(%5.2f, %5.2f)", segInfo.alpha*cpvdist(start, end), n.x, n.y); |
| 59 | } else { |
| 60 | ChipmunkDemoPrintString("Segment Query (None)"); |
| 61 | } |
| 62 | |
| 63 | // Draw a fat green line over the unoccluded part of the query |
| 64 | ChipmunkDebugDrawFatSegment(start, cpvlerp(start, end, segInfo.alpha), radius, RGBAColor(0,1,0,1), LAColor(0,0)); |
| 65 | |
| 66 | cpPointQueryInfo nearestInfo = {0}; |
| 67 | cpSpacePointQueryNearest(space, ChipmunkDemoMouse, 100.0, CP_SHAPE_FILTER_ALL, &nearestInfo); |
| 68 | if(nearestInfo.shape){ |
| 69 | // Draw a grey line to the closest shape. |
| 70 | ChipmunkDebugDrawDot(3, ChipmunkDemoMouse, RGBAColor(0.5, 0.5, 0.5, 1.0)); |
| 71 | ChipmunkDebugDrawSegment(ChipmunkDemoMouse, nearestInfo.point, RGBAColor(0.5, 0.5, 0.5, 1.0)); |
| 72 | |
| 73 | // Draw a red bounding box around the shape under the mouse. |
| 74 | if(nearestInfo.distance < 0) ChipmunkDebugDrawBB(cpShapeGetBB(nearestInfo.shape), RGBAColor(1,0,0,1)); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static cpSpace * |
| 79 | init(void) |
| 80 | { |
| 81 | QUERY_START = cpvzero; |
| 82 | |
| 83 | cpSpace *space = cpSpaceNew(); |
| 84 | cpSpaceSetIterations(space, 5); |
| 85 | |
| 86 | { // add a fat segment |
| 87 | cpFloat mass = 1.0f; |
| 88 | cpFloat length = 100.0f; |
| 89 | cpVect a = cpv(-length/2.0f, 0.0f), b = cpv(length/2.0f, 0.0f); |
| 90 | |
| 91 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b, 0.0f))); |
| 92 | cpBodySetPosition(body, cpv(0.0f, 100.0f)); |
| 93 | |
| 94 | cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 20.0f)); |
| 95 | } |
| 96 | |
| 97 | { // add a static segment |
| 98 | cpSpaceAddShape(space, cpSegmentShapeNew(cpSpaceGetStaticBody(space), cpv(0, 300), cpv(300, 0), 0.0f)); |
| 99 | } |
| 100 | |
| 101 | { // add a pentagon |
| 102 | cpFloat mass = 1.0f; |
| 103 | |
| 104 | cpVect verts[5]; |
| 105 | for(int i=0; i<5; i++){ |
| 106 | cpFloat angle = -2.0f*CP_PI*i/((cpFloat) 5); |
| 107 | verts[i] = cpv(30*cos(angle), 30*sin(angle)); |
| 108 | } |
| 109 | |
| 110 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 5, verts, cpvzero, 0.0f))); |
| 111 | cpBodySetPosition(body, cpv(50.0f, 30.0f)); |
| 112 | |
| 113 | cpSpaceAddShape(space, cpPolyShapeNew(body, 5, verts, cpTransformIdentity, 10.0f)); |
| 114 | } |
| 115 | |
| 116 | { // add a circle |
| 117 | cpFloat mass = 1.0f; |
| 118 | cpFloat r = 20.0f; |
| 119 | |
| 120 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, r, cpvzero))); |
| 121 | cpBodySetPosition(body, cpv(100.0f, 100.0f)); |
| 122 | |
| 123 | cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero)); |
| 124 | } |
| 125 | |
| 126 | return space; |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | destroy(cpSpace *space) |
| 131 | { |
| 132 | ChipmunkDemoFreeSpaceChildren(space); |
| 133 | cpSpaceFree(space); |
| 134 | } |
| 135 | |
| 136 | ChipmunkDemo Query = { |
| 137 | "Segment Query", |
| 138 | 1.0/60.0, |
| 139 | init, |
| 140 | update, |
| 141 | ChipmunkDemoDefaultDrawImpl, |
| 142 | destroy, |
| 143 | }; |