Debian Codegraph @

demo/Crane.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 cpBody *dollyBody = NULL;
26 // Constraint used as a servo motor to move the dolly back and forth.
27 static cpConstraint *dollyServo = NULL;
28
29 // Constraint used as a winch motor to lift the load.
30 static cpConstraint *winchServo = NULL;
31
32 // Temporary joint used to hold the hook to the load.
33 static cpConstraint *hookJoint = NULL;
34
35
36 static void
37 update(cpSpace *space, double dt)
38 {
39 // Set the first anchor point (the one attached to the static body) of the dolly servo to the mouse's x position.
40 cpPivotJointSetAnchorA(dollyServo, cpv(ChipmunkDemoMouse.x, 100));
41
42 // Set the max length of the winch servo to match the mouse's height.
43 cpSlideJointSetMax(winchServo, cpfmax(100 - ChipmunkDemoMouse.y, 50));
44
45 if(hookJoint && ChipmunkDemoRightClick){
46 cpSpaceRemoveConstraint(space, hookJoint);
47 cpConstraintFree(hookJoint);
48 hookJoint = NULL;
49 }
50
51 cpSpaceStep(space, dt);
52 }
53
54 enum COLLISION_TYPES {
55 HOOK_SENSOR = 1,
56 CRATE,
57 };
58
59 static void
60 AttachHook(cpSpace *space, cpBody *hook, cpBody *crate)
61 {
62 hookJoint = cpSpaceAddConstraint(space, cpPivotJointNew(hook, crate, cpBodyGetPosition(hook)));
63 }
64
65
66 static cpBool
67 HookCrate(cpArbiter *arb, cpSpace *space, void *data)
68 {
69 if(hookJoint == NULL){
70 // Get pointers to the two bodies in the collision pair and define local variables for them.
71 // Their order matches the order of the collision types passed
72 // to the collision handler this function was defined for
73 CP_ARBITER_GET_BODIES(arb, hook, crate);
74
75 // additions and removals can't be done in a normal callback.
76 // Schedule a post step callback to do it.
77 // Use the hook as the key and pass along the arbiter.
78 cpSpaceAddPostStepCallback(space, (cpPostStepFunc)AttachHook, hook, crate);
79 }
80
81 return cpTrue; // return value is ignored for sensor callbacks anyway
82 }
83
84
85 static cpSpace *
86 init(void)
87 {
88 ChipmunkDemoMessageString = "Control the crane by moving the mouse. Right click to release.";
89
90 cpSpace *space = cpSpaceNew();
91 cpSpaceSetIterations(space, 30);
92 cpSpaceSetGravity(space, cpv(0, -100));
93 cpSpaceSetDamping(space, 0.8);
94
95 cpBody *staticBody = cpSpaceGetStaticBody(space);
96 cpShape *shape;
97
98 shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f));
99 cpShapeSetElasticity(shape, 1.0f);
100 cpShapeSetFriction(shape, 1.0f);
101 cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER);
102
103 // Add a body for the dolly.
104 dollyBody = cpSpaceAddBody(space, cpBodyNew(10, INFINITY));
105 cpBodySetPosition(dollyBody, cpv(0, 100));
106
107 // Add a block so you can see it.
108 cpSpaceAddShape(space, cpBoxShapeNew(dollyBody, 30, 30, 0.0));
109
110 // Add a groove joint for it to move back and forth on.
111 cpSpaceAddConstraint(space, cpGrooveJointNew(staticBody, dollyBody, cpv(-250, 100), cpv(250, 100), cpvzero));
112
113 // Add a pivot joint to act as a servo motor controlling it's position
114 // By updating the anchor points of the pivot joint, you can move the dolly.
115 dollyServo = cpSpaceAddConstraint(space, cpPivotJointNew(staticBody, dollyBody, cpBodyGetPosition(dollyBody)));
116 // Max force the dolly servo can generate.
117 cpConstraintSetMaxForce(dollyServo, 10000);
118 // Max speed of the dolly servo
119 cpConstraintSetMaxBias(dollyServo, 100);
120 // You can also change the error bias to control how it slows down.
121 //cpConstraintSetErrorBias(dollyServo, 0.2);
122
123
124 // Add the crane hook.
125 cpBody *hookBody = cpSpaceAddBody(space, cpBodyNew(1, INFINITY));
126 cpBodySetPosition(hookBody, cpv(0, 50));
127
128 // Add a sensor shape for it. This will be used to figure out when the hook touches a box.
129 shape = cpSpaceAddShape(space, cpCircleShapeNew(hookBody, 10, cpvzero));
130 cpShapeSetSensor(shape, cpTrue);
131 cpShapeSetCollisionType(shape, HOOK_SENSOR);
132
133 // Add a slide joint to act as a winch motor
134 // By updating the max length of the joint you can make it pull up the load.
135 winchServo = cpSpaceAddConstraint(space, cpSlideJointNew(dollyBody, hookBody, cpvzero, cpvzero, 0, INFINITY));
136 // Max force the dolly servo can generate.
137 cpConstraintSetMaxForce(winchServo, 30000);
138 // Max speed of the dolly servo
139 cpConstraintSetMaxBias(winchServo, 60);
140
141 // TODO: cleanup
142 // Finally a box to play with
143 cpBody *boxBody = cpSpaceAddBody(space, cpBodyNew(30, cpMomentForBox(30, 50, 50)));
144 cpBodySetPosition(boxBody, cpv(200, -200));
145
146 // Add a block so you can see it.
147 shape = cpSpaceAddShape(space, cpBoxShapeNew(boxBody, 50, 50, 0.0));
148 cpShapeSetFriction(shape, 0.7);
149 cpShapeSetCollisionType(shape, CRATE);
150
151 cpCollisionHandler *handler = cpSpaceAddCollisionHandler(space, HOOK_SENSOR, CRATE);
152 handler->beginFunc = (cpCollisionBeginFunc)HookCrate;
153
154
155 return space;
156 }
157
158 static void
159 destroy(cpSpace *space)
160 {
161 ChipmunkDemoFreeSpaceChildren(space);
162 cpSpaceFree(space);
163 }
164
165 ChipmunkDemo Crane = {
166 "Crane",
167 1.0/60.0,
168 init,
169 update,
170 ChipmunkDemoDefaultDrawImpl,
171 destroy,
172 };