| 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 void |
| 25 | preStepcxx . . $ preStep(ca51702dbe117275).(cpSlideJointcxx . . $ cpSlideJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 26 | { |
| 27 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpSlideJoint#constraint..acxx . . $ cpConstraint#a.; |
| 28 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpSlideJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 29 | |
| 30 | joint->r1cxx . . $ cpSlideJoint#r1. = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(a->transformcxx . . $ cpBody#transform., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->anchorAcxx . . $ cpSlideJoint#anchorA., a->cogcxx . . $ cpBody#cog.)); |
| 31 | joint->r2cxx . . $ cpSlideJoint#r2. = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(b->transformcxx . . $ cpBody#transform., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->anchorBcxx . . $ cpSlideJoint#anchorB., b->cogcxx . . $ cpBody#cog.)); |
| 32 | |
| 33 | cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(b->pcxx . . $ cpBody#p., joint->r2cxx . . $ cpSlideJoint#r2.), cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(a->pcxx . . $ cpBody#p., joint->r1cxx . . $ cpSlideJoint#r1.)); |
| 34 | cpFloatcxx . . $ cpFloat# dist = cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(delta); |
| 35 | cpFloatcxx . . $ cpFloat# pdist = 0.0f; |
| 36 | if(dist > joint->maxcxx . . $ cpSlideJoint#max.) { |
| 37 | pdist = dist - joint->maxcxx . . $ cpSlideJoint#max.; |
| 38 | joint->ncxx . . $ cpSlideJoint#n. = cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(delta); |
| 39 | } else if(dist < joint->mincxx . . $ cpSlideJoint#min.) { |
| 40 | pdist = joint->mincxx . . $ cpSlideJoint#min. - dist; |
| 41 | joint->ncxx . . $ cpSlideJoint#n. = cpvnegcxx . . $ cpvneg(e06142a62109ae08).(cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(delta)); |
| 42 | } else { |
| 43 | joint->ncxx . . $ cpSlideJoint#n. = cpvzerocxx . . $ cpvzero.; |
| 44 | joint->jnAcccxx . . $ cpSlideJoint#jnAcc. = 0.0f; |
| 45 | } |
| 46 | |
| 47 | // calculate mass normal |
| 48 | joint->nMasscxx . . $ cpSlideJoint#nMass. = 1.0f/k_scalarcxx . . $ k_scalar(d97a6b5999e6b615).(a, b, joint->r1cxx . . $ cpSlideJoint#r1., joint->r2cxx . . $ cpSlideJoint#r2., joint->ncxx . . $ cpSlideJoint#n.); |
| 49 | |
| 50 | // calculate bias velocity |
| 51 | cpFloatcxx . . $ cpFloat# maxBias = joint->constraintcxx . . $ cpSlideJoint#constraint..maxBiascxx . . $ cpConstraint#maxBias.; |
| 52 | joint->biascxx . . $ cpSlideJoint#bias. = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(-bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(joint->constraintcxx . . $ cpSlideJoint#constraint..errorBiascxx . . $ cpConstraint#errorBias., dt)*pdist/dt, -maxBias, maxBias); |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | applyCachedImpulsecxx . . $ applyCachedImpulse(ca51702dbe117275).(cpSlideJointcxx . . $ cpSlideJoint# *joint, cpFloatcxx . . $ cpFloat# dt_coef) |
| 57 | { |
| 58 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpSlideJoint#constraint..acxx . . $ cpConstraint#a.; |
| 59 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpSlideJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 60 | |
| 61 | cpVectcxx . . $ cpVect# j = cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(joint->ncxx . . $ cpSlideJoint#n., joint->jnAcccxx . . $ cpSlideJoint#jnAcc.*dt_coef); |
| 62 | apply_impulsescxx . . $ apply_impulses(db940cdf68a6ac72).(a, b, joint->r1cxx . . $ cpSlideJoint#r1., joint->r2cxx . . $ cpSlideJoint#r2., j); |
| 63 | } |
| 64 | |
| 65 | static void |
| 66 | applyImpulsecxx . . $ applyImpulse(ca51702dbe117275).(cpSlideJointcxx . . $ cpSlideJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 67 | { |
| 68 | if(cpveqlcxx . . $ cpveql(c42fe4e993c6be46).(joint->ncxx . . $ cpSlideJoint#n., cpvzerocxx . . $ cpvzero.)) return; // early exit |
| 69 | |
| 70 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpSlideJoint#constraint..acxx . . $ cpConstraint#a.; |
| 71 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpSlideJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 72 | |
| 73 | cpVectcxx . . $ cpVect# n = joint->ncxx . . $ cpSlideJoint#n.; |
| 74 | cpVectcxx . . $ cpVect# r1 = joint->r1cxx . . $ cpSlideJoint#r1.; |
| 75 | cpVectcxx . . $ cpVect# r2 = joint->r2cxx . . $ cpSlideJoint#r2.; |
| 76 | |
| 77 | // compute relative velocity |
| 78 | cpVectcxx . . $ cpVect# vr = relative_velocitycxx . . $ relative_velocity(2ac5ebec3c2bf710).(a, b, r1, r2); |
| 79 | cpFloatcxx . . $ cpFloat# vrn = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(vr, n); |
| 80 | |
| 81 | // compute normal impulse |
| 82 | cpFloatcxx . . $ cpFloat# jn = (joint->biascxx . . $ cpSlideJoint#bias. - vrn)*joint->nMasscxx . . $ cpSlideJoint#nMass.; |
| 83 | cpFloatcxx . . $ cpFloat# jnOld = joint->jnAcccxx . . $ cpSlideJoint#jnAcc.; |
| 84 | joint->jnAcccxx . . $ cpSlideJoint#jnAcc. = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(jnOld + jn, -joint->constraintcxx . . $ cpSlideJoint#constraint..maxForcecxx . . $ cpConstraint#maxForce.*dt, 0.0f); |
| 85 | jn = joint->jnAcccxx . . $ cpSlideJoint#jnAcc. - jnOld; |
| 86 | |
| 87 | // apply impulse |
| 88 | apply_impulsescxx . . $ apply_impulses(db940cdf68a6ac72).(a, b, joint->r1cxx . . $ cpSlideJoint#r1., joint->r2cxx . . $ cpSlideJoint#r2., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, jn)); |
| 89 | } |
| 90 | |
| 91 | static cpFloatcxx . . $ cpFloat# |
| 92 | getImpulsecxx . . $ getImpulse(4f5ff132f97724f3).(cpConstraintcxx . . $ cpConstraint# *joint) |
| 93 | { |
| 94 | return cpfabscxx . . $ cpfabs(447c67c6676f75c3).(((cpSlideJointcxx . . $ cpSlideJoint# *)joint)->jnAcccxx . . $ cpSlideJoint#jnAcc.); |
| 95 | } |
| 96 | |
| 97 | static const cpConstraintClasscxx . . $ cpConstraintClass# klasscxx . . $ klass. = { |
| 98 | (cpConstraintPreStepImplcxx . . $ cpConstraintPreStepImpl#)preStepcxx . . $ preStep(ca51702dbe117275)., |
| 99 | (cpConstraintApplyCachedImpulseImplcxx . . $ cpConstraintApplyCachedImpulseImpl#)applyCachedImpulsecxx . . $ applyCachedImpulse(ca51702dbe117275)., |
| 100 | (cpConstraintApplyImpulseImplcxx . . $ cpConstraintApplyImpulseImpl#)applyImpulsecxx . . $ applyImpulse(ca51702dbe117275)., |
| 101 | (cpConstraintGetImpulseImplcxx . . $ cpConstraintGetImpulseImpl#)getImpulsecxx . . $ getImpulse(4f5ff132f97724f3)., |
| 102 | }; |
| 103 | |
| 104 | cpSlideJointcxx . . $ cpSlideJoint# * |
| 105 | cpSlideJointAlloccxx . . $ cpSlideJointAlloc(3f58f1376493320f).(void) |
| 106 | { |
| 107 | return (cpSlideJointcxx . . $ cpSlideJoint# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpSlideJointcxx . . $ cpSlideJoint#)); |
| 108 | } |
| 109 | |
| 110 | cpSlideJointcxx . . $ cpSlideJoint# * |
| 111 | cpSlideJointInitcxx . . $ cpSlideJointInit(ea5d607c1a3a3866).(cpSlideJointcxx . . $ cpSlideJoint# *joint, cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# anchorA, cpVectcxx . . $ cpVect# anchorB, cpFloatcxx . . $ cpFloat# min, cpFloatcxx . . $ cpFloat# max) |
| 112 | { |
| 113 | cpConstraintInitcxx . . $ cpConstraintInit(540fb6eaa94f170).((cpConstraintcxx . . $ cpConstraint# *)joint, &klasscxx . . $ klass., a, b); |
| 114 | |
| 115 | joint->anchorAcxx . . $ cpSlideJoint#anchorA. = anchorA; |
| 116 | joint->anchorBcxx . . $ cpSlideJoint#anchorB. = anchorB; |
| 117 | joint->mincxx . . $ cpSlideJoint#min. = min; |
| 118 | joint->maxcxx . . $ cpSlideJoint#max. = max; |
| 119 | |
| 120 | joint->jnAcccxx . . $ cpSlideJoint#jnAcc. = 0.0f; |
| 121 | |
| 122 | return joint; |
| 123 | } |
| 124 | |
| 125 | cpConstraintcxx . . $ cpConstraint# * |
| 126 | cpSlideJointNewcxx . . $ cpSlideJointNew(5b97552dd9e8a693).(cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# anchorA, cpVectcxx . . $ cpVect# anchorB, cpFloatcxx . . $ cpFloat# min, cpFloatcxx . . $ cpFloat# max) |
| 127 | { |
| 128 | return (cpConstraintcxx . . $ cpConstraint# *)cpSlideJointInitcxx . . $ cpSlideJointInit(ea5d607c1a3a3866).(cpSlideJointAlloccxx . . $ cpSlideJointAlloc(3f58f1376493320f).(), a, b, anchorA, anchorB, min, max); |
| 129 | } |
| 130 | |
| 131 | cpBoolcxx . . $ cpBool# |
| 132 | cpConstraintIsSlideJointcxx . . $ cpConstraintIsSlideJoint(ca4e56e976f68364).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 133 | { |
| 134 | return (constraint->klasscxx . . $ cpConstraint#klass. == &klasscxx . . $ klass.); |
| 135 | } |
| 136 | |
| 137 | cpVectcxx . . $ cpVect# |
| 138 | cpSlideJointGetAnchorAcxx . . $ cpSlideJointGetAnchorA(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 139 | { |
| 140 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint."); |
| 141 | return ((cpSlideJointcxx . . $ cpSlideJoint# *)constraint)->anchorAcxx . . $ cpSlideJoint#anchorA.; |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | cpSlideJointSetAnchorAcxx . . $ cpSlideJointSetAnchorA(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# anchorA) |
| 146 | { |
| 147 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint."); |
| 148 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 149 | ((cpSlideJointcxx . . $ cpSlideJoint# *)constraint)->anchorAcxx . . $ cpSlideJoint#anchorA. = anchorA; |
| 150 | } |
| 151 | |
| 152 | cpVectcxx . . $ cpVect# |
| 153 | cpSlideJointGetAnchorBcxx . . $ cpSlideJointGetAnchorB(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 154 | { |
| 155 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint."); |
| 156 | return ((cpSlideJointcxx . . $ cpSlideJoint# *)constraint)->anchorBcxx . . $ cpSlideJoint#anchorB.; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | cpSlideJointSetAnchorBcxx . . $ cpSlideJointSetAnchorB(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# anchorB) |
| 161 | { |
| 162 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint."); |
| 163 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 164 | ((cpSlideJointcxx . . $ cpSlideJoint# *)constraint)->anchorBcxx . . $ cpSlideJoint#anchorB. = anchorB; |
| 165 | } |
| 166 | |
| 167 | cpFloatcxx . . $ cpFloat# |
| 168 | cpSlideJointGetMincxx . . $ cpSlideJointGetMin(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 169 | { |
| 170 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint."); |
| 171 | return ((cpSlideJointcxx . . $ cpSlideJoint# *)constraint)->mincxx . . $ cpSlideJoint#min.; |
| 172 | } |
| 173 | |
| 174 | void |
| 175 | cpSlideJointSetMincxx . . $ cpSlideJointSetMin(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# min) |
| 176 | { |
| 177 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint."); |
| 178 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 179 | ((cpSlideJointcxx . . $ cpSlideJoint# *)constraint)->mincxx . . $ cpSlideJoint#min. = min; |
| 180 | } |
| 181 | |
| 182 | cpFloatcxx . . $ cpFloat# |
| 183 | cpSlideJointGetMaxcxx . . $ cpSlideJointGetMax(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 184 | { |
| 185 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint."); |
| 186 | return ((cpSlideJointcxx . . $ cpSlideJoint# *)constraint)->maxcxx . . $ cpSlideJoint#max.; |
| 187 | } |
| 188 | |
| 189 | void |
| 190 | cpSlideJointSetMaxcxx . . $ cpSlideJointSetMax(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# max) |
| 191 | { |
| 192 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint."); |
| 193 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 194 | ((cpSlideJointcxx . . $ cpSlideJoint# *)constraint)->maxcxx . . $ cpSlideJoint#max. = max; |
| 195 | } |