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(cpSlideJoint *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);
36 if(dist > joint->max) {
37 pdist = dist - joint->max;
38 joint->n = cpvnormalize(delta);
39 } else if(dist < joint->min) {
40 pdist = joint->min - dist;
41 joint->n = cpvneg(cpvnormalize(delta));
47 // calculate mass normal
48 joint->nMass = 1.0f/k_scalar(a, b, joint->r1, joint->r2, joint->n);
50 // calculate bias velocity
51 cpFloat maxBias = joint->constraint.maxBias;
52 joint->bias = cpfclamp(-bias_coef(joint->constraint.errorBias, dt)*pdist/dt, -maxBias, maxBias);
56 applyCachedImpulse(cpSlideJoint *joint, cpFloat dt_coef)
58 cpBody *a = joint->constraint.a;
59 cpBody *b = joint->constraint.b;
61 cpVect j = cpvmult(joint->n, joint->jnAcc*dt_coef);
62 apply_impulses(a, b, joint->r1, joint->r2, j);
66 applyImpulse(cpSlideJoint *joint, cpFloat dt)
68 if(cpveql(joint->n, cpvzero)) return; // early exit
70 cpBody *a = joint->constraint.a;
71 cpBody *b = joint->constraint.b;
74 cpVect r1 = joint->r1;
75 cpVect r2 = joint->r2;
77 // compute relative velocity
78 cpVect vr = relative_velocity(a, b, r1, r2);
79 cpFloat vrn = cpvdot(vr, n);
81 // compute normal impulse
82 cpFloat jn = (joint->bias - vrn)*joint->nMass;
83 cpFloat jnOld = joint->jnAcc;
84 joint->jnAcc = cpfclamp(jnOld + jn, -joint->constraint.maxForce*dt, 0.0f);
85 jn = joint->jnAcc - jnOld;
88 apply_impulses(a, b, joint->r1, joint->r2, cpvmult(n, jn));
92 getImpulse(cpConstraint *joint)
94 return cpfabs(((cpSlideJoint *)joint)->jnAcc);
97 static const cpConstraintClass klass = {
98 (cpConstraintPreStepImpl)preStep,
99 (cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
100 (cpConstraintApplyImpulseImpl)applyImpulse,
101 (cpConstraintGetImpulseImpl)getImpulse,
105 cpSlideJointAlloc(void)
107 return (cpSlideJoint *)cpcalloc(1, sizeof(cpSlideJoint));
111 cpSlideJointInit(cpSlideJoint *joint, cpBody *a, cpBody *b, cpVect anchorA, cpVect anchorB, cpFloat min, cpFloat max)
113 cpConstraintInit((cpConstraint *)joint, &klass, a, b);
115 joint->anchorA = anchorA;
116 joint->anchorB = anchorB;
126 cpSlideJointNew(cpBody *a, cpBody *b, cpVect anchorA, cpVect anchorB, cpFloat min, cpFloat max)
128 return (cpConstraint *)cpSlideJointInit(cpSlideJointAlloc(), a, b, anchorA, anchorB, min, max);
132 cpConstraintIsSlideJoint(const cpConstraint *constraint)
134 return (constraint->klass == &klass);
138 cpSlideJointGetAnchorA(const cpConstraint *constraint)
140 cpAssertHard(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint.");
141 return ((cpSlideJoint *)constraint)->anchorA;
145 cpSlideJointSetAnchorA(cpConstraint *constraint, cpVect anchorA)
147 cpAssertHard(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint.");
148 cpConstraintActivateBodies(constraint);
149 ((cpSlideJoint *)constraint)->anchorA = anchorA;
153 cpSlideJointGetAnchorB(const cpConstraint *constraint)
155 cpAssertHard(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint.");
156 return ((cpSlideJoint *)constraint)->anchorB;
160 cpSlideJointSetAnchorB(cpConstraint *constraint, cpVect anchorB)
162 cpAssertHard(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint.");
163 cpConstraintActivateBodies(constraint);
164 ((cpSlideJoint *)constraint)->anchorB = anchorB;
168 cpSlideJointGetMin(const cpConstraint *constraint)
170 cpAssertHard(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint.");
171 return ((cpSlideJoint *)constraint)->min;
175 cpSlideJointSetMin(cpConstraint *constraint, cpFloat min)
177 cpAssertHard(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint.");
178 cpConstraintActivateBodies(constraint);
179 ((cpSlideJoint *)constraint)->min = min;
183 cpSlideJointGetMax(const cpConstraint *constraint)
185 cpAssertHard(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint.");
186 return ((cpSlideJoint *)constraint)->max;
190 cpSlideJointSetMax(cpConstraint *constraint, cpFloat max)
192 cpAssertHard(cpConstraintIsSlideJoint(constraint), "Constraint is not a slide joint.");
193 cpConstraintActivateBodies(constraint);
194 ((cpSlideJoint *)constraint)->max = max;