| 1 | /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software |
| 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_private.h"cxx . . $ `<file>/include/chipmunk/chipmunk_private.h`/ |
| 23 | |
| 24 | static cpFloatcxx . . $ cpFloat# |
| 25 | defaultSpringTorquecxx . . $ defaultSpringTorque(d9744db83e731063).(cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *spring, cpFloatcxx . . $ cpFloat# relativeAngle){ |
| 26 | return (relativeAngle - spring->restAnglecxx . . $ cpDampedRotarySpring#restAngle.)*spring->stiffnesscxx . . $ cpDampedRotarySpring#stiffness.; |
| 27 | } |
| 28 | |
| 29 | static void |
| 30 | preStepcxx . . $ preStep(95b3f21233473a9a).(cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *spring, cpFloatcxx . . $ cpFloat# dt) |
| 31 | { |
| 32 | cpBodycxx . . $ cpBody# *a = spring->constraintcxx . . $ cpDampedRotarySpring#constraint..acxx . . $ cpConstraint#a.; |
| 33 | cpBodycxx . . $ cpBody# *b = spring->constraintcxx . . $ cpDampedRotarySpring#constraint..bcxx . . $ cpConstraint#b.; |
| 34 | |
| 35 | cpFloatcxx . . $ cpFloat# moment = a->i_invcxx . . $ cpBody#i_inv. + b->i_invcxx . . $ cpBody#i_inv.; |
| 36 | cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(moment != 0.0, "Unsolvable spring."); |
| 37 | spring->iSumcxx . . $ cpDampedRotarySpring#iSum. = 1.0f/moment; |
| 38 | |
| 39 | spring->w_coefcxx . . $ cpDampedRotarySpring#w_coef. = 1.0f - cpfexpcxx . . $ `include/chipmunk/chipmunk_types.h:74:10`!(-spring->dampingcxx . . $ cpDampedRotarySpring#damping.*dt*moment); |
| 40 | spring->target_wrncxx . . $ cpDampedRotarySpring#target_wrn. = 0.0f; |
| 41 | |
| 42 | // apply spring torque |
| 43 | cpFloatcxx . . $ cpFloat# j_spring = spring->springTorqueFunccxx . . $ cpDampedRotarySpring#springTorqueFunc.((cpConstraintcxx . . $ cpConstraint# *)spring, a->acxx . . $ cpBody#a. - b->acxx . . $ cpBody#a.)*dt; |
| 44 | spring->jAcccxx . . $ cpDampedRotarySpring#jAcc. = j_spring; |
| 45 | |
| 46 | a->wcxx . . $ cpBody#w. -= j_spring*a->i_invcxx . . $ cpBody#i_inv.; |
| 47 | b->wcxx . . $ cpBody#w. += j_spring*b->i_invcxx . . $ cpBody#i_inv.; |
| 48 | } |
| 49 | |
| 50 | static void applyCachedImpulsecxx . . $ applyCachedImpulse(95b3f21233473a9a).(cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *spring, cpFloatcxx . . $ cpFloat# dt_coef){} |
| 51 | |
| 52 | static void |
| 53 | applyImpulsecxx . . $ applyImpulse(95b3f21233473a9a).(cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *spring, cpFloatcxx . . $ cpFloat# dt) |
| 54 | { |
| 55 | cpBodycxx . . $ cpBody# *a = spring->constraintcxx . . $ cpDampedRotarySpring#constraint..acxx . . $ cpConstraint#a.; |
| 56 | cpBodycxx . . $ cpBody# *b = spring->constraintcxx . . $ cpDampedRotarySpring#constraint..bcxx . . $ cpConstraint#b.; |
| 57 | |
| 58 | // compute relative velocity |
| 59 | cpFloatcxx . . $ cpFloat# wrn = a->wcxx . . $ cpBody#w. - b->wcxx . . $ cpBody#w.;//normal_relative_velocity(a, b, r1, r2, n) - spring->target_vrn; |
| 60 | |
| 61 | // compute velocity loss from drag |
| 62 | // not 100% certain this is derived correctly, though it makes sense |
| 63 | cpFloatcxx . . $ cpFloat# w_damp = (spring->target_wrncxx . . $ cpDampedRotarySpring#target_wrn. - wrn)*spring->w_coefcxx . . $ cpDampedRotarySpring#w_coef.; |
| 64 | spring->target_wrncxx . . $ cpDampedRotarySpring#target_wrn. = wrn + w_damp; |
| 65 | |
| 66 | //apply_impulses(a, b, spring->r1, spring->r2, cpvmult(spring->n, v_damp*spring->nMass)); |
| 67 | cpFloatcxx . . $ cpFloat# j_damp = w_damp*spring->iSumcxx . . $ cpDampedRotarySpring#iSum.; |
| 68 | spring->jAcccxx . . $ cpDampedRotarySpring#jAcc. += j_damp; |
| 69 | |
| 70 | a->wcxx . . $ cpBody#w. += j_damp*a->i_invcxx . . $ cpBody#i_inv.; |
| 71 | b->wcxx . . $ cpBody#w. -= j_damp*b->i_invcxx . . $ cpBody#i_inv.; |
| 72 | } |
| 73 | |
| 74 | static cpFloatcxx . . $ cpFloat# |
| 75 | getImpulsecxx . . $ getImpulse(aac05b6e0bc7fc18).(cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *spring) |
| 76 | { |
| 77 | return spring->jAcccxx . . $ cpDampedRotarySpring#jAcc.; |
| 78 | } |
| 79 | |
| 80 | static const cpConstraintClasscxx . . $ cpConstraintClass# klasscxx . . $ klass. = { |
| 81 | (cpConstraintPreStepImplcxx . . $ cpConstraintPreStepImpl#)preStepcxx . . $ preStep(95b3f21233473a9a)., |
| 82 | (cpConstraintApplyCachedImpulseImplcxx . . $ cpConstraintApplyCachedImpulseImpl#)applyCachedImpulsecxx . . $ applyCachedImpulse(95b3f21233473a9a)., |
| 83 | (cpConstraintApplyImpulseImplcxx . . $ cpConstraintApplyImpulseImpl#)applyImpulsecxx . . $ applyImpulse(95b3f21233473a9a)., |
| 84 | (cpConstraintGetImpulseImplcxx . . $ cpConstraintGetImpulseImpl#)getImpulsecxx . . $ getImpulse(aac05b6e0bc7fc18)., |
| 85 | }; |
| 86 | |
| 87 | cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# * |
| 88 | cpDampedRotarySpringAlloccxx . . $ cpDampedRotarySpringAlloc(da2155a9dd8f16bc).(void) |
| 89 | { |
| 90 | return (cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpDampedRotarySpringcxx . . $ cpDampedRotarySpring#)); |
| 91 | } |
| 92 | |
| 93 | cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# * |
| 94 | cpDampedRotarySpringInitcxx . . $ cpDampedRotarySpringInit(7a1c675fa4a38436).(cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *spring, cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpFloatcxx . . $ cpFloat# restAngle, cpFloatcxx . . $ cpFloat# stiffness, cpFloatcxx . . $ cpFloat# damping) |
| 95 | { |
| 96 | cpConstraintInitcxx . . $ cpConstraintInit(540fb6eaa94f170).((cpConstraintcxx . . $ cpConstraint# *)spring, &klasscxx . . $ klass., a, b); |
| 97 | |
| 98 | spring->restAnglecxx . . $ cpDampedRotarySpring#restAngle. = restAngle; |
| 99 | spring->stiffnesscxx . . $ cpDampedRotarySpring#stiffness. = stiffness; |
| 100 | spring->dampingcxx . . $ cpDampedRotarySpring#damping. = damping; |
| 101 | spring->springTorqueFunccxx . . $ cpDampedRotarySpring#springTorqueFunc. = (cpDampedRotarySpringTorqueFunccxx . . $ cpDampedRotarySpringTorqueFunc#)defaultSpringTorquecxx . . $ defaultSpringTorque(d9744db83e731063).; |
| 102 | |
| 103 | spring->jAcccxx . . $ cpDampedRotarySpring#jAcc. = 0.0f; |
| 104 | |
| 105 | return spring; |
| 106 | } |
| 107 | |
| 108 | cpConstraintcxx . . $ cpConstraint# * |
| 109 | cpDampedRotarySpringNewcxx . . $ cpDampedRotarySpringNew(6e76adfc32a0e240).(cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpFloatcxx . . $ cpFloat# restAngle, cpFloatcxx . . $ cpFloat# stiffness, cpFloatcxx . . $ cpFloat# damping) |
| 110 | { |
| 111 | return (cpConstraintcxx . . $ cpConstraint# *)cpDampedRotarySpringInitcxx . . $ cpDampedRotarySpringInit(7a1c675fa4a38436).(cpDampedRotarySpringAlloccxx . . $ cpDampedRotarySpringAlloc(da2155a9dd8f16bc).(), a, b, restAngle, stiffness, damping); |
| 112 | } |
| 113 | |
| 114 | cpBoolcxx . . $ cpBool# |
| 115 | cpConstraintIsDampedRotarySpringcxx . . $ cpConstraintIsDampedRotarySpring(ca4e56e976f68364).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 116 | { |
| 117 | return (constraint->klasscxx . . $ cpConstraint#klass. == &klasscxx . . $ klass.); |
| 118 | } |
| 119 | |
| 120 | cpFloatcxx . . $ cpFloat# |
| 121 | cpDampedRotarySpringGetRestAnglecxx . . $ cpDampedRotarySpringGetRestAngle(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 122 | { |
| 123 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedRotarySpring(constraint), "Constraint is not a damped rotary spring."); |
| 124 | return ((cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)constraint)->restAnglecxx . . $ cpDampedRotarySpring#restAngle.; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | cpDampedRotarySpringSetRestAnglecxx . . $ cpDampedRotarySpringSetRestAngle(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# restAngle) |
| 129 | { |
| 130 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedRotarySpring(constraint), "Constraint is not a damped rotary spring."); |
| 131 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 132 | ((cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)constraint)->restAnglecxx . . $ cpDampedRotarySpring#restAngle. = restAngle; |
| 133 | } |
| 134 | |
| 135 | cpFloatcxx . . $ cpFloat# |
| 136 | cpDampedRotarySpringGetStiffnesscxx . . $ cpDampedRotarySpringGetStiffness(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 137 | { |
| 138 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedRotarySpring(constraint), "Constraint is not a damped rotary spring."); |
| 139 | return ((cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)constraint)->stiffnesscxx . . $ cpDampedRotarySpring#stiffness.; |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | cpDampedRotarySpringSetStiffnesscxx . . $ cpDampedRotarySpringSetStiffness(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# stiffness) |
| 144 | { |
| 145 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedRotarySpring(constraint), "Constraint is not a damped rotary spring."); |
| 146 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 147 | ((cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)constraint)->stiffnesscxx . . $ cpDampedRotarySpring#stiffness. = stiffness; |
| 148 | } |
| 149 | |
| 150 | cpFloatcxx . . $ cpFloat# |
| 151 | cpDampedRotarySpringGetDampingcxx . . $ cpDampedRotarySpringGetDamping(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 152 | { |
| 153 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedRotarySpring(constraint), "Constraint is not a damped rotary spring."); |
| 154 | return ((cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)constraint)->dampingcxx . . $ cpDampedRotarySpring#damping.; |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | cpDampedRotarySpringSetDampingcxx . . $ cpDampedRotarySpringSetDamping(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# damping) |
| 159 | { |
| 160 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedRotarySpring(constraint), "Constraint is not a damped rotary spring."); |
| 161 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 162 | ((cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)constraint)->dampingcxx . . $ cpDampedRotarySpring#damping. = damping; |
| 163 | } |
| 164 | |
| 165 | cpDampedRotarySpringTorqueFunccxx . . $ cpDampedRotarySpringTorqueFunc# |
| 166 | cpDampedRotarySpringGetSpringTorqueFunccxx . . $ cpDampedRotarySpringGetSpringTorqueFunc(ee677ac8b4e1cf75).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 167 | { |
| 168 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedRotarySpring(constraint), "Constraint is not a damped rotary spring."); |
| 169 | return ((cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)constraint)->springTorqueFunccxx . . $ cpDampedRotarySpring#springTorqueFunc.; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | cpDampedRotarySpringSetSpringTorqueFunccxx . . $ cpDampedRotarySpringSetSpringTorqueFunc(e3e72c040aa1b092).(cpConstraintcxx . . $ cpConstraint# *constraint, cpDampedRotarySpringTorqueFunccxx . . $ cpDampedRotarySpringTorqueFunc# springTorqueFunc) |
| 174 | { |
| 175 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedRotarySpring(constraint), "Constraint is not a damped rotary spring."); |
| 176 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 177 | ((cpDampedRotarySpringcxx . . $ cpDampedRotarySpring# *)constraint)->springTorqueFunccxx . . $ cpDampedRotarySpring#springTorqueFunc. = springTorqueFunc; |
| 178 | } |