#include "chipmunk/chipmunk_private.h"
static void
preStep(cpPivotJoint *joint, cpFloat dt)
{
cpBody *a = joint->constraint.a;
cpBody *b = joint->constraint.b;
joint->r1 = cpTransformVect(a->transform, cpvsub(joint->anchorA, a->cog));
joint->r2 = cpTransformVect(b->transform, cpvsub(joint->anchorB, b->cog));
joint-> k = k_tensor(a, b, joint->r1, joint->r2);
cpVect delta = cpvsub(cpvadd(b->p, joint->r2), cpvadd(a->p, joint->r1));
joint->bias = cpvclamp(cpvmult(delta, -bias_coef(joint->constraint.errorBias, dt)/dt), joint->constraint.maxBias);
}
static void
applyCachedImpulse(cpPivotJoint *joint, cpFloat dt_coef)
{
cpBody *a = joint->constraint.a;
cpBody *b = joint->constraint.b;
apply_impulses(a, b, joint->r1, joint->r2, cpvmult(joint->jAcc, dt_coef));
}
static void
applyImpulse(cpPivotJoint *joint, cpFloat dt)
{
cpBody *a = joint->constraint.a;
cpBody *b = joint->constraint.b;
cpVect r1 = joint->r1;
cpVect r2 = joint->r2;
cpVect vr = relative_velocity(a, b, r1, r2);
cpVect j = cpMat2x2Transform(joint->k, cpvsub(joint->bias, vr));
cpVect jOld = joint->jAcc;
joint->jAcc = cpvclamp(cpvadd(joint->jAcc, j), joint->constraint.maxForce*dt);
j = cpvsub(joint->jAcc, jOld);
apply_impulses(a, b, joint->r1, joint->r2, j);
}
static cpFloat
getImpulse(cpConstraint *joint)
{
return cpvlength(((cpPivotJoint *)joint)->jAcc);
}
static const cpConstraintClass klass = {
(cpConstraintPreStepImpl)preStep,
(cpConstraintApplyCachedImpulseImpl)applyCachedImpulse,
(cpConstraintApplyImpulseImpl)applyImpulse,
(cpConstraintGetImpulseImpl)getImpulse,
};
cpPivotJoint *
cpPivotJointAlloc(void)
{
return (cpPivotJoint *)cpcalloc(1, sizeof(cpPivotJoint));
}
cpPivotJoint *
cpPivotJointInit(cpPivotJoint *joint, cpBody *a, cpBody *b, cpVect anchorA, cpVect anchorB)
{
cpConstraintInit((cpConstraint *)joint, &klass, a, b);
joint->anchorA = anchorA;
joint->anchorB = anchorB;
joint->jAcc = cpvzero;
return joint;
}
cpConstraint *
cpPivotJointNew2(cpBody *a, cpBody *b, cpVect anchorA, cpVect anchorB)
{
return (cpConstraint *)cpPivotJointInit(cpPivotJointAlloc(), a, b, anchorA, anchorB);
}
cpConstraint *
cpPivotJointNew(cpBody *a, cpBody *b, cpVect pivot)
{
cpVect anchorA = (a ? cpBodyWorldToLocal(a, pivot) : pivot);
cpVect anchorB = (b ? cpBodyWorldToLocal(b, pivot) : pivot);
return cpPivotJointNew2(a, b, anchorA, anchorB);
}
cpBool
cpConstraintIsPivotJoint(const cpConstraint *constraint)
{
return (constraint->klass == &klass);
}
cpVect
cpPivotJointGetAnchorA(const cpConstraint *constraint)
{
cpAssertHard(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint.");
return ((cpPivotJoint *)constraint)->anchorA;
}
void
cpPivotJointSetAnchorA(cpConstraint *constraint, cpVect anchorA)
{
cpAssertHard(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint.");
cpConstraintActivateBodies(constraint);
((cpPivotJoint *)constraint)->anchorA = anchorA;
}
cpVect
cpPivotJointGetAnchorB(const cpConstraint *constraint)
{
cpAssertHard(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint.");
return ((cpPivotJoint *)constraint)->anchorB;
}
void
cpPivotJointSetAnchorB(cpConstraint *constraint, cpVect anchorB)
{
cpAssertHard(cpConstraintIsPivotJoint(constraint), "Constraint is not a pivot joint.");
cpConstraintActivateBodies(constraint);
((cpPivotJoint *)constraint)->anchorB = anchorB;
}