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(cpPivotJoint *joint, cpFloat dt)
27 cpBody *a = joint->constraint.a;
28 cpBody *b = joint->constraint.b;
30 joint->r1 = cpTransformVect(a->transform, cpvsub(joint->anchorA, a->cog));
31 joint->r2 = cpTransformVect(b->transform, cpvsub(joint->anchorB, b->cog));
33 // Calculate mass tensor
34 joint-> k = k_tensor(a, b, joint->r1, joint->r2);
36 // calculate bias velocity
37 cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
38 joint->bias = cpvclamp(cpvmult(delta, -bias_coef(joint->constraint.errorBias, dt)/dt), joint->constraint.maxBias);
42 applyCachedImpulse(cpPivotJoint *joint, cpFloat dt_coef)
44 cpBody *a = joint->constraint.a;
45 cpBody *b = joint->constraint.b;
47 apply_impulses(a, b, joint->r1, joint->r2, cpvmult(joint->jAcc, dt_coef));
51 applyImpulse(cpPivotJoint *joint, cpFloat dt)
53 cpBody *a = joint->constraint.a;
54 cpBody *b = joint->constraint.b;
56 cpVect r1 = joint->r1;
57 cpVect r2 = joint->r2;
59 // compute relative velocity
60 cpVect vr = relative_velocity(a, b, r1, r2);
62 // compute normal impulse
63 cpVect j = cpMat2x2Transform(joint->k, cpvsub(joint->bias, vr));
64 cpVect jOld = joint->jAcc;
65 joint->jAcc = cpvclamp(cpvadd(joint->jAcc, j), joint->constraint.maxForce*dt);
66 j = cpvsub(joint->jAcc, jOld);
69 apply_impulses(a, b, joint->r1, joint->r2, j);
73 getImpulse(cpConstraint *joint)
75 return cpvlength(((cpPivotJoint *)joint)->jAcc);
78 static const cpConstraintClass klass = {
79 (cpConstraintPreStepImpl)preStep,
80 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
81 (cpConstraintApplyImpulseImpl)applyImpulse,
82 (cpConstraintGetImpulseImpl)getImpulse,
86 cpPivotJointAlloc(void)
88 return (cpPivotJoint *)cpcalloc(1, sizeof(cpPivotJoint));
92 cpPivotJointInit(cpPivotJoint *joint, cpBody *a, cpBody *b, cpVect anchorA, cpVect anchorB)
94 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
96 joint->anchorA = anchorA;
97 joint->anchorB = anchorB;
99 joint->jAcc = cpvzero;
105 cpPivotJointNew2(cpBody *a, cpBody *b, cpVect anchorA, cpVect anchorB)
107 return (cpConstraint *)cpPivotJointInit(cpPivotJointAlloc(), a, b, anchorA, anchorB);
111 cpPivotJointNew(cpBody *a, cpBody *b, cpVect pivot)
113 cpVect anchorA = (a ? cpBodyWorldToLocal(a, pivot) : pivot);
114 cpVect anchorB = (b ? cpBodyWorldToLocal(b, pivot) : pivot);
115 return cpPivotJointNew2(a, b, anchorA, anchorB);
119 cpConstraintIsPivotJoint(const cpConstraint *constraint)
121 return (constraint->klass == &klass);
125 cpPivotJointGetAnchorA(const cpConstraint *constraint)
127 cpAssertHard(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint.");
128 return ((cpPivotJoint *)constraint)->anchorA;
132 cpPivotJointSetAnchorA(cpConstraint *constraint, cpVect anchorA)
134 cpAssertHard(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint.");
135 cpConstraintActivateBodies(constraint);
136 ((cpPivotJoint *)constraint)->anchorA = anchorA;
140 cpPivotJointGetAnchorB(const cpConstraint *constraint)
142 cpAssertHard(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint.");
143 return ((cpPivotJoint *)constraint)->anchorB;
147 cpPivotJointSetAnchorB(cpConstraint *constraint, cpVect anchorB)
149 cpAssertHard(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint.");
150 cpConstraintActivateBodies(constraint);
151 ((cpPivotJoint *)constraint)->anchorB = anchorB;