| 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# *balance_bodycxx . . $ balance_body.; |
| 26 | static cpFloatcxx . . $ cpFloat# balance_sincxx . . $ balance_sin. = 0.0; |
| 27 | //static cpFloat last_v = 0.0; |
| 28 | |
| 29 | static cpBodycxx . . $ cpBody# *wheel_bodycxx . . $ wheel_body.; |
| 30 | static cpConstraintcxx . . $ cpConstraint# *motorcxx . . $ motor.; |
| 31 | |
| 32 | /* |
| 33 | TODO: |
| 34 | - Clamp max angle dynamically based on output torque. |
| 35 | */ |
| 36 | |
| 37 | |
| 38 | static inline cpFloatcxx . . $ cpFloat# |
| 39 | bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(cpFloatcxx . . $ cpFloat# errorBias, cpFloatcxx . . $ cpFloat# dt) |
| 40 | { |
| 41 | return 1.0f - cpfpowcxx . . $ `include/chipmunk/chipmunk_types.h:75:10`!(errorBias, dt); |
| 42 | } |
| 43 | |
| 44 | static void motor_preSolvecxx . . $ motor_preSolve(e6d8226ee831afeb).(cpConstraintcxx . . $ cpConstraint# *motor, cpSpacecxx . . $ cpSpace# *space) |
| 45 | { |
| 46 | cpFloatcxx . . $ cpFloat# dt = cpSpaceGetCurrentTimeStepcxx . . $ cpSpaceGetCurrentTimeStep(f2dd6279e77f9c6b).(space); |
| 47 | |
| 48 | cpFloatcxx . . $ cpFloat# target_x = ChipmunkDemoMousecxx . . $ ChipmunkDemoMouse..xcxx . . $ cpVect#x.; |
| 49 | ChipmunkDebugDrawSegmentcxx . . $ ChipmunkDebugDrawSegment(307cf2b0f0a496c2).(cpvcxx . . $ cpv(3142d57d978b2176).(target_x, -1000.0), cpvcxx . . $ cpv(3142d57d978b2176).(target_x, 1000.0), RGBAColorcxx . . $ RGBAColor(54765961e0537a31).(1.0, 0.0, 0.0, 1.0)); |
| 50 | |
| 51 | cpFloatcxx . . $ cpFloat# max_v = 500.0; |
| 52 | cpFloatcxx . . $ cpFloat# target_v = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(0.5, dt/1.2)*(target_x - cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(balance_bodycxx . . $ balance_body.).xcxx . . $ cpVect#x.)/dt, -max_v, max_v); |
| 53 | cpFloatcxx . . $ cpFloat# error_v = (target_v - cpBodyGetVelocitycxx . . $ cpBodyGetVelocity(2e8d436bcfa5d74b).(balance_bodycxx . . $ balance_body.).xcxx . . $ cpVect#x.); |
| 54 | cpFloatcxx . . $ cpFloat# target_sin = 3.0e-3*bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(0.1, dt)*error_v/dt; |
| 55 | |
| 56 | cpFloatcxx . . $ cpFloat# max_sin = cpfsincxx . . $ `include/chipmunk/chipmunk_types.h:69:10`!(0.6); |
| 57 | balance_sincxx . . $ balance_sin. = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(balance_sincxx . . $ balance_sin. - 6.0e-5*bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(0.2, dt)*error_v/dt, -max_sin, max_sin); |
| 58 | cpFloatcxx . . $ cpFloat# target_a = asin(cpfclampcxx . . $ cpfclamp(7678bba071383ab).(-target_sin + balance_sincxx . . $ balance_sin., -max_sin, max_sin)); |
| 59 | cpFloatcxx . . $ cpFloat# angular_diff = asin(cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(cpBodyGetRotationcxx . . $ cpBodyGetRotation(2e8d436bcfa5d74b).(balance_bodycxx . . $ balance_body.), cpvforanglecxx . . $ cpvforangle(8d5769cc69967cd4).(target_a))); |
| 60 | cpFloatcxx . . $ cpFloat# target_w = bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(0.1, dt/0.4)*(angular_diff)/dt; |
| 61 | |
| 62 | cpFloatcxx . . $ cpFloat# max_rate = 50.0; |
| 63 | cpFloatcxx . . $ cpFloat# rate = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(cpBodyGetAngularVelocitycxx . . $ cpBodyGetAngularVelocity(2880d7999e0b34c3).(wheel_bodycxx . . $ wheel_body.) + cpBodyGetAngularVelocitycxx . . $ cpBodyGetAngularVelocity(2880d7999e0b34c3).(balance_bodycxx . . $ balance_body.) - target_w, -max_rate, max_rate); |
| 64 | cpSimpleMotorSetRatecxx . . $ cpSimpleMotorSetRate(dfce55250ba9e4fa).(motor, cpfclampcxx . . $ cpfclamp(7678bba071383ab).(rate, -max_rate, max_rate)); |
| 65 | cpConstraintSetMaxForcecxx . . $ cpConstraintSetMaxForce(dfce55250ba9e4fa).(motor, 8.0e4); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static void |
| 70 | updatecxx . . $ update(fa66a0a48611083e).(cpSpacecxx . . $ cpSpace# *space, double dt) |
| 71 | { |
| 72 | cpSpaceStepcxx . . $ cpSpaceStep(fa66a0a48611083e).(space, dt); |
| 73 | } |
| 74 | |
| 75 | static cpSpacecxx . . $ cpSpace# * |
| 76 | initcxx . . $ init(1f0082e822a7ce76).(void) |
| 77 | { |
| 78 | ChipmunkDemoMessageStringcxx . . $ ChipmunkDemoMessageString. = "This unicycle is completely driven and balanced by a single cpSimpleMotor.\nMove the mouse to make the unicycle follow it."; |
| 79 | |
| 80 | cpSpacecxx . . $ cpSpace# *space = cpSpaceNewcxx . . $ cpSpaceNew(1f0082e822a7ce76).(); |
| 81 | cpSpaceSetIterationscxx . . $ cpSpaceSetIterations(3274f250b6e81e27).(space, 30); |
| 82 | cpSpaceSetGravitycxx . . $ cpSpaceSetGravity(7561685522e3e327).(space, cpvcxx . . $ cpv(3142d57d978b2176).(0, -500)); |
| 83 | |
| 84 | { |
| 85 | cpShapecxx . . $ cpShape# *shape = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 86 | cpBodycxx . . $ cpBody# *staticBody = cpSpaceGetStaticBodycxx . . $ cpSpaceGetStaticBody(d6ad68aaf406e51f).(space); |
| 87 | |
| 88 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-3200,-240), cpvcxx . . $ cpv(3142d57d978b2176).(3200,-240), 0.0f)); |
| 89 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 90 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 91 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 92 | |
| 93 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(0,-200), cpvcxx . . $ cpv(3142d57d978b2176).(240,-240), 0.0f)); |
| 94 | cpShapeSetElasticitycxx . . $ cpShapeSetElasticity(2eca2465ee202d41).(shape, 1.0f); |
| 95 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0f); |
| 96 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, NOT_GRABBABLE_FILTERcxx . . $ NOT_GRABBABLE_FILTER.); |
| 97 | |
| 98 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpSegmentShapeNewcxx . . $ cpSegmentShapeNew(3a41acb06b39b945).(staticBody, cpvcxx . . $ cpv(3142d57d978b2176).(-240,-240), cpvcxx . . $ cpv(3142d57d978b2176).(0,-200), 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 | |
| 104 | |
| 105 | { |
| 106 | cpFloatcxx . . $ cpFloat# radius = 20.0; |
| 107 | cpFloatcxx . . $ cpFloat# mass = 1.0; |
| 108 | |
| 109 | cpFloatcxx . . $ cpFloat# moment = cpMomentForCirclecxx . . $ cpMomentForCircle(a539e2d95614f770).(mass, 0.0, radius, cpvzerocxx . . $ cpvzero.); |
| 110 | wheel_bodycxx . . $ wheel_body. = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment)); |
| 111 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(wheel_bodycxx . . $ wheel_body., cpvcxx . . $ cpv(3142d57d978b2176).(0.0, -160.0 + radius)); |
| 112 | |
| 113 | cpShapecxx . . $ cpShape# *shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpCircleShapeNewcxx . . $ cpCircleShapeNew(e5e4e3a52c89533b).(wheel_bodycxx . . $ wheel_body., radius, cpvzerocxx . . $ cpvzero.)); |
| 114 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.7); |
| 115 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, cpShapeFilterNewcxx . . $ cpShapeFilterNew(d4cd326b51c6d7a3).(1, CP_ALL_CATEGORIEScxx . . $ `include/chipmunk/chipmunk_types.h:233:10`!, CP_ALL_CATEGORIEScxx . . $ `include/chipmunk/chipmunk_types.h:233:10`!)); |
| 116 | } |
| 117 | |
| 118 | { |
| 119 | cpFloatcxx . . $ cpFloat# cog_offset = 30.0; |
| 120 | |
| 121 | cpBBcxx . . $ cpBB# bb1 = cpBBNewcxx . . $ cpBBNew(f05aa3203068e6d3).(-5.0, 0.0 - cog_offset, 5.0, cog_offset*1.2 - cog_offset); |
| 122 | cpBBcxx . . $ cpBB# bb2 = cpBBNewcxx . . $ cpBBNew(f05aa3203068e6d3).(-25.0, bb1.tcxx . . $ cpBB#t., 25.0, bb1.tcxx . . $ cpBB#t. + 10.0); |
| 123 | |
| 124 | cpFloatcxx . . $ cpFloat# mass = 3.0; |
| 125 | cpFloatcxx . . $ cpFloat# moment = cpMomentForBox2cxx . . $ cpMomentForBox2(6725099f9bd51722).(mass, bb1) + cpMomentForBox2cxx . . $ cpMomentForBox2(6725099f9bd51722).(mass, bb2); |
| 126 | |
| 127 | balance_bodycxx . . $ balance_body. = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, moment)); |
| 128 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(balance_bodycxx . . $ balance_body., cpvcxx . . $ cpv(3142d57d978b2176).(0.0, cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(wheel_bodycxx . . $ wheel_body.).ycxx . . $ cpVect#y. + cog_offset)); |
| 129 | |
| 130 | cpShapecxx . . $ cpShape# *shape = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 131 | |
| 132 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNew2cxx . . $ cpBoxShapeNew2(ac0258d00568be32).(balance_bodycxx . . $ balance_body., bb1, 0.0)); |
| 133 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0); |
| 134 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, cpShapeFilterNewcxx . . $ cpShapeFilterNew(d4cd326b51c6d7a3).(1, CP_ALL_CATEGORIEScxx . . $ `include/chipmunk/chipmunk_types.h:233:10`!, CP_ALL_CATEGORIEScxx . . $ `include/chipmunk/chipmunk_types.h:233:10`!)); |
| 135 | |
| 136 | shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNew2cxx . . $ cpBoxShapeNew2(ac0258d00568be32).(balance_bodycxx . . $ balance_body., bb2, 0.0)); |
| 137 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 1.0); |
| 138 | cpShapeSetFiltercxx . . $ cpShapeSetFilter(ec7e03c52efe89d5).(shape, cpShapeFilterNewcxx . . $ cpShapeFilterNew(d4cd326b51c6d7a3).(1, CP_ALL_CATEGORIEScxx . . $ `include/chipmunk/chipmunk_types.h:233:10`!, CP_ALL_CATEGORIEScxx . . $ `include/chipmunk/chipmunk_types.h:233:10`!)); |
| 139 | } |
| 140 | |
| 141 | cpVectcxx . . $ cpVect# anchorA = cpBodyWorldToLocalcxx . . $ cpBodyWorldToLocal(4ff4900d49a97e73).(balance_bodycxx . . $ balance_body., cpBodyGetPositioncxx . . $ cpBodyGetPosition(2e8d436bcfa5d74b).(wheel_bodycxx . . $ wheel_body.)); |
| 142 | cpVectcxx . . $ cpVect# groove_a = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(anchorA, cpvcxx . . $ cpv(3142d57d978b2176).(0.0, 30.0)); |
| 143 | cpVectcxx . . $ cpVect# groove_b = cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(anchorA, cpvcxx . . $ cpv(3142d57d978b2176).(0.0, -10.0)); |
| 144 | cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpGrooveJointNewcxx . . $ cpGrooveJointNew(f2f94869a8417a6b).(balance_bodycxx . . $ balance_body., wheel_bodycxx . . $ wheel_body., groove_a, groove_b, cpvzerocxx . . $ cpvzero.)); |
| 145 | cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpDampedSpringNewcxx . . $ cpDampedSpringNew(4558d7d073b8c5fd).(balance_bodycxx . . $ balance_body., wheel_bodycxx . . $ wheel_body., anchorA, cpvzerocxx . . $ cpvzero., 0.0, 6.0e2, 30.0)); |
| 146 | |
| 147 | motorcxx . . $ motor. = cpSpaceAddConstraintcxx . . $ cpSpaceAddConstraint(9bcc8bada2b14254).(space, cpSimpleMotorNewcxx . . $ cpSimpleMotorNew(fc786efe5abab4f7).(wheel_bodycxx . . $ wheel_body., balance_bodycxx . . $ balance_body., 0.0)); |
| 148 | cpConstraintSetPreSolveFunccxx . . $ cpConstraintSetPreSolveFunc(4f57467f17129581).(motorcxx . . $ motor., motor_preSolvecxx . . $ motor_preSolve(e6d8226ee831afeb).); |
| 149 | |
| 150 | { |
| 151 | cpFloatcxx . . $ cpFloat# width = 100.0; |
| 152 | cpFloatcxx . . $ cpFloat# height = 20.0; |
| 153 | cpFloatcxx . . $ cpFloat# mass = 3.0; |
| 154 | |
| 155 | cpBodycxx . . $ cpBody# *boxBody = cpSpaceAddBodycxx . . $ cpSpaceAddBody(1a8396fd971b33e7).(space, cpBodyNewcxx . . $ cpBodyNew(51915e6cc8310751).(mass, cpMomentForBoxcxx . . $ cpMomentForBox(7678bba071383ab).(mass, width, height))); |
| 156 | cpBodySetPositioncxx . . $ cpBodySetPosition(864e17bcbe059b08).(boxBody, cpvcxx . . $ cpv(3142d57d978b2176).(200, -100)); |
| 157 | |
| 158 | cpShapecxx . . $ cpShape# *shape = cpSpaceAddShapecxx . . $ cpSpaceAddShape(a8063921f8ae51c0).(space, cpBoxShapeNewcxx . . $ cpBoxShapeNew(94c4f8982441e75a).(boxBody, width, height, 0.0)); |
| 159 | cpShapeSetFrictioncxx . . $ cpShapeSetFriction(2eca2465ee202d41).(shape, 0.7); |
| 160 | } |
| 161 | |
| 162 | return space; |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | destroycxx . . $ destroy(728b9c9a69722ef8).(cpSpacecxx . . $ cpSpace# *space) |
| 167 | { |
| 168 | ChipmunkDemoFreeSpaceChildrencxx . . $ ChipmunkDemoFreeSpaceChildren(728b9c9a69722ef8).(space); |
| 169 | cpSpaceFreecxx . . $ cpSpaceFree(728b9c9a69722ef8).(space); |
| 170 | } |
| 171 | |
| 172 | ChipmunkDemocxx . . $ ChipmunkDemo# Unicyclecxx . . $ Unicycle. = { |
| 173 | "Unicycle", |
| 174 | 1.0/60.0, |
| 175 | initcxx . . $ init(1f0082e822a7ce76)., |
| 176 | updatecxx . . $ update(fa66a0a48611083e)., |
| 177 | ChipmunkDemoDefaultDrawImplcxx . . $ ChipmunkDemoDefaultDrawImpl(728b9c9a69722ef8)., |
| 178 | destroycxx . . $ destroy(728b9c9a69722ef8)., |
| 179 | }; |