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(cpRotaryLimitJoint *joint, cpFloat dt)
27 cpBody *a = joint->constraint.a;
28 cpBody *b = joint->constraint.b;
30 cpFloat dist = b->a - a->a;
32 if(dist > joint->max) {
33 pdist = joint->max - dist;
34 } else if(dist < joint->min) {
35 pdist = joint->min - dist;
38 // calculate moment of inertia coefficient.
39 joint->iSum = 1.0f/(a->i_inv + b->i_inv);
41 // calculate bias velocity
42 cpFloat maxBias = joint->constraint.maxBias;
43 joint->bias = cpfclamp(-bias_coef(joint->constraint.errorBias, dt)*pdist/dt, -maxBias, maxBias);
45 // If the bias is 0, the joint is not at a limit. Reset the impulse.
46 if(!joint->bias) joint->jAcc = 0.0f;
50 applyCachedImpulse(cpRotaryLimitJoint *joint, cpFloat dt_coef)
52 cpBody *a = joint->constraint.a;
53 cpBody *b = joint->constraint.b;
55 cpFloat j = joint->jAcc*dt_coef;
61 applyImpulse(cpRotaryLimitJoint *joint, cpFloat dt)
63 if(!joint->bias) return; // early exit
65 cpBody *a = joint->constraint.a;
66 cpBody *b = joint->constraint.b;
68 // compute relative rotational velocity
69 cpFloat wr = b->w - a->w;
71 cpFloat jMax = joint->constraint.maxForce*dt;
73 // compute normal impulse
74 cpFloat j = -(joint->bias + wr)*joint->iSum;
75 cpFloat jOld = joint->jAcc;
76 if(joint->bias < 0.0f){
77 joint->jAcc = cpfclamp(jOld + j, 0.0f, jMax);
79 joint->jAcc = cpfclamp(jOld + j, -jMax, 0.0f);
81 j = joint->jAcc - jOld;
89 getImpulse(cpRotaryLimitJoint *joint)
91 return cpfabs(joint->jAcc);
94 static const cpConstraintClass klass = {
95 (cpConstraintPreStepImpl)preStep,
96 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
97 (cpConstraintApplyImpulseImpl)applyImpulse,
98 (cpConstraintGetImpulseImpl)getImpulse,
102 cpRotaryLimitJointAlloc(void)
104 return (cpRotaryLimitJoint *)cpcalloc(1, sizeof(cpRotaryLimitJoint));
108 cpRotaryLimitJointInit(cpRotaryLimitJoint *joint, cpBody *a, cpBody *b, cpFloat min, cpFloat max)
110 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
121 cpRotaryLimitJointNew(cpBody *a, cpBody *b, cpFloat min, cpFloat max)
123 return (cpConstraint *)cpRotaryLimitJointInit(cpRotaryLimitJointAlloc(), a, b, min, max);
127 cpConstraintIsRotaryLimitJoint(const cpConstraint *constraint)
129 return (constraint->klass == &klass);
133 cpRotaryLimitJointGetMin(const cpConstraint *constraint)
135 cpAssertHard(cpConstraintIsRotaryLimitJoint(constraint), "Constraint is not a rotary limit joint.");
136 return ((cpRotaryLimitJoint *)constraint)->min;
140 cpRotaryLimitJointSetMin(cpConstraint *constraint, cpFloat min)
142 cpAssertHard(cpConstraintIsRotaryLimitJoint(constraint), "Constraint is not a rotary limit joint.");
143 cpConstraintActivateBodies(constraint);
144 ((cpRotaryLimitJoint *)constraint)->min = min;
148 cpRotaryLimitJointGetMax(const cpConstraint *constraint)
150 cpAssertHard(cpConstraintIsRotaryLimitJoint(constraint), "Constraint is not a rotary limit joint.");
151 return ((cpRotaryLimitJoint *)constraint)->max;
155 cpRotaryLimitJointSetMax(cpConstraint *constraint, cpFloat max)
157 cpAssertHard(cpConstraintIsRotaryLimitJoint(constraint), "Constraint is not a rotary limit joint.");
158 cpConstraintActivateBodies(constraint);
159 ((cpRotaryLimitJoint *)constraint)->max = max;