| 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 | static cpBody *balance_body; |
| 26 | static cpFloat balance_sin = 0.0; |
| 27 | //static cpFloat last_v = 0.0; |
| 28 | |
| 29 | static cpBody *wheel_body; |
| 30 | static cpConstraint *motor; |
| 31 | |
| 32 | /* |
| 33 | TODO: |
| 34 | - Clamp max angle dynamically based on output torque. |
| 35 | */ |
| 36 | |
| 37 | |
| 38 | static inline cpFloat |
| 39 | bias_coef(cpFloat errorBias, cpFloat dt) |
| 40 | { |
| 41 | return 1.0f - cpfpow(errorBias, dt); |
| 42 | } |
| 43 | |
| 44 | static void motor_preSolve(cpConstraint *motor, cpSpace *space) |
| 45 | { |
| 46 | cpFloat dt = cpSpaceGetCurrentTimeStep(space); |
| 47 | |
| 48 | cpFloat target_x = ChipmunkDemoMouse.x; |
| 49 | ChipmunkDebugDrawSegment(cpv(target_x, -1000.0), cpv(target_x, 1000.0), RGBAColor(1.0, 0.0, 0.0, 1.0)); |
| 50 | |
| 51 | cpFloat max_v = 500.0; |
| 52 | cpFloat target_v = cpfclamp(bias_coef(0.5, dt/1.2)*(target_x - cpBodyGetPosition(balance_body).x)/dt, -max_v, max_v); |
| 53 | cpFloat error_v = (target_v - cpBodyGetVelocity(balance_body).x); |
| 54 | cpFloat target_sin = 3.0e-3*bias_coef(0.1, dt)*error_v/dt; |
| 55 | |
| 56 | cpFloat max_sin = cpfsin(0.6); |
| 57 | balance_sin = cpfclamp(balance_sin - 6.0e-5*bias_coef(0.2, dt)*error_v/dt, -max_sin, max_sin); |
| 58 | cpFloat target_a = asin(cpfclamp(-target_sin + balance_sin, -max_sin, max_sin)); |
| 59 | cpFloat angular_diff = asin(cpvcross(cpBodyGetRotation(balance_body), cpvforangle(target_a))); |
| 60 | cpFloat target_w = bias_coef(0.1, dt/0.4)*(angular_diff)/dt; |
| 61 | |
| 62 | cpFloat max_rate = 50.0; |
| 63 | cpFloat rate = cpfclamp(cpBodyGetAngularVelocity(wheel_body) + cpBodyGetAngularVelocity(balance_body) - target_w, -max_rate, max_rate); |
| 64 | cpSimpleMotorSetRate(motor, cpfclamp(rate, -max_rate, max_rate)); |
| 65 | cpConstraintSetMaxForce(motor, 8.0e4); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static void |
| 70 | update(cpSpace *space, double dt) |
| 71 | { |
| 72 | cpSpaceStep(space, dt); |
| 73 | } |
| 74 | |
| 75 | static cpSpace * |
| 76 | init(void) |
| 77 | { |
| 78 | ChipmunkDemoMessageString = "This unicycle is completely driven and balanced by a single cpSimpleMotor.\nMove the mouse to make the unicycle follow it."; |
| 79 | |
| 80 | cpSpace *space = cpSpaceNew(); |
| 81 | cpSpaceSetIterations(space, 30); |
| 82 | cpSpaceSetGravity(space, cpv(0, -500)); |
| 83 | |
| 84 | { |
| 85 | cpShape *shape = NULL; |
| 86 | cpBody *staticBody = cpSpaceGetStaticBody(space); |
| 87 | |
| 88 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-3200,-240), cpv(3200,-240), 0.0f)); |
| 89 | cpShapeSetElasticity(shape, 1.0f); |
| 90 | cpShapeSetFriction(shape, 1.0f); |
| 91 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 92 | |
| 93 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(0,-200), cpv(240,-240), 0.0f)); |
| 94 | cpShapeSetElasticity(shape, 1.0f); |
| 95 | cpShapeSetFriction(shape, 1.0f); |
| 96 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 97 | |
| 98 | shape = cpSpaceAddShape(space, cpSegmentShapeNew(staticBody, cpv(-240,-240), cpv(0,-200), 0.0f)); |
| 99 | cpShapeSetElasticity(shape, 1.0f); |
| 100 | cpShapeSetFriction(shape, 1.0f); |
| 101 | cpShapeSetFilter(shape, NOT_GRABBABLE_FILTER); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | { |
| 106 | cpFloat radius = 20.0; |
| 107 | cpFloat mass = 1.0; |
| 108 | |
| 109 | cpFloat moment = cpMomentForCircle(mass, 0.0, radius, cpvzero); |
| 110 | wheel_body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); |
| 111 | cpBodySetPosition(wheel_body, cpv(0.0, -160.0 + radius)); |
| 112 | |
| 113 | cpShape *shape = cpSpaceAddShape(space, cpCircleShapeNew(wheel_body, radius, cpvzero)); |
| 114 | cpShapeSetFriction(shape, 0.7); |
| 115 | cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); |
| 116 | } |
| 117 | |
| 118 | { |
| 119 | cpFloat cog_offset = 30.0; |
| 120 | |
| 121 | cpBB bb1 = cpBBNew(-5.0, 0.0 - cog_offset, 5.0, cog_offset*1.2 - cog_offset); |
| 122 | cpBB bb2 = cpBBNew(-25.0, bb1.t, 25.0, bb1.t + 10.0); |
| 123 | |
| 124 | cpFloat mass = 3.0; |
| 125 | cpFloat moment = cpMomentForBox2(mass, bb1) + cpMomentForBox2(mass, bb2); |
| 126 | |
| 127 | balance_body = cpSpaceAddBody(space, cpBodyNew(mass, moment)); |
| 128 | cpBodySetPosition(balance_body, cpv(0.0, cpBodyGetPosition(wheel_body).y + cog_offset)); |
| 129 | |
| 130 | cpShape *shape = NULL; |
| 131 | |
| 132 | shape = cpSpaceAddShape(space, cpBoxShapeNew2(balance_body, bb1, 0.0)); |
| 133 | cpShapeSetFriction(shape, 1.0); |
| 134 | cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); |
| 135 | |
| 136 | shape = cpSpaceAddShape(space, cpBoxShapeNew2(balance_body, bb2, 0.0)); |
| 137 | cpShapeSetFriction(shape, 1.0); |
| 138 | cpShapeSetFilter(shape, cpShapeFilterNew(1, CP_ALL_CATEGORIES, CP_ALL_CATEGORIES)); |
| 139 | } |
| 140 | |
| 141 | cpVect anchorA = cpBodyWorldToLocal(balance_body, cpBodyGetPosition(wheel_body)); |
| 142 | cpVect groove_a = cpvadd(anchorA, cpv(0.0, 30.0)); |
| 143 | cpVect groove_b = cpvadd(anchorA, cpv(0.0, -10.0)); |
| 144 | cpSpaceAddConstraint(space, cpGrooveJointNew(balance_body, wheel_body, groove_a, groove_b, cpvzero)); |
| 145 | cpSpaceAddConstraint(space, cpDampedSpringNew(balance_body, wheel_body, anchorA, cpvzero, 0.0, 6.0e2, 30.0)); |
| 146 | |
| 147 | motor = cpSpaceAddConstraint(space, cpSimpleMotorNew(wheel_body, balance_body, 0.0)); |
| 148 | cpConstraintSetPreSolveFunc(motor, motor_preSolve); |
| 149 | |
| 150 | { |
| 151 | cpFloat width = 100.0; |
| 152 | cpFloat height = 20.0; |
| 153 | cpFloat mass = 3.0; |
| 154 | |
| 155 | cpBody *boxBody = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForBox(mass, width, height))); |
| 156 | cpBodySetPosition(boxBody, cpv(200, -100)); |
| 157 | |
| 158 | cpShape *shape = cpSpaceAddShape(space, cpBoxShapeNew(boxBody, width, height, 0.0)); |
| 159 | cpShapeSetFriction(shape, 0.7); |
| 160 | } |
| 161 | |
| 162 | return space; |
| 163 | } |
| 164 | |
| 165 | static void |
| 166 | destroy(cpSpace *space) |
| 167 | { |
| 168 | ChipmunkDemoFreeSpaceChildren(space); |
| 169 | cpSpaceFree(space); |
| 170 | } |
| 171 | |
| 172 | ChipmunkDemo Unicycle = { |
| 173 | "Unicycle", |
| 174 | 1.0/60.0, |
| 175 | init, |
| 176 | update, |
| 177 | ChipmunkDemoDefaultDrawImpl, |
| 178 | destroy, |
| 179 | }; |