| 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 | #define CHAIN_COUNT 8 |
| 26 | #define LINK_COUNT 10 |
| 27 | |
| 28 | static void |
| 29 | BreakablejointPostStepRemove(cpSpace *space, cpConstraint *joint, void *unused) |
| 30 | { |
| 31 | cpSpaceRemoveConstraint(space, joint); |
| 32 | cpConstraintFree(joint); |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | BreakableJointPostSolve(cpConstraint *joint, cpSpace *space) |
| 37 | { |
| 38 | cpFloat dt = cpSpaceGetCurrentTimeStep(space); |
| 39 | |
| 40 | // Convert the impulse to a force by dividing it by the timestep. |
| 41 | cpFloat force = cpConstraintGetImpulse(joint)/dt; |
| 42 | cpFloat maxForce = cpConstraintGetMaxForce(joint); |
| 43 | |
| 44 | // If the force is almost as big as the joint's max force, break it. |
| 45 | if(force > 0.9*maxForce){ |
| 46 | cpSpaceAddPostStepCallback(space, (cpPostStepFunc)BreakablejointPostStepRemove, joint, NULL); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | update(cpSpace *space, double dt) |
| 52 | { |
| 53 | cpSpaceStep(space, dt); |
| 54 | } |
| 55 | |
| 56 | static cpSpace * |
| 57 | init(void) |
| 58 | { |
| 59 | cpSpace *space = cpSpaceNew(); |
| 60 | cpSpaceSetIterations(space, 30); |
| 61 | cpSpaceSetGravity(space, cpv(0, -100)); |
| 62 | cpSpaceSetSleepTimeThreshold(space, 0.5f); |
| 63 | |
| 64 | cpBody *body, *staticBody = cpSpaceGetStaticBody(space); |
| 65 | cpShape *shape; |
| 66 | |
| 67 | // Create segments around the edge of the screen. |
| 68 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(-320,240), 0.0f)); |
| 69 | cpShapeSetElasticity(shape, 1.0f); |
| 70 | cpShapeSetFriction(shape, 1.0f); |
| 71 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 72 | |
| 73 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(320,-240), cpv(320,240), 0.0f)); |
| 74 | cpShapeSetElasticity(shape, 1.0f); |
| 75 | cpShapeSetFriction(shape, 1.0f); |
| 76 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 77 | |
| 78 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,-240), cpv(320,-240), 0.0f)); |
| 79 | cpShapeSetElasticity(shape, 1.0f); |
| 80 | cpShapeSetFriction(shape, 1.0f); |
| 81 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 82 | |
| 83 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-320,240), cpv(320,240), 0.0f)); |
| 84 | cpShapeSetElasticity(shape, 1.0f); |
| 85 | cpShapeSetFriction(shape, 1.0f); |
| 86 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 87 | |
| 88 | cpFloat mass = 1; |
| 89 | cpFloat width = 20; |
| 90 | cpFloat height = 30; |
| 91 | |
| 92 | cpFloat spacing = width*0.3; |
| 93 | |
| 94 | // Add lots of boxes. |
| 95 | for(int i=0; i<CHAIN_COUNT; i++){ |
| 96 | cpBody *prev = NULL; |
| 97 | |
| 98 | for(int j=0; j<LINK_COUNT; j++){ |
| 99 | cpVect pos = cpv(40*(i - (CHAIN_COUNT - 1)/2.0), 240 - (j + 0.5)*height - (j + 1)*spacing); |
| 100 | |
| 101 | body = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height))); |
| 102 | cpBodySetPosition(body, pos); |
| 103 | |
| 104 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(body, cpv(0, (height - width)/2.0), cpv(0, (width - height)/2.0), width/2.0)); |
| 105 | cpShapeSetFriction(shape, 0.8f); |
| 106 | |
| 107 | cpFloat breakingForce = 80000; |
| 108 | |
| 109 | cpConstraint *constraint = NULL; |
| 110 | if(prev == NULL){ |
| 111 | constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, staticBody, cpv(0, height/2), cpv(pos.x, 240), 0, spacing)); |
| 112 | } else { |
| 113 | constraint = cpSpaceAddConstraint(space, cpSlideJointNew(body, prev, cpv(0, height/2), cpv(0, -height/2), 0, spacing)); |
| 114 | } |
| 115 | |
| 116 | cpConstraintSetMaxForce(constraint, breakingForce); |
| 117 | cpConstraintSetPostSolveFunc(constraint, BreakableJointPostSolve); |
| 118 | cpConstraintSetCollideBodies(constraint, cpFalse); |
| 119 | |
| 120 | prev = body; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | cpFloat radius = 15.0f; |
| 125 | body = cpSpaceAddBody(space, cpBodyNew(10.0f, cpMomentForCircle(10.0f, 0.0f, radius, cpvzero))); |
| 126 | cpBodySetPosition(body, cpv(0, -240 + radius+5)); |
| 127 | cpBodySetVelocity(body, cpv(0, 300)); |
| 128 | |
| 129 | shape = cpSpaceAddShape(space, cpCircleShapeNew(body, radius, cpvzero)); |
| 130 | cpShapeSetElasticity(shape, 0.0f); |
| 131 | cpShapeSetFriction(shape, 0.9f); |
| 132 | |
| 133 | return space; |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | destroy(cpSpace *space) |
| 138 | { |
| 139 | ChipmunkDemoFreeSpaceChildren(space); |
| 140 | cpSpaceFree(space); |
| 141 | } |
| 142 | |
| 143 | ChipmunkDemo Chains = { |
| 144 | "Breakable Chains", |
| 145 | 1.0/180.0, |
| 146 | init, |
| 147 | update, |
| 148 | ChipmunkDemoDefaultDrawImpl, |
| 149 | destroy, |
| 150 | }; |