| 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 * |
| 26 | addBall(cpSpace *space, cpVect pos, cpVect boxOffset) |
| 27 | { |
| 28 | cpFloat radius = 15.0f; |
| 29 | cpFloat mass = 1.0f; |
| 30 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); |
| 31 | cpBodySetPosition(body, cpvadd(pos, boxOffset)); |
| 32 | |
| 33 | cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); |
| 34 | cpShapeSetElasticity(shape, 0.0f); |
| 35 | cpShapeSetFriction(shape, 0.7f); |
| 36 | |
| 37 | return body; |
| 38 | } |
| 39 | |
| 40 | static cpBody * |
| 41 | addLever(cpSpace *space, cpVect pos, cpVect boxOffset) |
| 42 | { |
| 43 | cpFloat mass = 1.0f; |
| 44 | cpVect a = cpv(0, 15); |
| 45 | cpVect b = cpv(0, -15); |
| 46 | |
| 47 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b, 0.0f))); |
| 48 | cpBodySetPosition(body, cpvadd(pos, cpvadd(boxOffset, cpv(0, -15)))); |
| 49 | |
| 50 | cpShape *shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 5.0f)); |
| 51 | cpShapeSetElasticity(shape, 0.0f); |
| 52 | cpShapeSetFriction(shape, 0.7f); |
| 53 | |
| 54 | return body; |
| 55 | } |
| 56 | |
| 57 | static cpBody * |
| 58 | addBar(cpSpace *space, cpVect pos, cpVect boxOffset) |
| 59 | { |
| 60 | cpFloat mass = 2.0f; |
| 61 | cpVect a = cpv(0, 30); |
| 62 | cpVect b = cpv(0, -30); |
| 63 | |
| 64 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForSegment(mass, a, b, 0.0f))); |
| 65 | cpBodySetPosition(body, cpvadd(pos, boxOffset)); |
| 66 | |
| 67 | cpShape *shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, a, b, 5.0f)); |
| 68 | cpShapeSetElasticity(shape, 0.0f); |
| 69 | cpShapeSetFriction(shape, 0.7f); |
| 70 | cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); |
| 71 | |
| 72 | return body; |
| 73 | } |
| 74 | |
| 75 | static cpBody * |
| 76 | addWheel(cpSpace *space, cpVect pos, cpVect boxOffset) |
| 77 | { |
| 78 | cpFloat radius = 15.0f; |
| 79 | cpFloat mass = 1.0f; |
| 80 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForCircle(mass, 0.0f, radius, cpvzero))); |
| 81 | cpBodySetPosition(body, cpvadd(pos, boxOffset)); |
| 82 | |
| 83 | cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); |
| 84 | cpShapeSetElasticity(shape, 0.0f); |
| 85 | cpShapeSetFriction(shape, 0.7f); |
| 86 | cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); |
| 87 | |
| 88 | return body; |
| 89 | } |
| 90 | |
| 91 | static cpBody * |
| 92 | addChassis(cpSpace *space, cpVect pos, cpVect boxOffset) |
| 93 | { |
| 94 | cpFloat mass = 5.0f; |
| 95 | cpFloat width = 80; |
| 96 | cpFloat height = 30; |
| 97 | |
| 98 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height))); |
| 99 | cpBodySetPosition(body, cpvadd(pos, boxOffset)); |
| 100 | |
| 101 | cpShape *shape = cpSpaceAddShape(space, cpBoxShapeNew(body, width, height, 0.0)); |
| 102 | cpShapeSetElasticity(shape, 0.0f); |
| 103 | cpShapeSetFriction(shape, 0.7f); |
| 104 | cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); |
| 105 | |
| 106 | return body; |
| 107 | } |
| 108 | |
| 109 | static cpSpace * |
| 110 | init(void) |
| 111 | { |
| 112 | cpSpace *space = cpSpaceNew(); |
| 113 | cpSpaceSetIterations(space, 10); |
| 114 | cpSpaceSetGravity(space, cpv(0, -100)); |
| 115 | cpSpaceSetSleepTimeThreshold(space, 0.5f); |
| 116 | |
| 117 | cpBody *staticBody = cpSpaceGetStaticBody(space); |
| 118 | cpShape *shape; |
| 119 | |
| 120 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); |
| 121 | cpShapeSetElasticity(shape, 1.0f); |
| 122 | cpShapeSetFriction(shape, 1.0f); |
| 123 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 124 | |
| 125 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,120), cpv(320,120), 0.0f)); |
| 126 | cpShapeSetElasticity(shape, 1.0f); |
| 127 | cpShapeSetFriction(shape, 1.0f); |
| 128 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 129 | |
| 130 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,0), cpv(320,0), 0.0f)); |
| 131 | cpShapeSetElasticity(shape, 1.0f); |
| 132 | cpShapeSetFriction(shape, 1.0f); |
| 133 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 134 | |
| 135 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-120), cpv(320,-120), 0.0f)); |
| 136 | cpShapeSetElasticity(shape, 1.0f); |
| 137 | cpShapeSetFriction(shape, 1.0f); |
| 138 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 139 | |
| 140 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); |
| 141 | cpShapeSetElasticity(shape, 1.0f); |
| 142 | cpShapeSetFriction(shape, 1.0f); |
| 143 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 144 | |
| 145 | |
| 146 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); |
| 147 | cpShapeSetElasticity(shape, 1.0f); |
| 148 | cpShapeSetFriction(shape, 1.0f); |
| 149 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 150 | |
| 151 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-160,-240), cpv(-160,240), 0.0f)); |
| 152 | cpShapeSetElasticity(shape, 1.0f); |
| 153 | cpShapeSetFriction(shape, 1.0f); |
| 154 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 155 | |
| 156 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,-240), cpv(0,240), 0.0f)); |
| 157 | cpShapeSetElasticity(shape, 1.0f); |
| 158 | cpShapeSetFriction(shape, 1.0f); |
| 159 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 160 | |
| 161 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(160,-240), cpv(160,240), 0.0f)); |
| 162 | cpShapeSetElasticity(shape, 1.0f); |
| 163 | cpShapeSetFriction(shape, 1.0f); |
| 164 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 165 | |
| 166 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); |
| 167 | cpShapeSetElasticity(shape, 1.0f); |
| 168 | cpShapeSetFriction(shape, 1.0f); |
| 169 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 170 | |
| 171 | cpVect boxOffset; |
| 172 | cpBody *body1, *body2; |
| 173 | |
| 174 | cpVect posA = cpv( 50, 60); |
| 175 | cpVect posB = cpv(110, 60); |
| 176 | |
| 177 | #define POS_A cpvadd(boxOffset, posA) |
| 178 | #define POS_B cpvadd(boxOffset, posB) |
| 179 | |
| 180 | // Pin Joints - Link shapes with a solid bar or pin. |
| 181 | // Keeps the anchor points the same distance apart from when the joint was created. |
| 182 | boxOffset = cpv(-320, -240); |
| 183 | body1 = addBall(space, posA, boxOffset); |
| 184 | body2 = addBall(space, posB, boxOffset); |
| 185 | cpSpaceAddConstraint(space, cpPinJointNew(body1, body2, cpv(15,0), cpv(-15,0))); |
| 186 | |
| 187 | // Slide Joints - Like pin joints but with a min/max distance. |
| 188 | // Can be used for a cheap approximation of a rope. |
| 189 | boxOffset = cpv(-160, -240); |
| 190 | body1 = addBall(space, posA, boxOffset); |
| 191 | body2 = addBall(space, posB, boxOffset); |
| 192 | cpSpaceAddConstraint(space, cpSlideJointNew(body1, body2, cpv(15,0), cpv(-15,0), 20.0f, 40.0f)); |
| 193 | |
| 194 | // Pivot Joints - Holds the two anchor points together. Like a swivel. |
| 195 | boxOffset = cpv(0, -240); |
| 196 | body1 = addBall(space, posA, boxOffset); |
| 197 | body2 = addBall(space, posB, boxOffset); |
| 198 | cpSpaceAddConstraint(space, cpPivotJointNew(body1, body2, cpvadd(boxOffset, cpv(80,60)))); |
| 199 | // cpPivotJointNew() takes it's anchor parameter in world coordinates. The anchors are calculated from that |
| 200 | // cpPivotJointNew2() lets you specify the two anchor points explicitly |
| 201 | |
| 202 | // Groove Joints - Like a pivot joint, but one of the anchors is a line segment that the pivot can slide in |
| 203 | boxOffset = cpv(160, -240); |
| 204 | body1 = addBall(space, posA, boxOffset); |
| 205 | body2 = addBall(space, posB, boxOffset); |
| 206 | cpSpaceAddConstraint(space, cpGrooveJointNew(body1, body2, cpv(30,30), cpv(30,-30), cpv(-30,0))); |
| 207 | |
| 208 | // Damped Springs |
| 209 | boxOffset = cpv(-320, -120); |
| 210 | body1 = addBall(space, posA, boxOffset); |
| 211 | body2 = addBall(space, posB, boxOffset); |
| 212 | cpSpaceAddConstraint(space, cpDampedSpringNew(body1, body2, cpv(15,0), cpv(-15,0), 20.0f, 5.0f, 0.3f)); |
| 213 | |
| 214 | // Damped Rotary Springs |
| 215 | boxOffset = cpv(-160, -120); |
| 216 | body1 = addBar(space, posA, boxOffset); |
| 217 | body2 = addBar(space, posB, boxOffset); |
| 218 | // Add some pin joints to hold the circles in place. |
| 219 | cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); |
| 220 | cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); |
| 221 | cpSpaceAddConstraint(space, cpDampedRotarySpringNew(body1, body2, 0.0f, 3000.0f, 60.0f)); |
| 222 | |
| 223 | // Rotary Limit Joint |
| 224 | boxOffset = cpv(0, -120); |
| 225 | body1 = addLever(space, posA, boxOffset); |
| 226 | body2 = addLever(space, posB, boxOffset); |
| 227 | // Add some pin joints to hold the circles in place. |
| 228 | cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); |
| 229 | cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); |
| 230 | // Hold their rotation within 90 degrees of each other. |
| 231 | cpSpaceAddConstraint(space, cpRotaryLimitJointNew(body1, body2, -CP_PI/2.0f, CP_PI/2.0f)); |
| 232 | |
| 233 | // Ratchet Joint - A rotary ratchet, like a socket wrench |
| 234 | boxOffset = cpv(160, -120); |
| 235 | body1 = addLever(space, posA, boxOffset); |
| 236 | body2 = addLever(space, posB, boxOffset); |
| 237 | // Add some pin joints to hold the circles in place. |
| 238 | cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); |
| 239 | cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); |
| 240 | // Ratchet every 90 degrees |
| 241 | cpSpaceAddConstraint(space, cpRatchetJointNew(body1, body2, 0.0f, CP_PI/2.0f)); |
| 242 | |
| 243 | // Gear Joint - Maintain a specific angular velocity ratio |
| 244 | boxOffset = cpv(-320, 0); |
| 245 | body1 = addBar(space, posA, boxOffset); |
| 246 | body2 = addBar(space, posB, boxOffset); |
| 247 | // Add some pin joints to hold the circles in place. |
| 248 | cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); |
| 249 | cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); |
| 250 | // Force one to sping 2x as fast as the other |
| 251 | cpSpaceAddConstraint(space, cpGearJointNew(body1, body2, 0.0f, 2.0f)); |
| 252 | |
| 253 | // Simple Motor - Maintain a specific angular relative velocity |
| 254 | boxOffset = cpv(-160, 0); |
| 255 | body1 = addBar(space, posA, boxOffset); |
| 256 | body2 = addBar(space, posB, boxOffset); |
| 257 | // Add some pin joints to hold the circles in place. |
| 258 | cpSpaceAddConstraint(space, cpPivotJointNew(body1, staticBody, POS_A)); |
| 259 | cpSpaceAddConstraint(space, cpPivotJointNew(body2, staticBody, POS_B)); |
| 260 | // Make them spin at 1/2 revolution per second in relation to each other. |
| 261 | cpSpaceAddConstraint(space, cpSimpleMotorNew(body1, body2, CP_PI)); |
| 262 | |
| 263 | // Make a car with some nice soft suspension |
| 264 | boxOffset = cpv(0, 0); |
| 265 | cpBody *wheel1 = addWheel(space, posA, boxOffset); |
| 266 | cpBody *wheel2 = addWheel(space, posB, boxOffset); |
| 267 | cpBody *chassis = addChassis(space, cpv(80, 100), boxOffset); |
| 268 | |
| 269 | cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel1, cpv(-30, -10), cpv(-30, -40), cpvzero)); |
| 270 | cpSpaceAddConstraint(space, cpGrooveJointNew(chassis, wheel2, cpv( 30, -10), cpv( 30, -40), cpvzero)); |
| 271 | |
| 272 | cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel1, cpv(-30, 0), cpvzero, 50.0f, 20.0f, 10.0f)); |
| 273 | cpSpaceAddConstraint(space, cpDampedSpringNew(chassis, wheel2, cpv( 30, 0), cpvzero, 50.0f, 20.0f, 10.0f)); |
| 274 | |
| 275 | return space; |
| 276 | } |
| 277 | |
| 278 | static void |
| 279 | update(cpSpace *space, double dt) |
| 280 | { |
| 281 | cpSpaceStep(space, dt); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | destroy(cpSpace *space) |
| 286 | { |
| 287 | ChipmunkDemoFreeSpaceChildren(space); |
| 288 | cpSpaceFree(space); |
| 289 | } |
| 290 | |
| 291 | ChipmunkDemo Joints = { |
| 292 | "Joints and Constraints", |
| 293 | 1.0/60.0, |
| 294 | init, |
| 295 | update, |
| 296 | ChipmunkDemoDefaultDrawImpl, |
| 297 | destroy, |
| 298 | }; |