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(cpPinJoint *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 cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
34 cpFloat dist = cpvlength(delta);
35 joint->n = cpvmult(delta, 1.0f/(dist ? dist : (cpFloat)INFINITY));
37 // calculate mass normal
38 joint->nMass = 1.0f/k_scalar(a, b, joint->r1, joint->r2, joint->n);
40 // calculate bias velocity
41 cpFloat maxBias = joint->constraint.maxBias;
42 joint->bias = cpfclamp(-bias_coef(joint->constraint.errorBias, dt)*(dist - joint->dist)/dt, -maxBias, maxBias);
46 applyCachedImpulse(cpPinJoint *joint, cpFloat dt_coef)
48 cpBody *a = joint->constraint.a;
49 cpBody *b = joint->constraint.b;
51 cpVect j = cpvmult(joint->n, joint->jnAcc*dt_coef);
52 apply_impulses(a, b, joint->r1, joint->r2, j);
56 applyImpulse(cpPinJoint *joint, cpFloat dt)
58 cpBody *a = joint->constraint.a;
59 cpBody *b = joint->constraint.b;
62 // compute relative velocity
63 cpFloat vrn = normal_relative_velocity(a, b, joint->r1, joint->r2, n);
65 cpFloat jnMax = joint->constraint.maxForce*dt;
67 // compute normal impulse
68 cpFloat jn = (joint->bias - vrn)*joint->nMass;
69 cpFloat jnOld = joint->jnAcc;
70 joint->jnAcc = cpfclamp(jnOld + jn, -jnMax, jnMax);
71 jn = joint->jnAcc - jnOld;
74 apply_impulses(a, b, joint->r1, joint->r2, cpvmult(n, jn));
78 getImpulse(cpPinJoint *joint)
80 return cpfabs(joint->jnAcc);
83 static const cpConstraintClass klass = {
84 (cpConstraintPreStepImpl)preStep,
85 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
86 (cpConstraintApplyImpulseImpl)applyImpulse,
87 (cpConstraintGetImpulseImpl)getImpulse,
94 return (cpPinJoint *)cpcalloc(1, sizeof(cpPinJoint));
98 cpPinJointInit(cpPinJoint *joint, cpBody *a, cpBody *b, cpVect anchorA, cpVect anchorB)
100 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
102 joint->anchorA = anchorA;
103 joint->anchorB = anchorB;
106 cpVect p1 = (a ? cpTransformPoint(a->transform, anchorA) : anchorA);
107 cpVect p2 = (b ? cpTransformPoint(b->transform, anchorB) : anchorB);
108 joint->dist = cpvlength(cpvsub(p2, p1));
110 cpAssertWarn(joint->dist > 0.0, "You created a 0 length pin joint. A pivot joint will be much more stable.");
118 cpPinJointNew(cpBody *a, cpBody *b, cpVect anchorA, cpVect anchorB)
120 return (cpConstraint *)cpPinJointInit(cpPinJointAlloc(), a, b, anchorA, anchorB);
124 cpConstraintIsPinJoint(const cpConstraint *constraint)
126 return (constraint->klass == &klass);
130 cpPinJointGetAnchorA(const cpConstraint *constraint)
132 cpAssertHard(cpConstraintIsPinJoint(constraint), "Constraint is not a pin joint.");
133 return ((cpPinJoint *)constraint)->anchorA;
137 cpPinJointSetAnchorA(cpConstraint *constraint, cpVect anchorA)
139 cpAssertHard(cpConstraintIsPinJoint(constraint), "Constraint is not a pin joint.");
140 cpConstraintActivateBodies(constraint);
141 ((cpPinJoint *)constraint)->anchorA = anchorA;
145 cpPinJointGetAnchorB(const cpConstraint *constraint)
147 cpAssertHard(cpConstraintIsPinJoint(constraint), "Constraint is not a pin joint.");
148 return ((cpPinJoint *)constraint)->anchorB;
152 cpPinJointSetAnchorB(cpConstraint *constraint, cpVect anchorB)
154 cpAssertHard(cpConstraintIsPinJoint(constraint), "Constraint is not a pin joint.");
155 cpConstraintActivateBodies(constraint);
156 ((cpPinJoint *)constraint)->anchorB = anchorB;
160 cpPinJointGetDist(const cpConstraint *constraint)
162 cpAssertHard(cpConstraintIsPinJoint(constraint), "Constraint is not a pin joint.");
163 return ((cpPinJoint *)constraint)->dist;
167 cpPinJointSetDist(cpConstraint *constraint, cpFloat dist)
169 cpAssertHard(cpConstraintIsPinJoint(constraint), "Constraint is not a pin joint.");
170 cpConstraintActivateBodies(constraint);
171 ((cpPinJoint *)constraint)->dist = dist;