Debian Codegraph @

demo/Pump.c

raw symbols SCIP
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 cpConstraint *motor;
26
27 #define numBalls 5
28 static cpBody *balls[numBalls];
29
30 static void
31 update(cpSpace *space, double dt)
32 {
33 cpFloat coef = (2.0f + ChipmunkDemoKeyboard.y)/3.0f;
34 cpFloat rate = ChipmunkDemoKeyboard.x*30.0f*coef;
35
36 cpSimpleMotorSetRate(motor, rate);
37 cpConstraintSetMaxForce(motor, rate ? 1000000.0f : 0.0f);
38
39 cpSpaceStep(space, dt);
40
41 for(int i=0; i<numBalls; i++){
42 cpBody *ball = balls[i];
43 cpVect pos = cpBodyGetPosition(ball);
44
45 if(pos.x > 320.0f){
46 cpBodySetVelocity(ball, cpvzero);
47 cpBodySetPosition(ball, cpv(-224.0f, 200.0f));
48 }
49 }
50 }
51
52 static cpBody *
53 add_ball(cpSpace *space, cpVect pos)
54 {
55 cpBody *body = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForCircle(1.0f, 30, 0, cpvzero)));
56 cpBodySetPosition(body, pos);
57
58 cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, 30, cpvzero));
59 cpShapeSetElasticity(shape, 0.0f);
60 cpShapeSetFriction(shape, 0.5f);
61
62 return body;
63 }
64
65 static cpSpace *
66 init(void)
67 {
68 ChipmunkDemoMessageString = "Use the arrow keys to control the machine.";
69
70 cpSpace *space = cpSpaceNew();
71 cpSpaceSetGravity(space, cpv(0, -600));
72
73 cpBody *staticBody = cpSpaceGetStaticBody(space);
74 cpShape *shape;
75
76 // beveling all of the line segments slightly helps prevent things from getting stuck on cracks
77 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-256,300), 2.0f));
78 cpShapeSetElasticity(shape, 0.0f);
79 cpShapeSetFriction(shape, 0.5f);
80 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
81
82 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-256,16), cpv(-192,0), 2.0f));
83 cpShapeSetElasticity(shape, 0.0f);
84 cpShapeSetFriction(shape, 0.5f);
85 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
86
87 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,0), cpv(-192, -64), 2.0f));
88 cpShapeSetElasticity(shape, 0.0f);
89 cpShapeSetFriction(shape, 0.5f);
90 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
91
92 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,-64), cpv(-128,144), 2.0f));
93 cpShapeSetElasticity(shape, 0.0f);
94 cpShapeSetFriction(shape, 0.5f);
95 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
96
97 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,80), cpv(-192,176), 2.0f));
98 cpShapeSetElasticity(shape, 0.0f);
99 cpShapeSetFriction(shape, 0.5f);
100 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
101
102 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-192,176), cpv(-128,240), 2.0f));
103 cpShapeSetElasticity(shape, 0.0f);
104 cpShapeSetFriction(shape, 0.5f);
105 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
106
107 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-128,144), cpv(192,64), 2.0f));
108 cpShapeSetElasticity(shape, 0.0f);
109 cpShapeSetFriction(shape, 0.5f);
110 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
111
112 cpVect verts[] = {
113 cpv(-30,-80),
114 cpv(-30, 80),
115 cpv( 30, 64),
116 cpv( 30,-80),
117 };
118
119 cpBody *plunger = cpSpaceAddBody(space, cpBodyNew(1.0f, INFINITY));
120 cpBodySetPosition(plunger, cpv(-160,-80));
121
122 shape = cpSpaceAddShape(space, cpPolyShapeNew(plunger, 4, verts, cpTransformIdentity, 0.0));
123 cpShapeSetElasticity(shape, 1.0f);
124 cpShapeSetFriction(shape, 0.5f);
125 cpShapeSetFilter(shape, cpShapeFilterNew(CP_NO_GROUP, 1, 1));
126
127 // add balls to hopper
128 for(int i=0; i<numBalls; i++)
129 balls[i] = add_ball(space, cpv(-224 + i,80 + 64*i));
130
131 // add small gear
132 cpBody *smallGear = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 80, 0, cpvzero)));
133 cpBodySetPosition(smallGear, cpv(-160,-160));
134 cpBodySetAngle(smallGear, -CP_PI/2.0f);
135
136 shape = cpSpaceAddShape(space, cpCircleShapeNew(smallGear, 80.0f, cpvzero));
137 cpShapeSetFilter(shape, CP_SHAPE_FILTER_NONE);
138
139 cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, smallGear, cpv(-160,-160), cpvzero));
140
141 // add big gear
142 cpBody *bigGear = cpSpaceAddBody(space, cpBodyNew(40.0f, cpMomentForCircle(40.0f, 160, 0, cpvzero)));
143 cpBodySetPosition(bigGear, cpv(80,-160));
144 cpBodySetAngle(bigGear, CP_PI/2.0f);
145
146 shape = cpSpaceAddShape(space, cpCircleShapeNew(bigGear, 160.0f, cpvzero));
147 cpShapeSetFilter(shape, CP_SHAPE_FILTER_NONE);
148
149 cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, bigGear, cpv(80,-160), cpvzero));
150
151 // connect the plunger to the small gear.
152 cpSpaceAddConstraint(space, cpPinJointNew(smallGear, plunger, cpv(80,0), cpv(0,0)));
153 // connect the gears.
154 cpSpaceAddConstraint(space, cpGearJointNew(smallGear, bigGear, -CP_PI/2.0f, -2.0f));
155
156
157 // feeder mechanism
158 cpFloat bottom = -300.0f;
159 cpFloat top = 32.0f;
160 cpBody *feeder = cpSpaceAddBody(space, cpBodyNew(1.0f, cpMomentForSegment(1.0f, cpv(-224.0f, bottom), cpv(-224.0f, top), 0.0f)));
161 cpBodySetPosition(feeder, cpv(-224, (bottom + top)/2.0f));
162
163 cpFloat len = top - bottom;
164 shape = cpSpaceAddShape(space, cpSegmentShapeNew(feeder, cpv(0.0f, len/2.0f), cpv(0.0f, -len/2.0f), 20.0f));
165 cpShapeSetFilter(shape, GRAB_FILTER);
166
167 cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, feeder, cpv(-224.0f, bottom), cpv(0.0f, -len/2.0f)));
168 cpVect anchr = cpBodyWorldToLocal(feeder, cpv(-224.0f, -160.0f));
169 cpSpaceAddConstraint(space, cpPinJointNew(feeder, smallGear, anchr, cpv(0.0f, 80.0f)));
170
171 // motorize the second gear
172 motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(staticBody, bigGear, 3.0f));
173
174 return space;
175 }
176
177 static void
178 destroy(cpSpace *space)
179 {
180 ChipmunkDemoFreeSpaceChildren(space);
181 cpSpaceFree(space);
182 }
183
184 ChipmunkDemo Pump = {
185 "Pump",
186 1.0/120.0,
187 init,
188 update,
189 ChipmunkDemoDefaultDrawImpl,
190 destroy,
191 };