| 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# *dollyBodycxx . . $ dollyBody. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 26 | // Constraint used as a servo motor to move the dolly back and forth. |
| 27 | static cpConstraintcxx . . $ cpConstraint# *dollyServocxx . . $ dollyServo. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 28 | |
| 29 | // Constraint used as a winch motor to lift the load. |
| 30 | static cpConstraintcxx . . $ cpConstraint# *winchServocxx . . $ winchServo. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 31 | |
| 32 | // Temporary joint used to hold the hook to the load. |
| 33 | static cpConstraintcxx . . $ cpConstraint# *hookJointcxx . . $ hookJoint. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 34 | |
| 35 | |
| 36 | static void |
| 37 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 38 | { |
| 39 | // Set the first anchor point (the one attached to the static body) of the dolly servo to the mouse's x position. |
| 40 | cpPivotJointSetAnchorAcxx . . $ cpPivotJointSetAnchorA(dec37a28d0c61656).(dollyServocxx . . $ dollyServo., cpvcxx . . $ cpv(3142d57d978b2176).(ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse..xcxx . . $ cpVect#x., 100)); |
| 41 | |
| 42 | // Set the max length of the winch servo to match the mouse's height. |
| 43 | cpSlideJointSetMaxcxx . . $ cpSlideJointSetMax(dfce55250ba9e4fa).(winchServocxx . . $ winchServo., cpfmaxcxx . . $ cpfmax(d8e7e0733fa41c87).(100 - ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse..ycxx . . $ cpVect#y., 50)); |
| 44 | |
| 45 | if(hookJointcxx . . $ hookJoint. && ChipmunkDemoRightClickcxx . . $ ChipmunkDemoRightClick.){ |
| 46 | cpSpaceRemoveConstraintcxx . . $ cpSpaceRemoveConstraint(ab426c857162788d).(space, hookJointcxx . . $ hookJoint.); |
| 47 | cpConstraintFreecxx . . $ cpConstraintFree(ce362f6e378064c6).(hookJointcxx . . $ hookJoint.); |
| 48 | hookJointcxx . . $ hookJoint. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 49 | } |
| 50 | |
| 51 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 52 | } |
| 53 | |
| 54 | enum COLLISION_TYPEScxx . . $ COLLISION_TYPES# { |
| 55 | HOOK_SENSORcxx . . $ HOOK_SENSOR. = 1, |
| 56 | CRATEcxx . . $ CRATE., |
| 57 | }; |
| 58 | |
| 59 | static void |
| 60 | AttachHookcxx . . $ AttachHook(d3406af57bf9545e).(cpSpacecxx . . $ cpSpace# *space, cpBodycxx . . $ cpBody# *hook, cpBodycxx . . $ cpBody# *crate) |
| 61 | { |
| 62 | hookJointcxx . . $ hookJoint. = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpPivotJointNewcxx . . $ cpPivotJointNew(8cdc1c6d6b6f0abb).(hook, crate, cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(hook))); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static cpBoolcxx . . $ cpBool# |
| 67 | HookCratecxx . . $ HookCrate(46926bbb19d93a13).(cpArbitercxx . . $ cpArbiter# *arb, cpSpacecxx . . $ cpSpace# *space, void *data) |
| 68 | { |
| 69 | if(hookJointcxx . . $ hookJoint. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!){ |
| 70 | // Get pointers to the two bodies in the collision pair and define local variables for them. |
| 71 | // Their order matches the order of the collision types passed |
| 72 | // to the collision handler this function was defined for |
| 73 | CP_ARBITER_GET_BODIEScxx . . $ `include/chipmunk/cpArbiter.h:81:9`!(arb, hook, crate); |
| 74 | |
| 75 | // additions and removals can't be done in a normal callback. |
| 76 | // Schedule a post step callback to do it. |
| 77 | // Use the hook as the key and pass along the arbiter. |
| 78 | cpSpaceAddPostStepCallbackcxx . . $ cpSpaceAddPostStepCallback(1ff9997ad0bee464).(space, (cpPostStepFunccxx . . $ cpPostStepFunc#)AttachHookcxx . . $ AttachHook(d3406af57bf9545e)., hook, crate); |
| 79 | } |
| 80 | |
| 81 | return cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!; // return value is ignored for sensor callbacks anyway |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static cpSpacecxx . . $ cpSpace# * |
| 86 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 87 | { |
| 88 | ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = "Control the crane by moving the mouse. Right click to release."; |
| 89 | |
| 90 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 91 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 30); |
| 92 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -100)); |
| 93 | cpSpaceSetDampingcxx . . $ cpSpaceSetDamping(fa66a0a48611083e).(space, 0.8); |
| 94 | |
| 95 | cpBodycxx . . $ cpBody# *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 96 | cpShapecxx . . $ cpShape# *shape; |
| 97 | |
| 98 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-320,-240), cpvcxx . . $ cpv(3142d57d978b2176).(320,-240), 0.0f)); |
| 99 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 100 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 101 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 102 | |
| 103 | // Add a body for the dolly. |
| 104 | dollyBodycxx . . $ dollyBody. = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(10, INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!)); |
| 105 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(dollyBodycxx . . $ dollyBody., cpvcxx . . $ cpv(3142d57d978b2176).(0, 100)); |
| 106 | |
| 107 | // Add a block so you can see it. |
| 108 | cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(dollyBodycxx . . $ dollyBody., 30, 30, 0.0)); |
| 109 | |
| 110 | // Add a groove joint for it to move back and forth on. |
| 111 | cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpGrooveJointNewcxx . . $ cpGrooveJointNew(f2f94869a8417a6b).(staticBody, dollyBodycxx . . $ dollyBody., cpvcxx . . $ cpv(3142d57d978b2176).(-250, 100), cpvcxx . . $ cpv(3142d57d978b2176).(250, 100), cpvzerocxx . . $ cpvzero.)); |
| 112 | |
| 113 | // Add a pivot joint to act as a servo motor controlling it's position |
| 114 | // By updating the anchor points of the pivot joint, you can move the dolly. |
| 115 | dollyServocxx . . $ dollyServo. = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpPivotJointNewcxx . . $ cpPivotJointNew(8cdc1c6d6b6f0abb).(staticBody, dollyBodycxx . . $ dollyBody., cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(dollyBodycxx . . $ dollyBody.))); |
| 116 | // Max force the dolly servo can generate. |
| 117 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(dollyServocxx . . $ dollyServo., 10000); |
| 118 | // Max speed of the dolly servo |
| 119 | cpConstraintSetMaxBiascxx . . $ cpConstraintSetMaxBias(dfce55250ba9e4fa).(dollyServocxx . . $ dollyServo., 100); |
| 120 | // You can also change the error bias to control how it slows down. |
| 121 | //cpConstraintSetErrorBias(dollyServo, 0.2); |
| 122 | |
| 123 | |
| 124 | // Add the crane hook. |
| 125 | cpBodycxx . . $ cpBody# *hookBody = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(1, INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!)); |
| 126 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(hookBody, cpvcxx . . $ cpv(3142d57d978b2176).(0, 50)); |
| 127 | |
| 128 | // Add a sensor shape for it. This will be used to figure out when the hook touches a box. |
| 129 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpCircleShapeNewcxx . . $ cpCircleShapeNew(e5e4e3a52c89533b).(hookBody, 10, cpvzerocxx . . $ cpvzero.)); |
| 130 | cpShapeSetSensorcxx . . $ cpShapeSetSensor(eff4d15739771431).(shape, cpTruecxx . . $ `include/chipmunk/chipmunk_types.h:183:10`!); |
| 131 | cpShapeSetCollisionTypecxx . . $ cpShapeSetCollisionType(2dd430e3162ea2fe).(shape, HOOK_SENSORcxx . . $ HOOK_SENSOR.); |
| 132 | |
| 133 | // Add a slide joint to act as a winch motor |
| 134 | // By updating the max length of the joint you can make it pull up the load. |
| 135 | winchServocxx . . $ winchServo. = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpSlideJointNewcxx . . $ cpSlideJointNew(5b97552dd9e8a693).(dollyBodycxx . . $ dollyBody., hookBody, cpvzerocxx . . $ cpvzero., cpvzerocxx . . $ cpvzero., 0, INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!)); |
| 136 | // Max force the dolly servo can generate. |
| 137 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(winchServocxx . . $ winchServo., 30000); |
| 138 | // Max speed of the dolly servo |
| 139 | cpConstraintSetMaxBiascxx . . $ cpConstraintSetMaxBias(dfce55250ba9e4fa).(winchServocxx . . $ winchServo., 60); |
| 140 | |
| 141 | // TODO: cleanup |
| 142 | // Finally a box to play with |
| 143 | cpBodycxx . . $ cpBody# *boxBody = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(30, cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(30, 50, 50))); |
| 144 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(boxBody, cpvcxx . . $ cpv(3142d57d978b2176).(200, -200)); |
| 145 | |
| 146 | // Add a block so you can see it. |
| 147 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(boxBody, 50, 50, 0.0)); |
| 148 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.7); |
| 149 | cpShapeSetCollisionTypecxx . . $ cpShapeSetCollisionType(2dd430e3162ea2fe).(shape, CRATEcxx . . $ CRATE.); |
| 150 | |
| 151 | cpCollisionHandlercxx . . $ cpCollisionHandler# *handler = cpSpaceAddCollisionHandlercxx . . $ cpSpaceAddCollisionHandler(becf4587622cddc8).(space, HOOK_SENSORcxx . . $ HOOK_SENSOR., CRATEcxx . . $ CRATE.); |
| 152 | handler->beginFunccxx . . $ cpCollisionHandler#beginFunc. = (cpCollisionBeginFunccxx . . $ cpCollisionBeginFunc#)HookCratecxx . . $ HookCrate(46926bbb19d93a13).; |
| 153 | |
| 154 | |
| 155 | return space; |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 160 | { |
| 161 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 162 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 163 | } |
| 164 | |
| 165 | ChipmunkDemocxx . . $ ChipmunkDemo# Cranecxx . . $ Crane. = { |
| 166 | "Crane", |
| 167 | 1.0/60.0, |
| 168 | initcxx . . $ init(1f0082e822a7ce76)., |
| 169 | updatecxx . . $ update(fa66a0a48611083e)., |
| 170 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 171 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 172 | }; |