Debian Codegraph @

demo/TheoJansen.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 /*
23 * The previous WalkBot demo I designed was fairly disappointing, so I implemented
24 * the mechanism that Theo Jansen uses in his kinetic sculptures. Brilliant.
25 * Read more here: http://en.wikipedia.org/wiki/Theo_Jansen
26 */
27
28 #include "chipmunk/chipmunk.h"
29 #include "ChipmunkDemo.h"
30
31 static cpConstraint *motor;
32
33 static void
34 update(cpSpace *space, double dt)
35 {
36 cpFloat coef = (2.0f + ChipmunkDemoKeyboard.y)/3.0f;
37 cpFloat rate = ChipmunkDemoKeyboard.x*10.0f*coef;
38 cpSimpleMotorSetRate(motor, rate);
39 cpConstraintSetMaxForce(motor, (rate) ? 100000.0f : 0.0f);
40
41 cpSpaceStep(space, dt);
42 }
43
44 static cpFloat seg_radius = 3.0f;
45
46 static void
47 make_leg(cpSpace *space, cpFloat side, cpFloat offset, cpBody *chassis, cpBody *crank, cpVect anchor)
48 {
49 cpVect a, b;
50 cpShape *shape;
51
52 cpFloat leg_mass = 1.0f;
53
54 // make leg
55 a = cpvzero, b = cpv(0.0f, side);
56 cpBody *upper_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f)));
57 cpBodySetPosition(upper_leg, cpv(offset, 0.0f));
58
59 shape = cpSpaceAddShape(space, cpSegmentShapeNew(upper_leg, a, b, seg_radius));
60 cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
61
62 cpSpaceAddConstraint(space, cpPivotJointNew2(chassis, upper_leg, cpv(offset, 0.0f), cpvzero));
63
64 // lower leg
65 a = cpvzero, b = cpv(0.0f, -1.0f*side);
66 cpBody *lower_leg = cpSpaceAddBody(space, cpBodyNew(leg_mass, cpMomentForSegment(leg_mass, a, b, 0.0f)));
67 cpBodySetPosition(lower_leg, cpv(offset, -side));
68
69 shape = cpSpaceAddShape(space, cpSegmentShapeNew(lower_leg, a, b, seg_radius));
70 cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
71
72 shape = cpSpaceAddShape(space, cpCircleShapeNew(lower_leg, seg_radius*2.0f, b));
73 cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
74 cpShapeSetElasticity(shape, 0.0f);
75 cpShapeSetFriction(shape, 1.0f);
76
77 cpSpaceAddConstraint(space, cpPinJointNew(chassis, lower_leg, cpv(offset, 0.0f), cpvzero));
78
79 cpSpaceAddConstraint(space, cpGearJointNew(upper_leg, lower_leg, 0.0f, 1.0f));
80
81 cpConstraint *constraint;
82 cpFloat diag = cpfsqrt(side*side + offset*offset);
83
84 constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, upper_leg, anchor, cpv(0.0f, side)));
85 cpPinJointSetDist(constraint, diag);
86
87 constraint = cpSpaceAddConstraint(space, cpPinJointNew(crank, lower_leg, anchor, cpvzero));
88 cpPinJointSetDist(constraint, diag);
89 }
90
91 static cpSpace *
92 init(void)
93 {
94 ChipmunkDemoMessageString = "Use the arrow keys to control the machine.";
95
96 cpSpace *space = cpSpaceNew();
97 cpSpaceSetIterations(space, 20);
98 cpSpaceSetGravity(space, cpv(0,-500));
99
100 cpBody *staticBody = cpSpaceGetStaticBody(space);
101 cpShape *shape;
102 cpVect a, b;
103
104 // Create segments around the edge of the screen.
105 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f));
106 cpShapeSetElasticity(shape, 1.0f);
107 cpShapeSetFriction(shape, 1.0f);
108 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
109
110 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f));
111 cpShapeSetElasticity(shape, 1.0f);
112 cpShapeSetFriction(shape, 1.0f);
113 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
114
115 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
116 cpShapeSetElasticity(shape, 1.0f);
117 cpShapeSetFriction(shape, 1.0f);
118 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
119
120 cpFloat offset = 30.0f;
121
122 // make chassis
123 cpFloat chassis_mass = 2.0f;
124 a = cpv(-offset, 0.0f), b = cpv(offset, 0.0f);
125 cpBody *chassis = cpSpaceAddBody(space, cpBodyNew(chassis_mass, cpMomentForSegment(chassis_mass, a, b, 0.0f)));
126
127 shape = cpSpaceAddShape(space, cpSegmentShapeNew(chassis, a, b, seg_radius));
128 cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
129
130 // make crank
131 cpFloat crank_mass = 1.0f;
132 cpFloat crank_radius = 13.0f;
133 cpBody *crank = cpSpaceAddBody(space, cpBodyNew(crank_mass, cpMomentForCircle(crank_mass, crank_radius, 0.0f, cpvzero)));
134
135 shape = cpSpaceAddShape(space, cpCircleShapeNew(crank, crank_radius, cpvzero));
136 cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES));
137
138 cpSpaceAddConstraint(space, cpPivotJointNew2(chassis, crank, cpvzero, cpvzero));
139
140 cpFloat side = 30.0f;
141
142 int num_legs = 2;
143 for(int i=0; i<num_legs; i++){
144 make_leg(space, side, offset, chassis, crank, cpvmult(cpvforangle((cpFloat)(2*i+0)/(cpFloat)num_legs*CP_PI), crank_radius));
145 make_leg(space, side, -offset, chassis, crank, cpvmult(cpvforangle((cpFloat)(2*i+1)/(cpFloat)num_legs*CP_PI), crank_radius));
146 }
147
148 motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(chassis, crank, 6.0f));
149
150 return space;
151 }
152
153 static void
154 destroy(cpSpace *space)
155 {
156 ChipmunkDemoFreeSpaceChildren(space);
157 cpSpaceFree(space);
158 }
159
160 ChipmunkDemo TheoJansen = {
161 "Theo Jansen Machine",
162 1.0/180.0,
163 init,
164 update,
165 ChipmunkDemoDefaultDrawImpl,
166 destroy,
167 };