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(cpSimpleMotor *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 + b->i_inv);
35 applyCachedImpulse(cpSimpleMotor *joint, cpFloat dt_coef)
37 cpBody *a = joint->constraint.a;
38 cpBody *b = joint->constraint.b;
40 cpFloat j = joint->jAcc*dt_coef;
46 applyImpulse(cpSimpleMotor *joint, cpFloat dt)
48 cpBody *a = joint->constraint.a;
49 cpBody *b = joint->constraint.b;
51 // compute relative rotational velocity
52 cpFloat wr = b->w - a->w + joint->rate;
54 cpFloat jMax = joint->constraint.maxForce*dt;
56 // compute normal impulse
57 cpFloat j = -wr*joint->iSum;
58 cpFloat jOld = joint->jAcc;
59 joint->jAcc = cpfclamp(jOld + j, -jMax, jMax);
60 j = joint->jAcc - jOld;
68 getImpulse(cpSimpleMotor *joint)
70 return cpfabs(joint->jAcc);
73 static const cpConstraintClass klass = {
74 (cpConstraintPreStepImpl)preStep,
75 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
76 (cpConstraintApplyImpulseImpl)applyImpulse,
77 (cpConstraintGetImpulseImpl)getImpulse,
81 cpSimpleMotorAlloc(void)
83 return (cpSimpleMotor *)cpcalloc(1, sizeof(cpSimpleMotor));
87 cpSimpleMotorInit(cpSimpleMotor *joint, cpBody *a, cpBody *b, cpFloat rate)
89 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
99 cpSimpleMotorNew(cpBody *a, cpBody *b, cpFloat rate)
101 return (cpConstraint *)cpSimpleMotorInit(cpSimpleMotorAlloc(), a, b, rate);
105 cpConstraintIsSimpleMotor(const cpConstraint *constraint)
107 return (constraint->klass == &klass);
111 cpSimpleMotorGetRate(const cpConstraint *constraint)
113 cpAssertHard(cpConstraintIsSimpleMotor(constraint), "Constraint is not a pin joint.");
114 return ((cpSimpleMotor *)constraint)->rate;
118 cpSimpleMotorSetRate(cpConstraint *constraint, cpFloat rate)
120 cpAssertHard(cpConstraintIsSimpleMotor(constraint), "Constraint is not a pin joint.");
121 cpConstraintActivateBodies(constraint);
122 ((cpSimpleMotor *)constraint)->rate = rate;