| 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(1f5c2f9c07f5e7a4).(cpGrooveJointcxx . . $ cpGrooveJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 26 | { |
| 27 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpGrooveJoint#constraint..acxx . . $ cpConstraint#a.; |
| 28 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpGrooveJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 29 | |
| 30 | // calculate endpoints in worldspace |
| 31 | cpVectcxx . . $ cpVect# ta = cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(a->transformcxx . . $ cpBody#transform., joint->grv_acxx . . $ cpGrooveJoint#grv_a.); |
| 32 | cpVectcxx . . $ cpVect# tb = cpTransformPointcxx . . $ cpTransformPoint(7fc9e234cc6e750a).(a->transformcxx . . $ cpBody#transform., joint->grv_bcxx . . $ cpGrooveJoint#grv_b.); |
| 33 | |
| 34 | // calculate axis |
| 35 | cpVectcxx . . $ cpVect# n = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(a->transformcxx . . $ cpBody#transform., joint->grv_ncxx . . $ cpGrooveJoint#grv_n.); |
| 36 | cpFloatcxx . . $ cpFloat# d = cpvdotcxx . . $ cpvdot(7d57c1c4dd559d53).(ta, n); |
| 37 | |
| 38 | joint->grv_tncxx . . $ cpGrooveJoint#grv_tn. = n; |
| 39 | joint->r2cxx . . $ cpGrooveJoint#r2. = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(b->transformcxx . . $ cpBody#transform., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->anchorBcxx . . $ cpGrooveJoint#anchorB., b->cogcxx . . $ cpBody#cog.)); |
| 40 | |
| 41 | // calculate tangential distance along the axis of r2 |
| 42 | cpFloatcxx . . $ cpFloat# td = cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(b->pcxx . . $ cpBody#p., joint->r2cxx . . $ cpGrooveJoint#r2.), n); |
| 43 | // calculate clamping factor and r2 |
| 44 | if(td <= cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(ta, n)){ |
| 45 | joint->clampcxx . . $ cpGrooveJoint#clamp. = 1.0f; |
| 46 | joint->r1cxx . . $ cpGrooveJoint#r1. = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(ta, a->pcxx . . $ cpBody#p.); |
| 47 | } else if(td >= cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(tb, n)){ |
| 48 | joint->clampcxx . . $ cpGrooveJoint#clamp. = -1.0f; |
| 49 | joint->r1cxx . . $ cpGrooveJoint#r1. = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(tb, a->pcxx . . $ cpBody#p.); |
| 50 | } else { |
| 51 | joint->clampcxx . . $ cpGrooveJoint#clamp. = 0.0f; |
| 52 | joint->r1cxx . . $ cpGrooveJoint#r1. = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(cpvperpcxx . . $ cpvperp(e06142a62109ae08).(n), -td), cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(n, d)), a->pcxx . . $ cpBody#p.); |
| 53 | } |
| 54 | |
| 55 | // Calculate mass tensor |
| 56 | joint->kcxx . . $ cpGrooveJoint#k. = k_tensorcxx . . $ k_tensor(df4b34f57e414a55).(a, b, joint->r1cxx . . $ cpGrooveJoint#r1., joint->r2cxx . . $ cpGrooveJoint#r2.); |
| 57 | |
| 58 | // calculate bias velocity |
| 59 | cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(b->pcxx . . $ cpBody#p., joint->r2cxx . . $ cpGrooveJoint#r2.), cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(a->pcxx . . $ cpBody#p., joint->r1cxx . . $ cpGrooveJoint#r1.)); |
| 60 | joint->biascxx . . $ cpGrooveJoint#bias. = cpvclampcxx . . $ cpvclamp(f5cc44da17d64838).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(delta, -bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(joint->constraintcxx . . $ cpGrooveJoint#constraint..errorBiascxx . . $ cpConstraint#errorBias., dt)/dt), joint->constraintcxx . . $ cpGrooveJoint#constraint..maxBiascxx . . $ cpConstraint#maxBias.); |
| 61 | } |
| 62 | |
| 63 | static void |
| 64 | applyCachedImpulsecxx . . $ applyCachedImpulse(1f5c2f9c07f5e7a4).(cpGrooveJointcxx . . $ cpGrooveJoint# *joint, cpFloatcxx . . $ cpFloat# dt_coef) |
| 65 | { |
| 66 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpGrooveJoint#constraint..acxx . . $ cpConstraint#a.; |
| 67 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpGrooveJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 68 | |
| 69 | apply_impulsescxx . . $ apply_impulses(db940cdf68a6ac72).(a, b, joint->r1cxx . . $ cpGrooveJoint#r1., joint->r2cxx . . $ cpGrooveJoint#r2., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(joint->jAcccxx . . $ cpGrooveJoint#jAcc., dt_coef)); |
| 70 | } |
| 71 | |
| 72 | static inline cpVectcxx . . $ cpVect# |
| 73 | grooveConstraincxx . . $ grooveConstrain(476bf1d3f340b7d7).(cpGrooveJointcxx . . $ cpGrooveJoint# *joint, cpVectcxx . . $ cpVect# j, cpFloatcxx . . $ cpFloat# dt){ |
| 74 | cpVectcxx . . $ cpVect# n = joint->grv_tncxx . . $ cpGrooveJoint#grv_tn.; |
| 75 | cpVectcxx . . $ cpVect# jClamp = (joint->clampcxx . . $ cpGrooveJoint#clamp.*cpvcrosscxx . . $ cpvcross(7d57c1c4dd559d53).(j, n) > 0.0f) ? j : cpvprojectcxx . . $ cpvproject(47eb3f105675e23c).(j, n); |
| 76 | return cpvclampcxx . . $ cpvclamp(f5cc44da17d64838).(jClamp, joint->constraintcxx . . $ cpGrooveJoint#constraint..maxForcecxx . . $ cpConstraint#maxForce.*dt); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | applyImpulsecxx . . $ applyImpulse(1f5c2f9c07f5e7a4).(cpGrooveJointcxx . . $ cpGrooveJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 81 | { |
| 82 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpGrooveJoint#constraint..acxx . . $ cpConstraint#a.; |
| 83 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpGrooveJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 84 | |
| 85 | cpVectcxx . . $ cpVect# r1 = joint->r1cxx . . $ cpGrooveJoint#r1.; |
| 86 | cpVectcxx . . $ cpVect# r2 = joint->r2cxx . . $ cpGrooveJoint#r2.; |
| 87 | |
| 88 | // compute impulse |
| 89 | cpVectcxx . . $ cpVect# vr = relative_velocitycxx . . $ relative_velocity(2ac5ebec3c2bf710).(a, b, r1, r2); |
| 90 | |
| 91 | cpVectcxx . . $ cpVect# j = cpMat2x2Transformcxx . . $ cpMat2x2Transform(8e5c73201ddfef53).(joint->kcxx . . $ cpGrooveJoint#k., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->biascxx . . $ cpGrooveJoint#bias., vr)); |
| 92 | cpVectcxx . . $ cpVect# jOld = joint->jAcccxx . . $ cpGrooveJoint#jAcc.; |
| 93 | joint->jAcccxx . . $ cpGrooveJoint#jAcc. = grooveConstraincxx . . $ grooveConstrain(476bf1d3f340b7d7).(joint, cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(jOld, j), dt); |
| 94 | j = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->jAcccxx . . $ cpGrooveJoint#jAcc., jOld); |
| 95 | |
| 96 | // apply impulse |
| 97 | apply_impulsescxx . . $ apply_impulses(db940cdf68a6ac72).(a, b, joint->r1cxx . . $ cpGrooveJoint#r1., joint->r2cxx . . $ cpGrooveJoint#r2., j); |
| 98 | } |
| 99 | |
| 100 | static cpFloatcxx . . $ cpFloat# |
| 101 | getImpulsecxx . . $ getImpulse(e7ccd612375c88be).(cpGrooveJointcxx . . $ cpGrooveJoint# *joint) |
| 102 | { |
| 103 | return cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(joint->jAcccxx . . $ cpGrooveJoint#jAcc.); |
| 104 | } |
| 105 | |
| 106 | static const cpConstraintClasscxx . . $ cpConstraintClass# klasscxx . . $ klass. = { |
| 107 | (cpConstraintPreStepImplcxx . . $ cpConstraintPreStepImpl#)preStepcxx . . $ preStep(1f5c2f9c07f5e7a4)., |
| 108 | (cpConstraintApplyCachedImpulseImplcxx . . $ cpConstraintApplyCachedImpulseImpl#)applyCachedImpulsecxx . . $ applyCachedImpulse(1f5c2f9c07f5e7a4)., |
| 109 | (cpConstraintApplyImpulseImplcxx . . $ cpConstraintApplyImpulseImpl#)applyImpulsecxx . . $ applyImpulse(1f5c2f9c07f5e7a4)., |
| 110 | (cpConstraintGetImpulseImplcxx . . $ cpConstraintGetImpulseImpl#)getImpulsecxx . . $ getImpulse(e7ccd612375c88be)., |
| 111 | }; |
| 112 | |
| 113 | cpGrooveJointcxx . . $ cpGrooveJoint# * |
| 114 | cpGrooveJointAlloccxx . . $ cpGrooveJointAlloc(63561a10be8b7011).(void) |
| 115 | { |
| 116 | return (cpGrooveJointcxx . . $ cpGrooveJoint# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpGrooveJointcxx . . $ cpGrooveJoint#)); |
| 117 | } |
| 118 | |
| 119 | cpGrooveJointcxx . . $ cpGrooveJoint# * |
| 120 | cpGrooveJointInitcxx . . $ cpGrooveJointInit(d2ecd70125a4d466).(cpGrooveJointcxx . . $ cpGrooveJoint# *joint, cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# groove_a, cpVectcxx . . $ cpVect# groove_b, cpVectcxx . . $ cpVect# anchorB) |
| 121 | { |
| 122 | cpConstraintInitcxx . . $ cpConstraintInit(540fb6eaa94f170).((cpConstraintcxx . . $ cpConstraint# *)joint, &klasscxx . . $ klass., a, b); |
| 123 | |
| 124 | joint->grv_acxx . . $ cpGrooveJoint#grv_a. = groove_a; |
| 125 | joint->grv_bcxx . . $ cpGrooveJoint#grv_b. = groove_b; |
| 126 | joint->grv_ncxx . . $ cpGrooveJoint#grv_n. = cpvperpcxx . . $ cpvperp(e06142a62109ae08).(cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(groove_b, groove_a))); |
| 127 | joint->anchorBcxx . . $ cpGrooveJoint#anchorB. = anchorB; |
| 128 | |
| 129 | joint->jAcccxx . . $ cpGrooveJoint#jAcc. = cpvzerocxx . . $ cpvzero.; |
| 130 | |
| 131 | return joint; |
| 132 | } |
| 133 | |
| 134 | cpConstraintcxx . . $ cpConstraint# * |
| 135 | cpGrooveJointNewcxx . . $ cpGrooveJointNew(f2f94869a8417a6b).(cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# groove_a, cpVectcxx . . $ cpVect# groove_b, cpVectcxx . . $ cpVect# anchorB) |
| 136 | { |
| 137 | return (cpConstraintcxx . . $ cpConstraint# *)cpGrooveJointInitcxx . . $ cpGrooveJointInit(d2ecd70125a4d466).(cpGrooveJointAlloccxx . . $ cpGrooveJointAlloc(63561a10be8b7011).(), a, b, groove_a, groove_b, anchorB); |
| 138 | } |
| 139 | |
| 140 | cpBoolcxx . . $ cpBool# |
| 141 | cpConstraintIsGrooveJointcxx . . $ cpConstraintIsGrooveJoint(ca4e56e976f68364).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 142 | { |
| 143 | return (constraint->klasscxx . . $ cpConstraint#klass. == &klasscxx . . $ klass.); |
| 144 | } |
| 145 | |
| 146 | cpVectcxx . . $ cpVect# |
| 147 | cpGrooveJointGetGrooveAcxx . . $ cpGrooveJointGetGrooveA(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 148 | { |
| 149 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGrooveJoint(constraint), "Constraint is not a groove joint."); |
| 150 | return ((cpGrooveJointcxx . . $ cpGrooveJoint# *)constraint)->grv_acxx . . $ cpGrooveJoint#grv_a.; |
| 151 | } |
| 152 | |
| 153 | void |
| 154 | cpGrooveJointSetGrooveAcxx . . $ cpGrooveJointSetGrooveA(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# value) |
| 155 | { |
| 156 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGrooveJoint(constraint), "Constraint is not a groove joint."); |
| 157 | cpGrooveJointcxx . . $ cpGrooveJoint# *g = (cpGrooveJointcxx . . $ cpGrooveJoint# *)constraint; |
| 158 | |
| 159 | g->grv_acxx . . $ cpGrooveJoint#grv_a. = value; |
| 160 | g->grv_ncxx . . $ cpGrooveJoint#grv_n. = cpvperpcxx . . $ cpvperp(e06142a62109ae08).(cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(g->grv_bcxx . . $ cpGrooveJoint#grv_b., value))); |
| 161 | |
| 162 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 163 | } |
| 164 | |
| 165 | cpVectcxx . . $ cpVect# |
| 166 | cpGrooveJointGetGrooveBcxx . . $ cpGrooveJointGetGrooveB(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 167 | { |
| 168 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGrooveJoint(constraint), "Constraint is not a groove joint."); |
| 169 | return ((cpGrooveJointcxx . . $ cpGrooveJoint# *)constraint)->grv_bcxx . . $ cpGrooveJoint#grv_b.; |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | cpGrooveJointSetGrooveBcxx . . $ cpGrooveJointSetGrooveB(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# value) |
| 174 | { |
| 175 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGrooveJoint(constraint), "Constraint is not a groove joint."); |
| 176 | cpGrooveJointcxx . . $ cpGrooveJoint# *g = (cpGrooveJointcxx . . $ cpGrooveJoint# *)constraint; |
| 177 | |
| 178 | g->grv_bcxx . . $ cpGrooveJoint#grv_b. = value; |
| 179 | g->grv_ncxx . . $ cpGrooveJoint#grv_n. = cpvperpcxx . . $ cpvperp(e06142a62109ae08).(cpvnormalizecxx . . $ cpvnormalize(e06142a62109ae08).(cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(value, g->grv_acxx . . $ cpGrooveJoint#grv_a.))); |
| 180 | |
| 181 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 182 | } |
| 183 | |
| 184 | cpVectcxx . . $ cpVect# |
| 185 | cpGrooveJointGetAnchorBcxx . . $ cpGrooveJointGetAnchorB(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 186 | { |
| 187 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGrooveJoint(constraint), "Constraint is not a groove joint."); |
| 188 | return ((cpGrooveJointcxx . . $ cpGrooveJoint# *)constraint)->anchorBcxx . . $ cpGrooveJoint#anchorB.; |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | cpGrooveJointSetAnchorBcxx . . $ cpGrooveJointSetAnchorB(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# anchorB) |
| 193 | { |
| 194 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGrooveJoint(constraint), "Constraint is not a groove joint."); |
| 195 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 196 | ((cpGrooveJointcxx . . $ cpGrooveJoint# *)constraint)->anchorBcxx . . $ cpGrooveJoint#anchorB. = anchorB; |
| 197 | } |