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
update(cpSpace * space,double dt)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 = {};
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 = {};
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 *
init(void)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 const int NUM_VERTS = 5;
104
105 cpVect verts[NUM_VERTS];
106 for(int i=0; i<NUM_VERTS; i++){
107 cpFloat angle = -2.0f*CP_PI*i/((cpFloat) NUM_VERTS);
108 verts[i] = cpv(30*cos(angle), 30*sin(angle));
109 }
110
111 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, NUM_VERTS, verts, cpvzero, 0.0f)));
112 cpBodySetPosition(body, cpv(50.0f, 30.0f));
113
114 cpSpaceAddShape(space, cpPolyShapeNew(body, NUM_VERTS, verts, cpTransformIdentity, 10.0f));
115 }
116
117 { // add a circle
118 cpFloat mass = 1.0f;
119 cpFloat r = 20.0f;
120
121 cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, r, cpvzero)));
122 cpBodySetPosition(body, cpv(100.0f, 100.0f));
123
124 cpSpaceAddShape(space, cpCircleShapeNew(body, r, cpvzero));
125 }
126
127 return space;
128 }
129
130 static void
destroy(cpSpace * space)131 destroy(cpSpace *space)
132 {
133 ChipmunkDemoFreeSpaceChildren(space);
134 cpSpaceFree(space);
135 }
136
137 ChipmunkDemo Query = {
138 "Segment Query",
139 1.0/60.0,
140 init,
141 update,
142 ChipmunkDemoDefaultDrawImpl,
143 destroy,
144 };
145