| 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 | defaultSpringForcecxx . . $ defaultSpringForce(1ec5306623d3a4dc).(cpDampedSpringcxx . . $ cpDampedSpring# *spring, cpFloatcxx . . $ cpFloat# dist){ |
| 26 | return (spring->restLengthcxx . . $ cpDampedSpring#restLength. - dist)*spring->stiffnesscxx . . $ cpDampedSpring#stiffness.; |
| 27 | } |
| 28 | |
| 29 | static void |
| 30 | preStepcxx . . $ preStep(c5fbbbc5405a7c6f).(cpDampedSpringcxx . . $ cpDampedSpring# *spring, cpFloatcxx . . $ cpFloat# dt) |
| 31 | { |
| 32 | cpBodycxx . . $ cpBody# *a = spring->constraintcxx . . $ cpDampedSpring#constraint..acxx . . $ cpConstraint#a.; |
| 33 | cpBodycxx . . $ cpBody# *b = spring->constraintcxx . . $ cpDampedSpring#constraint..bcxx . . $ cpConstraint#b.; |
| 34 | |
| 35 | spring->r1cxx . . $ cpDampedSpring#r1. = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(a->transformcxx . . $ cpBody#transform., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(spring->anchorAcxx . . $ cpDampedSpring#anchorA., a->cogcxx . . $ cpBody#cog.)); |
| 36 | spring->r2cxx . . $ cpDampedSpring#r2. = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(b->transformcxx . . $ cpBody#transform., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(spring->anchorBcxx . . $ cpDampedSpring#anchorB., b->cogcxx . . $ cpBody#cog.)); |
| 37 | |
| 38 | cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(b->pcxx . . $ cpBody#p., spring->r2cxx . . $ cpDampedSpring#r2.), cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(a->pcxx . . $ cpBody#p., spring->r1cxx . . $ cpDampedSpring#r1.)); |
| 39 | cpFloatcxx . . $ cpFloat# dist = cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(delta); |
| 40 | spring->ncxx . . $ cpDampedSpring#n. = cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(delta, 1.0f/(dist ? dist : INFINITYcxx . . $ `<external>/11263917310783833665/math.h:93:12`!)); |
| 41 | |
| 42 | cpFloatcxx . . $ cpFloat# k = k_scalarcxx . . $ k_scalar(d97a6b5999e6b615).(a, b, spring->r1cxx . . $ cpDampedSpring#r1., spring->r2cxx . . $ cpDampedSpring#r2., spring->ncxx . . $ cpDampedSpring#n.); |
| 43 | cpAssertSoftcxx . . $ `include/chipmunk/chipmunk.h:51:10`!(k != 0.0, "Unsolvable spring."); |
| 44 | spring->nMasscxx . . $ cpDampedSpring#nMass. = 1.0f/k; |
| 45 | |
| 46 | spring->target_vrncxx . . $ cpDampedSpring#target_vrn. = 0.0f; |
| 47 | spring->v_coefcxx . . $ cpDampedSpring#v_coef. = 1.0f - cpfexpcxx . . $ `include/chipmunk/chipmunk_types.h:74:10`!(-spring->dampingcxx . . $ cpDampedSpring#damping.*dt*k); |
| 48 | |
| 49 | // apply spring force |
| 50 | cpFloatcxx . . $ cpFloat# f_spring = spring->springForceFunccxx . . $ cpDampedSpring#springForceFunc.((cpConstraintcxx . . $ cpConstraint# *)spring, dist); |
| 51 | cpFloatcxx . . $ cpFloat# j_spring = spring->jAcccxx . . $ cpDampedSpring#jAcc. = f_spring*dt; |
| 52 | apply_impulsescxx . . $ apply_impulses(db940cdf68a6ac72).(a, b, spring->r1cxx . . $ cpDampedSpring#r1., spring->r2cxx . . $ cpDampedSpring#r2., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(spring->ncxx . . $ cpDampedSpring#n., j_spring)); |
| 53 | } |
| 54 | |
| 55 | static void applyCachedImpulsecxx . . $ applyCachedImpulse(c5fbbbc5405a7c6f).(cpDampedSpringcxx . . $ cpDampedSpring# *spring, cpFloatcxx . . $ cpFloat# dt_coef){} |
| 56 | |
| 57 | static void |
| 58 | applyImpulsecxx . . $ applyImpulse(c5fbbbc5405a7c6f).(cpDampedSpringcxx . . $ cpDampedSpring# *spring, cpFloatcxx . . $ cpFloat# dt) |
| 59 | { |
| 60 | cpBodycxx . . $ cpBody# *a = spring->constraintcxx . . $ cpDampedSpring#constraint..acxx . . $ cpConstraint#a.; |
| 61 | cpBodycxx . . $ cpBody# *b = spring->constraintcxx . . $ cpDampedSpring#constraint..bcxx . . $ cpConstraint#b.; |
| 62 | |
| 63 | cpVectcxx . . $ cpVect# n = spring->ncxx . . $ cpDampedSpring#n.; |
| 64 | cpVectcxx . . $ cpVect# r1 = spring->r1cxx . . $ cpDampedSpring#r1.; |
| 65 | cpVectcxx . . $ cpVect# r2 = spring->r2cxx . . $ cpDampedSpring#r2.; |
| 66 | |
| 67 | // compute relative velocity |
| 68 | cpFloatcxx . . $ cpFloat# vrn = normal_relative_velocitycxx . . $ normal_relative_velocity(d97a6b5999e6b615).(a, b, r1, r2, n); |
| 69 | |
| 70 | // compute velocity loss from drag |
| 71 | cpFloatcxx . . $ cpFloat# v_damp = (spring->target_vrncxx . . $ cpDampedSpring#target_vrn. - vrn)*spring->v_coefcxx . . $ cpDampedSpring#v_coef.; |
| 72 | spring->target_vrncxx . . $ cpDampedSpring#target_vrn. = vrn + v_damp; |
| 73 | |
| 74 | cpFloatcxx . . $ cpFloat# j_damp = v_damp*spring->nMasscxx . . $ cpDampedSpring#nMass.; |
| 75 | spring->jAcccxx . . $ cpDampedSpring#jAcc. += j_damp; |
| 76 | apply_impulsescxx . . $ apply_impulses(db940cdf68a6ac72).(a, b, spring->r1cxx . . $ cpDampedSpring#r1., spring->r2cxx . . $ cpDampedSpring#r2., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(spring->ncxx . . $ cpDampedSpring#n., j_damp)); |
| 77 | } |
| 78 | |
| 79 | static cpFloatcxx . . $ cpFloat# |
| 80 | getImpulsecxx . . $ getImpulse(6ba265ef4b4296ca).(cpDampedSpringcxx . . $ cpDampedSpring# *spring) |
| 81 | { |
| 82 | return spring->jAcccxx . . $ cpDampedSpring#jAcc.; |
| 83 | } |
| 84 | |
| 85 | static const cpConstraintClasscxx . . $ cpConstraintClass# klasscxx . . $ klass. = { |
| 86 | (cpConstraintPreStepImplcxx . . $ cpConstraintPreStepImpl#)preStepcxx . . $ preStep(c5fbbbc5405a7c6f)., |
| 87 | (cpConstraintApplyCachedImpulseImplcxx . . $ cpConstraintApplyCachedImpulseImpl#)applyCachedImpulsecxx . . $ applyCachedImpulse(c5fbbbc5405a7c6f)., |
| 88 | (cpConstraintApplyImpulseImplcxx . . $ cpConstraintApplyImpulseImpl#)applyImpulsecxx . . $ applyImpulse(c5fbbbc5405a7c6f)., |
| 89 | (cpConstraintGetImpulseImplcxx . . $ cpConstraintGetImpulseImpl#)getImpulsecxx . . $ getImpulse(6ba265ef4b4296ca)., |
| 90 | }; |
| 91 | |
| 92 | cpDampedSpringcxx . . $ cpDampedSpring# * |
| 93 | cpDampedSpringAlloccxx . . $ cpDampedSpringAlloc(36a8fb622e357825).(void) |
| 94 | { |
| 95 | return (cpDampedSpringcxx . . $ cpDampedSpring# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpDampedSpringcxx . . $ cpDampedSpring#)); |
| 96 | } |
| 97 | |
| 98 | cpDampedSpringcxx . . $ cpDampedSpring# * |
| 99 | cpDampedSpringInitcxx . . $ cpDampedSpringInit(d9a48c4637880a6b).(cpDampedSpringcxx . . $ cpDampedSpring# *spring, cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# anchorA, cpVectcxx . . $ cpVect# anchorB, cpFloatcxx . . $ cpFloat# restLength, cpFloatcxx . . $ cpFloat# stiffness, cpFloatcxx . . $ cpFloat# damping) |
| 100 | { |
| 101 | cpConstraintInitcxx . . $ cpConstraintInit(540fb6eaa94f170).((cpConstraintcxx . . $ cpConstraint# *)spring, &klasscxx . . $ klass., a, b); |
| 102 | |
| 103 | spring->anchorAcxx . . $ cpDampedSpring#anchorA. = anchorA; |
| 104 | spring->anchorBcxx . . $ cpDampedSpring#anchorB. = anchorB; |
| 105 | |
| 106 | spring->restLengthcxx . . $ cpDampedSpring#restLength. = restLength; |
| 107 | spring->stiffnesscxx . . $ cpDampedSpring#stiffness. = stiffness; |
| 108 | spring->dampingcxx . . $ cpDampedSpring#damping. = damping; |
| 109 | spring->springForceFunccxx . . $ cpDampedSpring#springForceFunc. = (cpDampedSpringForceFunccxx . . $ cpDampedSpringForceFunc#)defaultSpringForcecxx . . $ defaultSpringForce(1ec5306623d3a4dc).; |
| 110 | |
| 111 | spring->jAcccxx . . $ cpDampedSpring#jAcc. = 0.0f; |
| 112 | |
| 113 | return spring; |
| 114 | } |
| 115 | |
| 116 | cpConstraintcxx . . $ cpConstraint# * |
| 117 | cpDampedSpringNewcxx . . $ cpDampedSpringNew(4558d7d073b8c5fd).(cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# anchorA, cpVectcxx . . $ cpVect# anchorB, cpFloatcxx . . $ cpFloat# restLength, cpFloatcxx . . $ cpFloat# stiffness, cpFloatcxx . . $ cpFloat# damping) |
| 118 | { |
| 119 | return (cpConstraintcxx . . $ cpConstraint# *)cpDampedSpringInitcxx . . $ cpDampedSpringInit(d9a48c4637880a6b).(cpDampedSpringAlloccxx . . $ cpDampedSpringAlloc(36a8fb622e357825).(), a, b, anchorA, anchorB, restLength, stiffness, damping); |
| 120 | } |
| 121 | |
| 122 | cpBoolcxx . . $ cpBool# |
| 123 | cpConstraintIsDampedSpringcxx . . $ cpConstraintIsDampedSpring(ca4e56e976f68364).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 124 | { |
| 125 | return (constraint->klasscxx . . $ cpConstraint#klass. == &klasscxx . . $ klass.); |
| 126 | } |
| 127 | |
| 128 | cpVectcxx . . $ cpVect# |
| 129 | cpDampedSpringGetAnchorAcxx . . $ cpDampedSpringGetAnchorA(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 130 | { |
| 131 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 132 | return ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->anchorAcxx . . $ cpDampedSpring#anchorA.; |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | cpDampedSpringSetAnchorAcxx . . $ cpDampedSpringSetAnchorA(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# anchorA) |
| 137 | { |
| 138 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 139 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 140 | ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->anchorAcxx . . $ cpDampedSpring#anchorA. = anchorA; |
| 141 | } |
| 142 | |
| 143 | cpVectcxx . . $ cpVect# |
| 144 | cpDampedSpringGetAnchorBcxx . . $ cpDampedSpringGetAnchorB(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 145 | { |
| 146 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 147 | return ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->anchorBcxx . . $ cpDampedSpring#anchorB.; |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | cpDampedSpringSetAnchorBcxx . . $ cpDampedSpringSetAnchorB(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# anchorB) |
| 152 | { |
| 153 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 154 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 155 | ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->anchorBcxx . . $ cpDampedSpring#anchorB. = anchorB; |
| 156 | } |
| 157 | |
| 158 | cpFloatcxx . . $ cpFloat# |
| 159 | cpDampedSpringGetRestLengthcxx . . $ cpDampedSpringGetRestLength(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 160 | { |
| 161 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 162 | return ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->restLengthcxx . . $ cpDampedSpring#restLength.; |
| 163 | } |
| 164 | |
| 165 | void |
| 166 | cpDampedSpringSetRestLengthcxx . . $ cpDampedSpringSetRestLength(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# restLength) |
| 167 | { |
| 168 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 169 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 170 | ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->restLengthcxx . . $ cpDampedSpring#restLength. = restLength; |
| 171 | } |
| 172 | |
| 173 | cpFloatcxx . . $ cpFloat# |
| 174 | cpDampedSpringGetStiffnesscxx . . $ cpDampedSpringGetStiffness(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 175 | { |
| 176 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 177 | return ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->stiffnesscxx . . $ cpDampedSpring#stiffness.; |
| 178 | } |
| 179 | |
| 180 | void |
| 181 | cpDampedSpringSetStiffnesscxx . . $ cpDampedSpringSetStiffness(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# stiffness) |
| 182 | { |
| 183 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 184 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 185 | ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->stiffnesscxx . . $ cpDampedSpring#stiffness. = stiffness; |
| 186 | } |
| 187 | |
| 188 | cpFloatcxx . . $ cpFloat# |
| 189 | cpDampedSpringGetDampingcxx . . $ cpDampedSpringGetDamping(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 190 | { |
| 191 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 192 | return ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->dampingcxx . . $ cpDampedSpring#damping.; |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | cpDampedSpringSetDampingcxx . . $ cpDampedSpringSetDamping(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# damping) |
| 197 | { |
| 198 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 199 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 200 | ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->dampingcxx . . $ cpDampedSpring#damping. = damping; |
| 201 | } |
| 202 | |
| 203 | cpDampedSpringForceFunccxx . . $ cpDampedSpringForceFunc# |
| 204 | cpDampedSpringGetSpringForceFunccxx . . $ cpDampedSpringGetSpringForceFunc(ee677ac8b4e1cf75).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 205 | { |
| 206 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 207 | return ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->springForceFunccxx . . $ cpDampedSpring#springForceFunc.; |
| 208 | } |
| 209 | |
| 210 | void |
| 211 | cpDampedSpringSetSpringForceFunccxx . . $ cpDampedSpringSetSpringForceFunc(e3e72c040aa1b092).(cpConstraintcxx . . $ cpConstraint# *constraint, cpDampedSpringForceFunccxx . . $ cpDampedSpringForceFunc# springForceFunc) |
| 212 | { |
| 213 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsDampedSpring(constraint), "Constraint is not a damped spring."); |
| 214 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 215 | ((cpDampedSpringcxx . . $ cpDampedSpring# *)constraint)->springForceFunccxx . . $ cpDampedSpring#springForceFunc. = springForceFunc; |
| 216 | } |