| 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 *tankBody, *tankControlBody; |
| 26 | |
| 27 | static void |
| 28 | update(cpSpace *space, double dt) |
| 29 | { |
| 30 | // turn the control body based on the angle relative to the actual body |
| 31 | cpVect mouseDelta = cpvsub(ChipmunkDemoMouse, cpBodyGetPosition(tankBody)); |
| 32 | cpFloat turn = cpvtoangle(cpvunrotate(cpBodyGetRotation(tankBody), mouseDelta)); |
| 33 | cpBodySetAngle(tankControlBody, cpBodyGetAngle(tankBody) - turn); |
| 34 | |
| 35 | // drive the tank towards the mouse |
| 36 | if(cpvnear(ChipmunkDemoMouse, cpBodyGetPosition(tankBody), 30.0)){ |
| 37 | cpBodySetVelocity(tankControlBody, cpvzero); // stop |
| 38 | } else { |
| 39 | cpFloat direction = (cpvdot(mouseDelta, cpBodyGetRotation(tankBody)) > 0.0 ? 1.0 : -1.0); |
| 40 | cpBodySetVelocity(tankControlBody, cpvrotate(cpBodyGetRotation(tankBody), cpv(30.0f*direction, 0.0f))); |
| 41 | } |
| 42 | |
| 43 | cpSpaceStep(space, dt); |
| 44 | } |
| 45 | |
| 46 | static cpBody * |
| 47 | add_box(cpSpace *space, cpFloat size, cpFloat mass) |
| 48 | { |
| 49 | cpFloat radius = cpvlength(cpv(size, size)); |
| 50 | |
| 51 | cpBody *body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, size, size))); |
| 52 | cpBodySetPosition(body, cpv(frand()*(640 - 2*radius) - (320 - radius), frand()*(480 - 2*radius) - (240 - radius))); |
| 53 | |
| 54 | cpShape *shape = cpSpaceAddShape(space, cpBoxShapeNew(body, size, size, 0.0)); |
| 55 | cpShapeSetElasticity(shape, 0.0f); |
| 56 | cpShapeSetFriction(shape, 0.7f); |
| 57 | |
| 58 | return body; |
| 59 | } |
| 60 | |
| 61 | static cpSpace * |
| 62 | init(void) |
| 63 | { |
| 64 | ChipmunkDemoMessageString = "Use the mouse to drive the tank, it will follow the cursor."; |
| 65 | |
| 66 | cpSpace *space = cpSpaceNew(); |
| 67 | cpSpaceSetIterations(space, 10); |
| 68 | cpSpaceSetSleepTimeThreshold(space, 0.5f); |
| 69 | |
| 70 | cpBody *staticBody = cpSpaceGetStaticBody(space); |
| 71 | cpShape *shape; |
| 72 | |
| 73 | // Create segments around the edge of the screen. |
| 74 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); |
| 75 | cpShapeSetElasticity(shape, 1.0f); |
| 76 | cpShapeSetFriction(shape, 1.0f); |
| 77 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 78 | |
| 79 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); |
| 80 | cpShapeSetElasticity(shape, 1.0f); |
| 81 | cpShapeSetFriction(shape, 1.0f); |
| 82 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 83 | |
| 84 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); |
| 85 | cpShapeSetElasticity(shape, 1.0f); |
| 86 | cpShapeSetFriction(shape, 1.0f); |
| 87 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 88 | |
| 89 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); |
| 90 | cpShapeSetElasticity(shape, 1.0f); |
| 91 | cpShapeSetFriction(shape, 1.0f); |
| 92 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 93 | |
| 94 | for(int i=0; i<50; i++){ |
| 95 | cpBody *body = add_box(space, 20, 1); |
| 96 | |
| 97 | cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(staticBody, body, cpvzero, cpvzero)); |
| 98 | cpConstraintSetMaxBias(pivot, 0); // disable joint correction |
| 99 | cpConstraintSetMaxForce(pivot, 1000.0f); // emulate linear friction |
| 100 | |
| 101 | cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(staticBody, body, 0.0f, 1.0f)); |
| 102 | cpConstraintSetMaxBias(gear, 0); // disable joint correction |
| 103 | cpConstraintSetMaxForce(gear, 5000.0f); // emulate angular friction |
| 104 | } |
| 105 | |
| 106 | // We joint the tank to the control body and control the tank indirectly by modifying the control body. |
| 107 | tankControlBody = cpSpaceAddBody(space, cpBodyNewKinematic()); |
| 108 | tankBody = add_box(space, 30, 10); |
| 109 | |
| 110 | cpConstraint *pivot = cpSpaceAddConstraint(space, cpPivotJointNew2(tankControlBody, tankBody, cpvzero, cpvzero)); |
| 111 | cpConstraintSetMaxBias(pivot, 0); // disable joint correction |
| 112 | cpConstraintSetMaxForce(pivot, 10000.0f); // emulate linear friction |
| 113 | |
| 114 | cpConstraint *gear = cpSpaceAddConstraint(space, cpGearJointNew(tankControlBody, tankBody, 0.0f, 1.0f)); |
| 115 | cpConstraintSetErrorBias(gear, 0); // attempt to fully correct the joint each step |
| 116 | cpConstraintSetMaxBias(gear, 1.2f); // but limit it's angular correction rate |
| 117 | cpConstraintSetMaxForce(gear, 50000.0f); // emulate angular friction |
| 118 | |
| 119 | return space; |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | destroy(cpSpace *space) |
| 124 | { |
| 125 | ChipmunkDemoFreeSpaceChildren(space); |
| 126 | cpSpaceFree(space); |
| 127 | } |
| 128 | |
| 129 | ChipmunkDemo Tank = { |
| 130 | "Tank", |
| 131 | 1.0/60.0, |
| 132 | init, |
| 133 | update, |
| 134 | ChipmunkDemoDefaultDrawImpl, |
| 135 | destroy, |
| 136 | }; |