| 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(aa3d8f97e6c84705).(cpPivotJointcxx . . $ cpPivotJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 26 | { |
| 27 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpPivotJoint#constraint..acxx . . $ cpConstraint#a.; |
| 28 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpPivotJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 29 | |
| 30 | joint->r1cxx . . $ cpPivotJoint#r1. = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(a->transformcxx . . $ cpBody#transform., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->anchorAcxx . . $ cpPivotJoint#anchorA., a->cogcxx . . $ cpBody#cog.)); |
| 31 | joint->r2cxx . . $ cpPivotJoint#r2. = cpTransformVectcxx . . $ cpTransformVect(7fc9e234cc6e750a).(b->transformcxx . . $ cpBody#transform., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->anchorBcxx . . $ cpPivotJoint#anchorB., b->cogcxx . . $ cpBody#cog.)); |
| 32 | |
| 33 | // Calculate mass tensor |
| 34 | joint-> kcxx . . $ cpPivotJoint#k. = k_tensorcxx . . $ k_tensor(df4b34f57e414a55).(a, b, joint->r1cxx . . $ cpPivotJoint#r1., joint->r2cxx . . $ cpPivotJoint#r2.); |
| 35 | |
| 36 | // calculate bias velocity |
| 37 | cpVectcxx . . $ cpVect# delta = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(b->pcxx . . $ cpBody#p., joint->r2cxx . . $ cpPivotJoint#r2.), cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(a->pcxx . . $ cpBody#p., joint->r1cxx . . $ cpPivotJoint#r1.)); |
| 38 | joint->biascxx . . $ cpPivotJoint#bias. = cpvclampcxx . . $ cpvclamp(f5cc44da17d64838).(cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(delta, -bias_coefcxx . . $ bias_coef(d8e7e0733fa41c87).(joint->constraintcxx . . $ cpPivotJoint#constraint..errorBiascxx . . $ cpConstraint#errorBias., dt)/dt), joint->constraintcxx . . $ cpPivotJoint#constraint..maxBiascxx . . $ cpConstraint#maxBias.); |
| 39 | } |
| 40 | |
| 41 | static void |
| 42 | applyCachedImpulsecxx . . $ applyCachedImpulse(aa3d8f97e6c84705).(cpPivotJointcxx . . $ cpPivotJoint# *joint, cpFloatcxx . . $ cpFloat# dt_coef) |
| 43 | { |
| 44 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpPivotJoint#constraint..acxx . . $ cpConstraint#a.; |
| 45 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpPivotJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 46 | |
| 47 | apply_impulsescxx . . $ apply_impulses(db940cdf68a6ac72).(a, b, joint->r1cxx . . $ cpPivotJoint#r1., joint->r2cxx . . $ cpPivotJoint#r2., cpvmultcxx . . $ cpvmult(f5cc44da17d64838).(joint->jAcccxx . . $ cpPivotJoint#jAcc., dt_coef)); |
| 48 | } |
| 49 | |
| 50 | static void |
| 51 | applyImpulsecxx . . $ applyImpulse(aa3d8f97e6c84705).(cpPivotJointcxx . . $ cpPivotJoint# *joint, cpFloatcxx . . $ cpFloat# dt) |
| 52 | { |
| 53 | cpBodycxx . . $ cpBody# *a = joint->constraintcxx . . $ cpPivotJoint#constraint..acxx . . $ cpConstraint#a.; |
| 54 | cpBodycxx . . $ cpBody# *b = joint->constraintcxx . . $ cpPivotJoint#constraint..bcxx . . $ cpConstraint#b.; |
| 55 | |
| 56 | cpVectcxx . . $ cpVect# r1 = joint->r1cxx . . $ cpPivotJoint#r1.; |
| 57 | cpVectcxx . . $ cpVect# r2 = joint->r2cxx . . $ cpPivotJoint#r2.; |
| 58 | |
| 59 | // compute relative velocity |
| 60 | cpVectcxx . . $ cpVect# vr = relative_velocitycxx . . $ relative_velocity(2ac5ebec3c2bf710).(a, b, r1, r2); |
| 61 | |
| 62 | // compute normal impulse |
| 63 | cpVectcxx . . $ cpVect# j = cpMat2x2Transformcxx . . $ cpMat2x2Transform(8e5c73201ddfef53).(joint->kcxx . . $ cpPivotJoint#k., cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->biascxx . . $ cpPivotJoint#bias., vr)); |
| 64 | cpVectcxx . . $ cpVect# jOld = joint->jAcccxx . . $ cpPivotJoint#jAcc.; |
| 65 | joint->jAcccxx . . $ cpPivotJoint#jAcc. = cpvclampcxx . . $ cpvclamp(f5cc44da17d64838).(cpvaddcxx . . $ cpvadd(47eb3f105675e23c).(joint->jAcccxx . . $ cpPivotJoint#jAcc., j), joint->constraintcxx . . $ cpPivotJoint#constraint..maxForcecxx . . $ cpConstraint#maxForce.*dt); |
| 66 | j = cpvsubcxx . . $ cpvsub(47eb3f105675e23c).(joint->jAcccxx . . $ cpPivotJoint#jAcc., jOld); |
| 67 | |
| 68 | // apply impulse |
| 69 | apply_impulsescxx . . $ apply_impulses(db940cdf68a6ac72).(a, b, joint->r1cxx . . $ cpPivotJoint#r1., joint->r2cxx . . $ cpPivotJoint#r2., j); |
| 70 | } |
| 71 | |
| 72 | static cpFloatcxx . . $ cpFloat# |
| 73 | getImpulsecxx . . $ getImpulse(4f5ff132f97724f3).(cpConstraintcxx . . $ cpConstraint# *joint) |
| 74 | { |
| 75 | return cpvlengthcxx . . $ cpvlength(c4ef53c170268e01).(((cpPivotJointcxx . . $ cpPivotJoint# *)joint)->jAcccxx . . $ cpPivotJoint#jAcc.); |
| 76 | } |
| 77 | |
| 78 | static const cpConstraintClasscxx . . $ cpConstraintClass# klasscxx . . $ klass. = { |
| 79 | (cpConstraintPreStepImplcxx . . $ cpConstraintPreStepImpl#)preStepcxx . . $ preStep(aa3d8f97e6c84705)., |
| 80 | (cpConstraintApplyCachedImpulseImplcxx . . $ cpConstraintApplyCachedImpulseImpl#)applyCachedImpulsecxx . . $ applyCachedImpulse(aa3d8f97e6c84705)., |
| 81 | (cpConstraintApplyImpulseImplcxx . . $ cpConstraintApplyImpulseImpl#)applyImpulsecxx . . $ applyImpulse(aa3d8f97e6c84705)., |
| 82 | (cpConstraintGetImpulseImplcxx . . $ cpConstraintGetImpulseImpl#)getImpulsecxx . . $ getImpulse(4f5ff132f97724f3)., |
| 83 | }; |
| 84 | |
| 85 | cpPivotJointcxx . . $ cpPivotJoint# * |
| 86 | cpPivotJointAlloccxx . . $ cpPivotJointAlloc(2b83ee7c2aa9ae84).(void) |
| 87 | { |
| 88 | return (cpPivotJointcxx . . $ cpPivotJoint# *)cpcalloccxx . . $ `include/chipmunk/chipmunk.h:72:10`!(1, sizeof(cpPivotJointcxx . . $ cpPivotJoint#)); |
| 89 | } |
| 90 | |
| 91 | cpPivotJointcxx . . $ cpPivotJoint# * |
| 92 | cpPivotJointInitcxx . . $ cpPivotJointInit(e46d329aeafd96a4).(cpPivotJointcxx . . $ cpPivotJoint# *joint, cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# anchorA, cpVectcxx . . $ cpVect# anchorB) |
| 93 | { |
| 94 | cpConstraintInitcxx . . $ cpConstraintInit(540fb6eaa94f170).((cpConstraintcxx . . $ cpConstraint# *)joint, &klasscxx . . $ klass., a, b); |
| 95 | |
| 96 | joint->anchorAcxx . . $ cpPivotJoint#anchorA. = anchorA; |
| 97 | joint->anchorBcxx . . $ cpPivotJoint#anchorB. = anchorB; |
| 98 | |
| 99 | joint->jAcccxx . . $ cpPivotJoint#jAcc. = cpvzerocxx . . $ cpvzero.; |
| 100 | |
| 101 | return joint; |
| 102 | } |
| 103 | |
| 104 | cpConstraintcxx . . $ cpConstraint# * |
| 105 | cpPivotJointNew2cxx . . $ cpPivotJointNew2(74f6d8fa0e302020).(cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# anchorA, cpVectcxx . . $ cpVect# anchorB) |
| 106 | { |
| 107 | return (cpConstraintcxx . . $ cpConstraint# *)cpPivotJointInitcxx . . $ cpPivotJointInit(e46d329aeafd96a4).(cpPivotJointAlloccxx . . $ cpPivotJointAlloc(2b83ee7c2aa9ae84).(), a, b, anchorA, anchorB); |
| 108 | } |
| 109 | |
| 110 | cpConstraintcxx . . $ cpConstraint# * |
| 111 | cpPivotJointNewcxx . . $ cpPivotJointNew(8cdc1c6d6b6f0abb).(cpBodycxx . . $ cpBody# *a, cpBodycxx . . $ cpBody# *b, cpVectcxx . . $ cpVect# pivot) |
| 112 | { |
| 113 | cpVectcxx . . $ cpVect# anchorA = (a ? cpBodyWorldToLocalcxx . . $ cpBodyWorldToLocal(4ff4900d49a97e73).(a, pivot) : pivot); |
| 114 | cpVectcxx . . $ cpVect# anchorB = (b ? cpBodyWorldToLocalcxx . . $ cpBodyWorldToLocal(4ff4900d49a97e73).(b, pivot) : pivot); |
| 115 | return cpPivotJointNew2cxx . . $ cpPivotJointNew2(74f6d8fa0e302020).(a, b, anchorA, anchorB); |
| 116 | } |
| 117 | |
| 118 | cpBoolcxx . . $ cpBool# |
| 119 | cpConstraintIsPivotJointcxx . . $ cpConstraintIsPivotJoint(ca4e56e976f68364).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 120 | { |
| 121 | return (constraint->klasscxx . . $ cpConstraint#klass. == &klasscxx . . $ klass.); |
| 122 | } |
| 123 | |
| 124 | cpVectcxx . . $ cpVect# |
| 125 | cpPivotJointGetAnchorAcxx . . $ cpPivotJointGetAnchorA(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 126 | { |
| 127 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint."); |
| 128 | return ((cpPivotJointcxx . . $ cpPivotJoint# *)constraint)->anchorAcxx . . $ cpPivotJoint#anchorA.; |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | cpPivotJointSetAnchorAcxx . . $ cpPivotJointSetAnchorA(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# anchorA) |
| 133 | { |
| 134 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint."); |
| 135 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 136 | ((cpPivotJointcxx . . $ cpPivotJoint# *)constraint)->anchorAcxx . . $ cpPivotJoint#anchorA. = anchorA; |
| 137 | } |
| 138 | |
| 139 | cpVectcxx . . $ cpVect# |
| 140 | cpPivotJointGetAnchorBcxx . . $ cpPivotJointGetAnchorB(faf0cf136ff270ad).(const cpConstraintcxx . . $ cpConstraint# *constraint) |
| 141 | { |
| 142 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint."); |
| 143 | return ((cpPivotJointcxx . . $ cpPivotJoint# *)constraint)->anchorBcxx . . $ cpPivotJoint#anchorB.; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | cpPivotJointSetAnchorBcxx . . $ cpPivotJointSetAnchorB(dec37a28d0c61656).(cpConstraintcxx . . $ cpConstraint# *constraint, cpVectcxx . . $ cpVect# anchorB) |
| 148 | { |
| 149 | cpAssertHardcxx . . $ `include/chipmunk/chipmunk.h:58:9`!(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint."); |
| 150 | cpConstraintActivateBodiescxx . . $ cpConstraintActivateBodies(ce362f6e378064c6).(constraint); |
| 151 | ((cpPivotJointcxx . . $ cpPivotJoint# *)constraint)->anchorBcxx . . $ cpPivotJoint#anchorB. = anchorB; |
| 152 | } |