| 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 | static cpBodycxx . . $ cpBody# *tankBodycxx . . $ tankBody., *tankControlBodycxx . . $ tankControlBody.; |
| 26 | |
| 27 | static void |
| 28 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 29 | { |
| 30 | // turn the control body based on the angle relative to the actual body |
| 31 | cpVectcxx . . $ cpVect# mouseDelta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(tankBodycxx . . $ tankBody.)); |
| 32 | cpFloatcxx . . $ cpFloat# turn = cpvtoanglecxx . . $ cpvtoangle(c4ef53c170268e01).(cpvunrotatecxx . . $ cpvunrotate(47eb3f105675e23c).(cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(tankBodycxx . . $ tankBody.), mouseDelta)); |
| 33 | cpBodySetAnglecxx . . $ cpBodySetAngle(797bd3bb7a653b39).(tankControlBodycxx . . $ tankControlBody., cpBodyGetAnglecxx . . $ cpBodyGetAngle(2880d7999e0b34c3).(tankBodycxx . . $ tankBody.) - turn); |
| 34 | |
| 35 | // drive the tank towards the mouse |
| 36 | if(cpvnearcxx . . $ cpvnear(69cc569a1d96b8bc).(ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse., cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(tankBodycxx . . $ tankBody.), 30.0)){ |
| 37 | cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(tankControlBodycxx . . $ tankControlBody., cpvzerocxx . . $ cpvzero.); // stop |
| 38 | } else { |
| 39 | cpFloatcxx . . $ cpFloat# direction = (cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(mouseDelta, cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(tankBodycxx . . $ tankBody.)) > 0.0 ? 1.0 : -1.0); |
| 40 | cpBodySetVelocitycxx . . $ cpBodySetVelocity(864e17bcbe059b08).(tankControlBodycxx . . $ tankControlBody., cpvrotatecxx . . $ cpvrotate(47eb3f105675e23c).(cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(tankBodycxx . . $ tankBody.), cpvcxx . . $ cpv(3142d57d978b2176).(30.0f*direction, 0.0f))); |
| 41 | } |
| 42 | |
| 43 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 44 | } |
| 45 | |
| 46 | static cpBodycxx . . $ cpBody# * |
| 47 | add_boxcxx . . $ add_box(2c787cd1c450712c).(cpSpacecxx . . $ cpSpace# *space, cpFloatcxx . . $ cpFloat# size, cpFloatcxx . . $ cpFloat# mass) |
| 48 | { |
| 49 | cpFloatcxx . . $ cpFloat# radius = cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(cpvcxx . . $ cpv(3142d57d978b2176).(size, size)); |
| 50 | |
| 51 | cpBodycxx . . $ cpBody# *body = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(mass, size, size))); |
| 52 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(body, cpvcxx . . $ cpv(3142d57d978b2176).(frandcxx . . $ frand(31fa7155c10fdd13).()*(640 - 2*radius) - (320 - radius), frandcxx . . $ frand(31fa7155c10fdd13).()*(480 - 2*radius) - (240 - radius))); |
| 53 | |
| 54 | cpShapecxx . . $ cpShape# *shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(body, size, size, 0.0)); |
| 55 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 0.0f); |
| 56 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.7f); |
| 57 | |
| 58 | return body; |
| 59 | } |
| 60 | |
| 61 | static cpSpacecxx . . $ cpSpace# * |
| 62 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 63 | { |
| 64 | ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = "Use the mouse to drive the tank, it will follow the cursor."; |
| 65 | |
| 66 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 67 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 10); |
| 68 | cpSpaceSetSleepTimeThresholdcxx . . $ cpSpaceSetSleepTimeThreshold(fa66a0a48611083e).(space, 0.5f); |
| 69 | |
| 70 | cpBodycxx . . $ cpBody# *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 71 | cpShapecxx . . $ cpShape# *shape; |
| 72 | |
| 73 | // Create segments around the edge of the screen. |
| 74 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), 0.0f)); |
| 75 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 76 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 77 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 78 | |
| 79 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f)); |
| 80 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 81 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 82 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 83 | |
| 84 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), 0.0f)); |
| 85 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 86 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 87 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 88 | |
| 89 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,240), cpvcxx . . $ cpv(3142d57d978b2176).(320,240), 0.0f)); |
| 90 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 91 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 92 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 93 | |
| 94 | for(int i=0; i<50; i++){ |
| 95 | cpBodycxx . . $ cpBody# *body = add_boxcxx . . $ add_box(2c787cd1c450712c).(space, 20, 1); |
| 96 | |
| 97 | cpConstraintcxx . . $ cpConstraint# *pivot = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpPivotJointNew2cxx . . $ cpPivotJointNew2(74f6d8fa0e302020).(staticBody, body, cpvzerocxx . . $ cpvzero., cpvzerocxx . . $ cpvzero.)); |
| 98 | cpConstraintSetMaxBiascxx . . $ cpConstraintSetMaxBias(dfce55250ba9e4fa).(pivot, 0); // disable joint correction |
| 99 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(pivot, 1000.0f); // emulate linear friction |
| 100 | |
| 101 | cpConstraintcxx . . $ cpConstraint# *gear = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpGearJointNewcxx . . $ cpGearJointNew(d029de5d93e4cb4a).(staticBody, body, 0.0f, 1.0f)); |
| 102 | cpConstraintSetMaxBiascxx . . $ cpConstraintSetMaxBias(dfce55250ba9e4fa).(gear, 0); // disable joint correction |
| 103 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(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 | tankControlBodycxx . . $ tankControlBody. = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewKinematiccxx . . $ cpBodyNewKinematic(af6db6e743345a49).()); |
| 108 | tankBodycxx . . $ tankBody. = add_boxcxx . . $ add_box(2c787cd1c450712c).(space, 30, 10); |
| 109 | |
| 110 | cpConstraintcxx . . $ cpConstraint# *pivot = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpPivotJointNew2cxx . . $ cpPivotJointNew2(74f6d8fa0e302020).(tankControlBodycxx . . $ tankControlBody., tankBodycxx . . $ tankBody., cpvzerocxx . . $ cpvzero., cpvzerocxx . . $ cpvzero.)); |
| 111 | cpConstraintSetMaxBiascxx . . $ cpConstraintSetMaxBias(dfce55250ba9e4fa).(pivot, 0); // disable joint correction |
| 112 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(pivot, 10000.0f); // emulate linear friction |
| 113 | |
| 114 | cpConstraintcxx . . $ cpConstraint# *gear = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpGearJointNewcxx . . $ cpGearJointNew(d029de5d93e4cb4a).(tankControlBodycxx . . $ tankControlBody., tankBodycxx . . $ tankBody., 0.0f, 1.0f)); |
| 115 | cpConstraintSetErrorBiascxx . . $ cpConstraintSetErrorBias(dfce55250ba9e4fa).(gear, 0); // attempt to fully correct the joint each step |
| 116 | cpConstraintSetMaxBiascxx . . $ cpConstraintSetMaxBias(dfce55250ba9e4fa).(gear, 1.2f); // but limit it's angular correction rate |
| 117 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(gear, 50000.0f); // emulate angular friction |
| 118 | |
| 119 | return space; |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 124 | { |
| 125 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 126 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 127 | } |
| 128 | |
| 129 | ChipmunkDemocxx . . $ ChipmunkDemo# Tankcxx . . $ Tank. = { |
| 130 | "Tank", |
| 131 | 1.0/60.0, |
| 132 | initcxx . . $ init(1f0082e822a7ce76)., |
| 133 | updatecxx . . $ update(fa66a0a48611083e)., |
| 134 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 135 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 136 | }; |