| 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"cxx . . $ `<file>/include/chipmunk/chipmunk.h`/ |
| 23 | #include "ChipmunkDemo.h"cxx . . $ `<file>/demo/ChipmunkDemo.h`/ |
| 24 | |
| 25 | #define CHAIN_COUNTcxx . . $ `demo/Chains.c:25:9`! 8 |
| 26 | #define LINK_COUNTcxx . . $ `demo/Chains.c:26:9`! 10 |
| 27 | |
| 28 | static void |
| 29 | BreakablejointPostStepRemovecxx . . $ BreakablejointPostStepRemove(78322303cc2cca01).(cpSpacecxx . . $ cpSpace# *space, cpConstraintcxx . . $ cpConstraint# *joint, void *unused) |
| 30 | { |
| 31 | cpSpaceRemoveConstraintcxx . . $ cpSpaceRemoveConstraint(ab426c857162788d).(space, joint); |
| 32 | cpConstraintFreecxx . . $ cpConstraintFree(ce362f6e378064c6).(joint); |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | BreakableJointPostSolvecxx . . $ BreakableJointPostSolve(e6d8226ee831afeb).(cpConstraintcxx . . $ cpConstraint# *joint, cpSpacecxx . . $ cpSpace# *space) |
| 37 | { |
| 38 | cpFloatcxx . . $ cpFloat# dt = cpSpaceGetCurrentTimeStepcxx . . $ cpSpaceGetCurrentTimeStep(f2dd6279e77f9c6b).(space); |
| 39 | |
| 40 | // Convert the impulse to a force by dividing it by the timestep. |
| 41 | cpFloatcxx . . $ cpFloat# force = cpConstraintGetImpulsecxx . . $ cpConstraintGetImpulse(4f5ff132f97724f3).(joint)/dt; |
| 42 | cpFloatcxx . . $ cpFloat# maxForce = cpConstraintGetMaxForcecxx . . $ cpConstraintGetMaxForce(9c3ac5f8d34d584e).(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 | cpSpaceAddPostStepCallbackcxx . . $ cpSpaceAddPostStepCallback(1ff9997ad0bee464).(space, (cpPostStepFunccxx . . $ cpPostStepFunc#)BreakablejointPostStepRemovecxx . . $ BreakablejointPostStepRemove(78322303cc2cca01)., joint, NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 52 | { |
| 53 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 54 | } |
| 55 | |
| 56 | static cpSpacecxx . . $ cpSpace# * |
| 57 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 58 | { |
| 59 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 60 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 30); |
| 61 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -100)); |
| 62 | cpSpaceSetSleepTimeThresholdcxx . . $ cpSpaceSetSleepTimeThreshold(fa66a0a48611083e).(space, 0.5f); |
| 63 | |
| 64 | cpBodycxx . . $ cpBody# *body, *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 65 | cpShapecxx . . $ cpShape# *shape; |
| 66 | |
| 67 | // Create segments around the edge of the screen. |
| 68 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), 0.0f)); |
| 69 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 70 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 71 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 72 | |
| 73 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f)); |
| 74 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 75 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 76 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 77 | |
| 78 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), 0.0f)); |
| 79 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 80 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 81 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 82 | |
| 83 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f)); |
| 84 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 85 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 86 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 87 | |
| 88 | cpFloatcxx . . $ cpFloat# mass = 1; |
| 89 | cpFloatcxx . . $ cpFloat# width = 20; |
| 90 | cpFloatcxx . . $ cpFloat# height = 30; |
| 91 | |
| 92 | cpFloatcxx . . $ cpFloat# spacing = width*0.3; |
| 93 | |
| 94 | // Add lots of boxes. |
| 95 | for(int i=0; i<CHAIN_COUNTcxx . . $ `demo/Chains.c:25:9`!; i++){ |
| 96 | cpBodycxx . . $ cpBody# *prev = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 97 | |
| 98 | for(int j=0; j<LINK_COUNTcxx . . $ `demo/Chains.c:26:9`!; j++){ |
| 99 | cpVectcxx . . $ cpVect# pos = cpvcxx . . $ cpv(3142d57d978b2176).(40*(i - (CHAIN_COUNTcxx . . $ `demo/Chains.c:25:9`! - 1)/2.0), 240 - (j + 0.5)*height - (j + 1)*spacing); |
| 100 | |
| 101 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(mass, width, height))); |
| 102 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, pos); |
| 103 | |
| 104 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0, (height - width)/2.0), cpvcxx . . $ cpv(3142d57d978b2176).(0, (width - height)/2.0), width/2.0)); |
| 105 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.8f); |
| 106 | |
| 107 | cpFloatcxx . . $ cpFloat# breakingForce = 80000; |
| 108 | |
| 109 | cpConstraintcxx . . $ cpConstraint# *constraint = NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!; |
| 110 | if(prev == NULLcxx . . $ `<external>/16391990256953433862/stddef.h:419:9`!){ |
| 111 | constraint = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpSlideJointNewcxx . . $ cpSlideJointNew(5b97552dd9e8a693).(body, staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(0, height/2), cpvcxx . . $ cpv(3142d57d978b2176).(pos.xcxx . . $ cpVect#x., 240), 0, spacing)); |
| 112 | } else { |
| 113 | constraint = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpSlideJointNewcxx . . $ cpSlideJointNew(5b97552dd9e8a693).(body, prev, cpvcxx . . $ cpv(3142d57d978b2176).(0, height/2), cpvcxx . . $ cpv(3142d57d978b2176).(0, -height/2), 0, spacing)); |
| 114 | } |
| 115 | |
| 116 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(constraint, breakingForce); |
| 117 | cpConstraintSetPostSolveFunccxx . . $ cpConstraintSetPostSolveFunc(4f57467f17129581).(constraint, BreakableJointPostSolvecxx . . $ BreakableJointPostSolve(e6d8226ee831afeb).); |
| 118 | cpConstraintSetCollideBodiescxx . . $ cpConstraintSetCollideBodies(480f56b574d66410).(constraint, cpFalsecxx . . $ `include/chipmunk/chipmunk_types.h:188:10`!); |
| 119 | |
| 120 | prev = body; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | cpFloatcxx . . $ cpFloat# radius = 15.0f; |
| 125 | body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(10.0f, cpMomentForCirclecxx . . $ cpMomentForCircle(a539e2d95614f770).(10.0f, 0.0f, radius, cpvzerocxx . . $ cpvzero.))); |
| 126 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0, -240 + radius+5)); |
| 127 | cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(0, 300)); |
| 128 | |
| 129 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpCircleShapeNewcxx . . $ cpCircleShapeNew(e5e4e3a52c89533b).(body, radius, cpvzerocxx . . $ cpvzero.)); |
| 130 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 0.0f); |
| 131 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.9f); |
| 132 | |
| 133 | return space; |
| 134 | } |
| 135 | |
| 136 | static void |
| 137 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 138 | { |
| 139 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 140 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 141 | } |
| 142 | |
| 143 | ChipmunkDemocxx . . $ ChipmunkDemo# Chainscxx . . $ Chains. = { |
| 144 | "Breakable Chains", |
| 145 | 1.0/180.0, |
| 146 | initcxx . . $ init(1f0082e822a7ce76)., |
| 147 | updatecxx . . $ update(fa66a0a48611083e)., |
| 148 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 149 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 150 | }; |