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(cpRatchetJoint *joint, cpFloat dt)
27 cpBody *a = joint->constraint.a;
28 cpBody *b = joint->constraint.b;
30 cpFloat angle = joint->angle;
31 cpFloat phase = joint->phase;
32 cpFloat ratchet = joint->ratchet;
34 cpFloat delta = b->a - a->a;
35 cpFloat diff = angle - delta;
38 if(diff*ratchet > 0.0f){
41 joint->angle = cpffloor((delta - phase)/ratchet)*ratchet + phase;
44 // calculate moment of inertia coefficient.
45 joint->iSum = 1.0f/(a->i_inv + b->i_inv);
47 // calculate bias velocity
48 cpFloat maxBias = joint->constraint.maxBias;
49 joint->bias = cpfclamp(-bias_coef(joint->constraint.errorBias, dt)*pdist/dt, -maxBias, maxBias);
51 // If the bias is 0, the joint is not at a limit. Reset the impulse.
52 if(!joint->bias) joint->jAcc = 0.0f;
56 applyCachedImpulse(cpRatchetJoint *joint, cpFloat dt_coef)
58 cpBody *a = joint->constraint.a;
59 cpBody *b = joint->constraint.b;
61 cpFloat j = joint->jAcc*dt_coef;
67 applyImpulse(cpRatchetJoint *joint, cpFloat dt)
69 if(!joint->bias) return; // early exit
71 cpBody *a = joint->constraint.a;
72 cpBody *b = joint->constraint.b;
74 // compute relative rotational velocity
75 cpFloat wr = b->w - a->w;
76 cpFloat ratchet = joint->ratchet;
78 cpFloat jMax = joint->constraint.maxForce*dt;
80 // compute normal impulse
81 cpFloat j = -(joint->bias + wr)*joint->iSum;
82 cpFloat jOld = joint->jAcc;
83 joint->jAcc = cpfclamp((jOld + j)*ratchet, 0.0f, jMax*cpfabs(ratchet))/ratchet;
84 j = joint->jAcc - jOld;
92 getImpulse(cpRatchetJoint *joint)
94 return cpfabs(joint->jAcc);
97 static const cpConstraintClass klass = {
98 (cpConstraintPreStepImpl)preStep,
99 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
100 (cpConstraintApplyImpulseImpl)applyImpulse,
101 (cpConstraintGetImpulseImpl)getImpulse,
105 cpRatchetJointAlloc(void)
107 return (cpRatchetJoint *)cpcalloc(1, sizeof(cpRatchetJoint));
111 cpRatchetJointInit(cpRatchetJoint *joint, cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
113 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
116 joint->phase = phase;
117 joint->ratchet = ratchet;
120 joint->angle = (b ? b->a : 0.0f) - (a ? a->a : 0.0f);
126 cpRatchetJointNew(cpBody *a, cpBody *b, cpFloat phase, cpFloat ratchet)
128 return (cpConstraint *)cpRatchetJointInit(cpRatchetJointAlloc(), a, b, phase, ratchet);
132 cpConstraintIsRatchetJoint(const cpConstraint *constraint)
134 return (constraint->klass == &klass);
138 cpRatchetJointGetAngle(const cpConstraint *constraint)
140 cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
141 return ((cpRatchetJoint *)constraint)->angle;
145 cpRatchetJointSetAngle(cpConstraint *constraint, cpFloat angle)
147 cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
148 cpConstraintActivateBodies(constraint);
149 ((cpRatchetJoint *)constraint)->angle = angle;
153 cpRatchetJointGetPhase(const cpConstraint *constraint)
155 cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
156 return ((cpRatchetJoint *)constraint)->phase;
160 cpRatchetJointSetPhase(cpConstraint *constraint, cpFloat phase)
162 cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
163 cpConstraintActivateBodies(constraint);
164 ((cpRatchetJoint *)constraint)->phase = phase;
167 cpRatchetJointGetRatchet(const cpConstraint *constraint)
169 cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
170 return ((cpRatchetJoint *)constraint)->ratchet;
174 cpRatchetJointSetRatchet(cpConstraint *constraint, cpFloat ratchet)
176 cpAssertHard(cpConstraintIsRatchetJoint(constraint), "Constraint is not a ratchet joint.");
177 cpConstraintActivateBodies(constraint);
178 ((cpRatchetJoint *)constraint)->ratchet = ratchet;