| 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(12b15c9fc31088e0).(cpGearJointcxx . . $ cpGearJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 26 | { |
| 27 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpGearJoint#constraint..acxx . . $ cpConstraint#a.; |
| 28 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpGearJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 29 | |
| 30 | // calculate moment of inertia coefficient. |
| 31 | joint->iSumcxx . . $ cpGearJoint#iSum. = 1.0f/(a->i_invcxx . . $ cpBody#i_inv.*joint->ratio_invcxx . . $ cpGearJoint#ratio_inv. + joint->ratiocxx . . $ cpGearJoint#ratio.*b->i_invcxx . . $ cpBody#i_inv.); |
| 32 | |
| 33 | // calculate bias velocity |
| 34 | cpFloatcxx . . $ cpFloat# maxBias = joint->constraintcxx . . $ cpGearJoint#constraint..maxBiascxx . . $ cpConstraint#maxBias.; |
| 35 | joint->biascxx . . $ cpGearJoint#bias. = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(-bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(joint->constraintcxx . . $ cpGearJoint#constraint..errorBiascxx . . $ cpConstraint#errorBias., dt)*(b->acxx . . $ cpBody#a.*joint->ratiocxx . . $ cpGearJoint#ratio. - a->acxx . . $ cpBody#a. - joint->phasecxx . . $ cpGearJoint#phase.)/dt, -maxBias, maxBias); |
| 36 | } |
| 37 | |
| 38 | static void |
| 39 | applyCachedImpulsecxx . . $ applyCachedImpulse(12b15c9fc31088e0).(cpGearJointcxx . . $ cpGearJoint# *joint, cpFloatcxx . . $ cpFloat# dt_coef) |
| 40 | { |
| 41 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpGearJoint#constraint..acxx . . $ cpConstraint#a.; |
| 42 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpGearJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 43 | |
| 44 | cpFloatcxx . . $ cpFloat# j = joint->jAcccxx . . $ cpGearJoint#jAcc.*dt_coef; |
| 45 | a->wcxx . . $ cpBody#w. -= j*a->i_invcxx . . $ cpBody#i_inv.*joint->ratio_invcxx . . $ cpGearJoint#ratio_inv.; |
| 46 | b->wcxx . . $ cpBody#w. += j*b->i_invcxx . . $ cpBody#i_inv.; |
| 47 | } |
| 48 | |
| 49 | static void |
| 50 | applyImpulsecxx . . $ applyImpulse(12b15c9fc31088e0).(cpGearJointcxx . . $ cpGearJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 51 | { |
| 52 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpGearJoint#constraint..acxx . . $ cpConstraint#a.; |
| 53 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpGearJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 54 | |
| 55 | // compute relative rotational velocity |
| 56 | cpFloatcxx . . $ cpFloat# wr = b->wcxx . . $ cpBody#w.*joint->ratiocxx . . $ cpGearJoint#ratio. - a->wcxx . . $ cpBody#w.; |
| 57 | |
| 58 | cpFloatcxx . . $ cpFloat# jMax = joint->constraintcxx . . $ cpGearJoint#constraint..maxForcecxx . . $ cpConstraint#maxForce.*dt; |
| 59 | |
| 60 | // compute normal impulse |
| 61 | cpFloatcxx . . $ cpFloat# j = (joint->biascxx . . $ cpGearJoint#bias. - wr)*joint->iSumcxx . . $ cpGearJoint#iSum.; |
| 62 | cpFloatcxx . . $ cpFloat# jOld = joint->jAcccxx . . $ cpGearJoint#jAcc.; |
| 63 | joint->jAcccxx . . $ cpGearJoint#jAcc. = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(jOld + j, -jMax, jMax); |
| 64 | j = joint->jAcccxx . . $ cpGearJoint#jAcc. - jOld; |
| 65 | |
| 66 | // apply impulse |
| 67 | a->wcxx . . $ cpBody#w. -= j*a->i_invcxx . . $ cpBody#i_inv.*joint->ratio_invcxx . . $ cpGearJoint#ratio_inv.; |
| 68 | b->wcxx . . $ cpBody#w. += j*b->i_invcxx . . $ cpBody#i_inv.; |
| 69 | } |
| 70 | |
| 71 | static cpFloatcxx . . $ cpFloat# |
| 72 | getImpulsecxx . . $ getImpulse(f0f9f38519d66fab).(cpGearJointcxx . . $ cpGearJoint# *joint) |
| 73 | { |
| 74 | return cpfabscxx . . $ cpfabs(447c67c6676f75c3).(joint->jAcccxx . . $ cpGearJoint#jAcc.); |
| 75 | } |
| 76 | |
| 77 | static const cpConstraintClasscxx . . $ cpConstraintClass# klasscxx . . $ klass. = { |
| 78 | (cpConstraintPreStepImplcxx . . $ cpConstraintPreStepImpl#)preStepcxx . . $ preStep(12b15c9fc31088e0)., |
| 79 | (cpConstraintApplyCachedImpulseImplcxx . . $ cpConstraintApplyCachedImpulseImpl#)applyCachedImpulsecxx . . $ applyCachedImpulse(12b15c9fc31088e0)., |
| 80 | (cpConstraintApplyImpulseImplcxx . . $ cpConstraintApplyImpulseImpl#)applyImpulsecxx . . $ applyImpulse(12b15c9fc31088e0)., |
| 81 | (cpConstraintGetImpulseImplcxx . . $ cpConstraintGetImpulseImpl#)getImpulsecxx . . $ getImpulse(f0f9f38519d66fab)., |
| 82 | }; |
| 83 | |
| 84 | cpGearJointcxx . . $ cpGearJoint# * |
| 85 | cpGearJointAlloccxx . . $ cpGearJointAlloc(428b0fd7f49b31af).(void) |
| 86 | { |
| 87 | return (cpGearJointcxx . . $ cpGearJoint# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpGearJointcxx . . $ cpGearJoint#)); |
| 88 | } |
| 89 | |
| 90 | cpGearJointcxx . . $ cpGearJoint# * |
| 91 | cpGearJointInitcxx . . $ cpGearJointInit(c2733191032eead8).(cpGearJointcxx . . $ cpGearJoint# *joint, cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpFloatcxx . . $ cpFloat# phase, cpFloatcxx . . $ cpFloat# ratio) |
| 92 | { |
| 93 | cpConstraintInitcxx . . $ cpConstraintInit(540fb6eaa94f170).((cpConstraintcxx . . $ cpConstraint# *)joint, &klasscxx . . $ klass., a, b); |
| 94 | |
| 95 | joint->phasecxx . . $ cpGearJoint#phase. = phase; |
| 96 | joint->ratiocxx . . $ cpGearJoint#ratio. = ratio; |
| 97 | joint->ratio_invcxx . . $ cpGearJoint#ratio_inv. = 1.0f/ratio; |
| 98 | |
| 99 | joint->jAcccxx . . $ cpGearJoint#jAcc. = 0.0f; |
| 100 | |
| 101 | return joint; |
| 102 | } |
| 103 | |
| 104 | cpConstraintcxx . . $ cpConstraint# * |
| 105 | cpGearJointNewcxx . . $ cpGearJointNew(d029de5d93e4cb4a).(cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpFloatcxx . . $ cpFloat# phase, cpFloatcxx . . $ cpFloat# ratio) |
| 106 | { |
| 107 | return (cpConstraintcxx . . $ cpConstraint# *)cpGearJointInitcxx . . $ cpGearJointInit(c2733191032eead8).(cpGearJointAlloccxx . . $ cpGearJointAlloc(428b0fd7f49b31af).(), a, b, phase, ratio); |
| 108 | } |
| 109 | |
| 110 | cpBoolcxx . . $ cpBool# |
| 111 | cpConstraintIsGearJointcxx . . $ cpConstraintIsGearJoint(ca4e56e976f68364).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 112 | { |
| 113 | return (constraint->klasscxx . . $ cpConstraint#klass. == &klasscxx . . $ klass.); |
| 114 | } |
| 115 | |
| 116 | cpFloatcxx . . $ cpFloat# |
| 117 | cpGearJointGetPhasecxx . . $ cpGearJointGetPhase(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 118 | { |
| 119 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint."); |
| 120 | return ((cpGearJointcxx . . $ cpGearJoint# *)constraint)->phasecxx . . $ cpGearJoint#phase.; |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | cpGearJointSetPhasecxx . . $ cpGearJointSetPhase(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# phase) |
| 125 | { |
| 126 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint."); |
| 127 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 128 | ((cpGearJointcxx . . $ cpGearJoint# *)constraint)->phasecxx . . $ cpGearJoint#phase. = phase; |
| 129 | } |
| 130 | |
| 131 | cpFloatcxx . . $ cpFloat# |
| 132 | cpGearJointGetRatiocxx . . $ cpGearJointGetRatio(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 133 | { |
| 134 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint."); |
| 135 | return ((cpGearJointcxx . . $ cpGearJoint# *)constraint)->ratiocxx . . $ cpGearJoint#ratio.; |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | cpGearJointSetRatiocxx . . $ cpGearJointSetRatio(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# ratio) |
| 140 | { |
| 141 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint."); |
| 142 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 143 | ((cpGearJointcxx . . $ cpGearJoint# *)constraint)->ratiocxx . . $ cpGearJoint#ratio. = ratio; |
| 144 | ((cpGearJointcxx . . $ cpGearJoint# *)constraint)->ratio_invcxx . . $ cpGearJoint#ratio_inv. = 1.0f/ratio; |
| 145 | } |