| 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(1dda1fe05d2e7470).(cpRatchetJointcxx . . $ cpRatchetJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 26 | { |
| 27 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpRatchetJoint#constraint..acxx . . $ cpConstraint#a.; |
| 28 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpRatchetJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 29 | |
| 30 | cpFloatcxx . . $ cpFloat# angle = joint->anglecxx . . $ cpRatchetJoint#angle.; |
| 31 | cpFloatcxx . . $ cpFloat# phase = joint->phasecxx . . $ cpRatchetJoint#phase.; |
| 32 | cpFloatcxx . . $ cpFloat# ratchet = joint->ratchetcxx . . $ cpRatchetJoint#ratchet.; |
| 33 | |
| 34 | cpFloatcxx . . $ cpFloat# delta = b->acxx . . $ cpBody#a. - a->acxx . . $ cpBody#a.; |
| 35 | cpFloatcxx . . $ cpFloat# diff = angle - delta; |
| 36 | cpFloatcxx . . $ cpFloat# pdist = 0.0f; |
| 37 | |
| 38 | if(diff*ratchet > 0.0f){ |
| 39 | pdist = diff; |
| 40 | } else { |
| 41 | joint->anglecxx . . $ cpRatchetJoint#angle. = cpffloorcxx . . $ `include/chipmunk/chipmunk_types.h:76:10`!((delta - phase)/ratchet)*ratchet + phase; |
| 42 | } |
| 43 | |
| 44 | // calculate moment of inertia coefficient. |
| 45 | joint->iSumcxx . . $ cpRatchetJoint#iSum. = 1.0f/(a->i_invcxx . . $ cpBody#i_inv. + b->i_invcxx . . $ cpBody#i_inv.); |
| 46 | |
| 47 | // calculate bias velocity |
| 48 | cpFloatcxx . . $ cpFloat# maxBias = joint->constraintcxx . . $ cpRatchetJoint#constraint..maxBiascxx . . $ cpConstraint#maxBias.; |
| 49 | joint->biascxx . . $ cpRatchetJoint#bias. = cpfclampcxx . . $ cpfclamp(7678bba071383ab).(-bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(joint->constraintcxx . . $ cpRatchetJoint#constraint..errorBiascxx . . $ cpConstraint#errorBias., dt)*pdist/dt, -maxBias, maxBias); |
| 50 | |
| 51 | // If the bias is 0, the joint is not at a limit. Reset the impulse. |
| 52 | if(!joint->biascxx . . $ cpRatchetJoint#bias.) joint->jAcccxx . . $ cpRatchetJoint#jAcc. = 0.0f; |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | applyCachedImpulsecxx . . $ applyCachedImpulse(1dda1fe05d2e7470).(cpRatchetJointcxx . . $ cpRatchetJoint# *joint, cpFloatcxx . . $ cpFloat# dt_coef) |
| 57 | { |
| 58 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpRatchetJoint#constraint..acxx . . $ cpConstraint#a.; |
| 59 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpRatchetJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 60 | |
| 61 | cpFloatcxx . . $ cpFloat# j = joint->jAcccxx . . $ cpRatchetJoint#jAcc.*dt_coef; |
| 62 | a->wcxx . . $ cpBody#w. -= j*a->i_invcxx . . $ cpBody#i_inv.; |
| 63 | b->wcxx . . $ cpBody#w. += j*b->i_invcxx . . $ cpBody#i_inv.; |
| 64 | } |
| 65 | |
| 66 | static void |
| 67 | applyImpulsecxx . . $ applyImpulse(1dda1fe05d2e7470).(cpRatchetJointcxx . . $ cpRatchetJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 68 | { |
| 69 | if(!joint->biascxx . . $ cpRatchetJoint#bias.) return; // early exit |
| 70 | |
| 71 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpRatchetJoint#constraint..acxx . . $ cpConstraint#a.; |
| 72 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpRatchetJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 73 | |
| 74 | // compute relative rotational velocity |
| 75 | cpFloatcxx . . $ cpFloat# wr = b->wcxx . . $ cpBody#w. - a->wcxx . . $ cpBody#w.; |
| 76 | cpFloatcxx . . $ cpFloat# ratchet = joint->ratchetcxx . . $ cpRatchetJoint#ratchet.; |
| 77 | |
| 78 | cpFloatcxx . . $ cpFloat# jMax = joint->constraintcxx . . $ cpRatchetJoint#constraint..maxForcecxx . . $ cpConstraint#maxForce.*dt; |
| 79 | |
| 80 | // compute normal impulse |
| 81 | cpFloatcxx . . $ cpFloat# j = -(joint->biascxx . . $ cpRatchetJoint#bias. + wr)*joint->iSumcxx . . $ cpRatchetJoint#iSum.; |
| 82 | cpFloatcxx . . $ cpFloat# jOld = joint->jAcccxx . . $ cpRatchetJoint#jAcc.; |
| 83 | joint->jAcccxx . . $ cpRatchetJoint#jAcc. = cpfclampcxx . . $ cpfclamp(7678bba071383ab).((jOld + j)*ratchet, 0.0f, jMax*cpfabscxx . . $ cpfabs(447c67c6676f75c3).(ratchet))/ratchet; |
| 84 | j = joint->jAcccxx . . $ cpRatchetJoint#jAcc. - jOld; |
| 85 | |
| 86 | // apply impulse |
| 87 | a->wcxx . . $ cpBody#w. -= j*a->i_invcxx . . $ cpBody#i_inv.; |
| 88 | b->wcxx . . $ cpBody#w. += j*b->i_invcxx . . $ cpBody#i_inv.; |
| 89 | } |
| 90 | |
| 91 | static cpFloatcxx . . $ cpFloat# |
| 92 | getImpulsecxx . . $ getImpulse(596c793f03893b0d).(cpRatchetJointcxx . . $ cpRatchetJoint# *joint) |
| 93 | { |
| 94 | return cpfabscxx . . $ cpfabs(447c67c6676f75c3).(joint->jAcccxx . . $ cpRatchetJoint#jAcc.); |
| 95 | } |
| 96 | |
| 97 | static const cpConstraintClasscxx . . $ cpConstraintClass# klasscxx . . $ klass. = { |
| 98 | (cpConstraintPreStepImplcxx . . $ cpConstraintPreStepImpl#)preStepcxx . . $ preStep(1dda1fe05d2e7470)., |
| 99 | (cpConstraintApplyCachedImpulseImplcxx . . $ cpConstraintApplyCachedImpulseImpl#)applyCachedImpulsecxx . . $ applyCachedImpulse(1dda1fe05d2e7470)., |
| 100 | (cpConstraintApplyImpulseImplcxx . . $ cpConstraintApplyImpulseImpl#)applyImpulsecxx . . $ applyImpulse(1dda1fe05d2e7470)., |
| 101 | (cpConstraintGetImpulseImplcxx . . $ cpConstraintGetImpulseImpl#)getImpulsecxx . . $ getImpulse(596c793f03893b0d)., |
| 102 | }; |
| 103 | |
| 104 | cpRatchetJointcxx . . $ cpRatchetJoint# * |
| 105 | cpRatchetJointAlloccxx . . $ cpRatchetJointAlloc(76da0e6e96f81d7d).(void) |
| 106 | { |
| 107 | return (cpRatchetJointcxx . . $ cpRatchetJoint# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpRatchetJointcxx . . $ cpRatchetJoint#)); |
| 108 | } |
| 109 | |
| 110 | cpRatchetJointcxx . . $ cpRatchetJoint# * |
| 111 | cpRatchetJointInitcxx . . $ cpRatchetJointInit(4c71536c5d1b86e9).(cpRatchetJointcxx . . $ cpRatchetJoint# *joint, cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpFloatcxx . . $ cpFloat# phase, cpFloatcxx . . $ cpFloat# ratchet) |
| 112 | { |
| 113 | cpConstraintInitcxx . . $ cpConstraintInit(540fb6eaa94f170).((cpConstraintcxx . . $ cpConstraint# *)joint, &klasscxx . . $ klass., a, b); |
| 114 | |
| 115 | joint->anglecxx . . $ cpRatchetJoint#angle. = 0.0f; |
| 116 | joint->phasecxx . . $ cpRatchetJoint#phase. = phase; |
| 117 | joint->ratchetcxx . . $ cpRatchetJoint#ratchet. = ratchet; |
| 118 | |
| 119 | // STATIC_BODY_CHECK |
| 120 | joint->anglecxx . . $ cpRatchetJoint#angle. = (b ? b->acxx . . $ cpBody#a. : 0.0f) - (a ? a->acxx . . $ cpBody#a. : 0.0f); |
| 121 | |
| 122 | return joint; |
| 123 | } |
| 124 | |
| 125 | cpConstraintcxx . . $ cpConstraint# * |
| 126 | cpRatchetJointNewcxx . . $ cpRatchetJointNew(d029de5d93e4cb4a).(cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpFloatcxx . . $ cpFloat# phase, cpFloatcxx . . $ cpFloat# ratchet) |
| 127 | { |
| 128 | return (cpConstraintcxx . . $ cpConstraint# *)cpRatchetJointInitcxx . . $ cpRatchetJointInit(4c71536c5d1b86e9).(cpRatchetJointAlloccxx . . $ cpRatchetJointAlloc(76da0e6e96f81d7d).(), a, b, phase, ratchet); |
| 129 | } |
| 130 | |
| 131 | cpBoolcxx . . $ cpBool# |
| 132 | cpConstraintIsRatchetJointcxx . . $ cpConstraintIsRatchetJoint(ca4e56e976f68364).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 133 | { |
| 134 | return (constraint->klasscxx . . $ cpConstraint#klass. == &klasscxx . . $ klass.); |
| 135 | } |
| 136 | |
| 137 | cpFloatcxx . . $ cpFloat# |
| 138 | cpRatchetJointGetAnglecxx . . $ cpRatchetJointGetAngle(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 139 | { |
| 140 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint."); |
| 141 | return ((cpRatchetJointcxx . . $ cpRatchetJoint# *)constraint)->anglecxx . . $ cpRatchetJoint#angle.; |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | cpRatchetJointSetAnglecxx . . $ cpRatchetJointSetAngle(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# angle) |
| 146 | { |
| 147 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint."); |
| 148 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 149 | ((cpRatchetJointcxx . . $ cpRatchetJoint# *)constraint)->anglecxx . . $ cpRatchetJoint#angle. = angle; |
| 150 | } |
| 151 | |
| 152 | cpFloatcxx . . $ cpFloat# |
| 153 | cpRatchetJointGetPhasecxx . . $ cpRatchetJointGetPhase(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 154 | { |
| 155 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint."); |
| 156 | return ((cpRatchetJointcxx . . $ cpRatchetJoint# *)constraint)->phasecxx . . $ cpRatchetJoint#phase.; |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | cpRatchetJointSetPhasecxx . . $ cpRatchetJointSetPhase(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# phase) |
| 161 | { |
| 162 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint."); |
| 163 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 164 | ((cpRatchetJointcxx . . $ cpRatchetJoint# *)constraint)->phasecxx . . $ cpRatchetJoint#phase. = phase; |
| 165 | } |
| 166 | cpFloatcxx . . $ cpFloat# |
| 167 | cpRatchetJointGetRatchetcxx . . $ cpRatchetJointGetRatchet(9c3ac5f8d34d584e).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 168 | { |
| 169 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint."); |
| 170 | return ((cpRatchetJointcxx . . $ cpRatchetJoint# *)constraint)->ratchetcxx . . $ cpRatchetJoint#ratchet.; |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | cpRatchetJointSetRatchetcxx . . $ cpRatchetJointSetRatchet(dfce55250ba9e4fa).(cpConstraintcxx . . $ cpConstraint# *constraint, cpFloatcxx . . $ cpFloat# ratchet) |
| 175 | { |
| 176 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint."); |
| 177 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 178 | ((cpRatchetJointcxx . . $ cpRatchetJoint# *)constraint)->ratchetcxx . . $ cpRatchetJoint#ratchet. = ratchet; |
| 179 | } |