1 /* Copyright (c) 2013 Scott Lembcke and Howling Moon Software
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:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
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
22 #include "chipmunk/chipmunk_private.h"
25 preStep(cpGearJoint *joint, cpFloat dt)
27 cpBody *a = joint->constraint.a;
28 cpBody *b = joint->constraint.b;
30 // calculate moment of inertia coefficient.
31 joint->iSum = 1.0f/(a->i_inv*joint->ratio_inv + joint->ratio*b->i_inv);
33 // calculate bias velocity
34 cpFloat maxBias = joint->constraint.maxBias;
35 joint->bias = cpfclamp(-bias_coef(joint->constraint.errorBias, dt)*(b->a*joint->ratio - a->a - joint->phase)/dt, -maxBias, maxBias);
39 applyCachedImpulse(cpGearJoint *joint, cpFloat dt_coef)
41 cpBody *a = joint->constraint.a;
42 cpBody *b = joint->constraint.b;
44 cpFloat j = joint->jAcc*dt_coef;
45 a->w -= j*a->i_inv*joint->ratio_inv;
50 applyImpulse(cpGearJoint *joint, cpFloat dt)
52 cpBody *a = joint->constraint.a;
53 cpBody *b = joint->constraint.b;
55 // compute relative rotational velocity
56 cpFloat wr = b->w*joint->ratio - a->w;
58 cpFloat jMax = joint->constraint.maxForce*dt;
60 // compute normal impulse
61 cpFloat j = (joint->bias - wr)*joint->iSum;
62 cpFloat jOld = joint->jAcc;
63 joint->jAcc = cpfclamp(jOld + j, -jMax, jMax);
64 j = joint->jAcc - jOld;
67 a->w -= j*a->i_inv*joint->ratio_inv;
72 getImpulse(cpGearJoint *joint)
74 return cpfabs(joint->jAcc);
77 static const cpConstraintClass klass = {
78 (cpConstraintPreStepImpl)preStep,
79 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
80 (cpConstraintApplyImpulseImpl)applyImpulse,
81 (cpConstraintGetImpulseImpl)getImpulse,
85 cpGearJointAlloc(void)
87 return (cpGearJoint *)cpcalloc(1, sizeof(cpGearJoint));
91 cpGearJointInit(cpGearJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratio)
93 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
97 joint->ratio_inv = 1.0f/ratio;
105 cpGearJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratio)
107 return (cpConstraint *)cpGearJointInit(cpGearJointAlloc(), a, b, phase, ratio);
111 cpConstraintIsGearJoint(const cpConstraint *constraint)
113 return (constraint->klass == &klass);
117 cpGearJointGetPhase(const cpConstraint *constraint)
119 cpAssertHard(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint.");
120 return ((cpGearJoint *)constraint)->phase;
124 cpGearJointSetPhase(cpConstraint *constraint, cpFloat phase)
126 cpAssertHard(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint.");
127 cpConstraintActivateBodies(constraint);
128 ((cpGearJoint *)constraint)->phase = phase;
132 cpGearJointGetRatio(const cpConstraint *constraint)
134 cpAssertHard(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint.");
135 return ((cpGearJoint *)constraint)->ratio;
139 cpGearJointSetRatio(cpConstraint *constraint, cpFloat ratio)
141 cpAssertHard(cpConstraintIsGearJoint(constraint), "Constraint is not a ratchet joint.");
142 cpConstraintActivateBodies(constraint);
143 ((cpGearJoint *)constraint)->ratio = ratio;
144 ((cpGearJoint *)constraint)->ratio_inv = 1.0f/ratio;