using
System;
using
System.Diagnostics;
using
FarseerPhysics.Common;
using
Microsoft.Xna.Framework;
namespace
FarseerPhysics.Dynamics.Joints
{
/// <summary>
/// A distance joint contrains two points on two bodies
/// to remain at a fixed distance from each other. You can view
/// this as a massless, rigid rod.
/// </summary>
public
class
SliderJoint : Joint
{
public
Vector2 LocalAnchorA;
public
Vector2 LocalAnchorB;
private
float
_bias;
private
float
_gamma;
private
float
_impulse;
private
float
_mass;
private
Vector2 _u;
internal
SliderJoint()
{
JointType = JointType.Slider;
}
/// <summary>
/// Initializes a new instance of the <see cref="SliderJoint"/> class.
/// Warning: Do not use a zero or short length.
/// </summary>
/// <param name="bodyA">The first body.</param>
/// <param name="bodyB">The second body.</param>
/// <param name="localAnchorA">The first body anchor.</param>
/// <param name="localAnchorB">The second body anchor.</param>
/// <param name="minLength">The minimum length between anchorpoints</param>
/// <param name="maxlength">The maximum length between anchorpoints.</param>
public
SliderJoint(Body bodyA, Body bodyB, Vector2 localAnchorA, Vector2 localAnchorB,
float
minLength,
float
maxlength)
:
base
(bodyA, bodyB)
{
JointType = JointType.Slider;
LocalAnchorA = localAnchorA;
LocalAnchorB = localAnchorB;
MaxLength = maxlength;
MinLength = minLength;
}
/// <summary>
/// The maximum length between the anchor points.
/// </summary>
/// <value>The length.</value>
public
float
MaxLength {
get
;
set
; }
/// <summary>
/// The minimal length between the anchor points.
/// </summary>
/// <value>The length.</value>
public
float
MinLength {
get
;
set
; }
/// <summary>
/// The mass-spring-damper frequency in Hertz.
/// </summary>
/// <value>The frequency.</value>
public
float
Frequency {
get
;
set
; }
/// <summary>
/// The damping ratio. 0 = no damping, 1 = critical damping.
/// </summary>
/// <value>The damping ratio.</value>
public
float
DampingRatio {
get
;
set
; }
public
override
Vector2 WorldAnchorA
{
get
{
return
BodyA.GetWorldPoint(LocalAnchorA); }
}
public
override
Vector2 WorldAnchorB
{
get
{
return
BodyB.GetWorldPoint(LocalAnchorB); }
set
{ Debug.Assert(
false
,
"You can't set the world anchor on this joint type."
); }
}
public
override
Vector2 GetReactionForce(
float
inv_dt)
{
Vector2 F = (inv_dt * _impulse) * _u;
return
F;
}
public
override
float
GetReactionTorque(
float
inv_dt)
{
return
0.0f;
}
internal
override
void
InitVelocityConstraints(
ref
TimeStep step)
{
Body b1 = BodyA;
Body b2 = BodyB;
Transform xf1, xf2;
b1.GetTransform(
out
xf1);
b2.GetTransform(
out
xf2);
Vector2 r1 = MathUtils.Multiply(
ref
xf1.R, LocalAnchorA - b1.LocalCenter);
Vector2 r2 = MathUtils.Multiply(
ref
xf2.R, LocalAnchorB - b2.LocalCenter);
_u = b2.Sweep.C + r2 - b1.Sweep.C - r1;
float
length = _u.Length();
if
(length < MaxLength && length > MinLength)
{
return
;
}
if
(length > Settings.LinearSlop)
{
_u *= 1.0f / length;
}
else
{
_u = Vector2.Zero;
}
float
cr1u = MathUtils.Cross(r1, _u);
float
cr2u = MathUtils.Cross(r2, _u);
float
invMass = b1.InvMass + b1.InvI * cr1u * cr1u + b2.InvMass + b2.InvI * cr2u * cr2u;
Debug.Assert(invMass > Settings.Epsilon);
_mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
if
(Frequency > 0.0f)
{
float
C = length - MaxLength;
float
omega = 2.0f * Settings.Pi * Frequency;
float
d = 2.0f * _mass * DampingRatio * omega;
float
k = _mass * omega * omega;
_gamma = step.dt * (d + step.dt * k);
_gamma = _gamma != 0.0f ? 1.0f / _gamma : 0.0f;
_bias = C * step.dt * k * _gamma;
_mass = invMass + _gamma;
_mass = _mass != 0.0f ? 1.0f / _mass : 0.0f;
}
if
(Settings.EnableWarmstarting)
{
_impulse *= step.dtRatio;
Vector2 P = _impulse * _u;
b1.LinearVelocityInternal -= b1.InvMass * P;
b1.AngularVelocityInternal -= b1.InvI * MathUtils.Cross(r1, P);
b2.LinearVelocityInternal += b2.InvMass * P;
b2.AngularVelocityInternal += b2.InvI * MathUtils.Cross(r2, P);
}
else
{
_impulse = 0.0f;
}
}
internal
override
void
SolveVelocityConstraints(
ref
TimeStep step)
{
Body b1 = BodyA;
Body b2 = BodyB;
Transform xf1, xf2;
b1.GetTransform(
out
xf1);
b2.GetTransform(
out
xf2);
Vector2 r1 = MathUtils.Multiply(
ref
xf1.R, LocalAnchorA - b1.LocalCenter);
Vector2 r2 = MathUtils.Multiply(
ref
xf2.R, LocalAnchorB - b2.LocalCenter);
Vector2 d = b2.Sweep.C + r2 - b1.Sweep.C - r1;
float
length = d.Length();
if
(length < MaxLength && length > MinLength)
{
return
;
}
Vector2 v1 = b1.LinearVelocityInternal + MathUtils.Cross(b1.AngularVelocityInternal, r1);
Vector2 v2 = b2.LinearVelocityInternal + MathUtils.Cross(b2.AngularVelocityInternal, r2);
float
Cdot = Vector2.Dot(_u, v2 - v1);
float
impulse = -_mass * (Cdot + _bias + _gamma * _impulse);
_impulse += impulse;
Vector2 P = impulse * _u;
b1.LinearVelocityInternal -= b1.InvMass * P;
b1.AngularVelocityInternal -= b1.InvI * MathUtils.Cross(r1, P);
b2.LinearVelocityInternal += b2.InvMass * P;
b2.AngularVelocityInternal += b2.InvI * MathUtils.Cross(r2, P);
}
internal
override
bool
SolvePositionConstraints()
{
if
(Frequency > 0.0f)
{
return
true
;
}
Body b1 = BodyA;
Body b2 = BodyB;
Transform xf1, xf2;
b1.GetTransform(
out
xf1);
b2.GetTransform(
out
xf2);
Vector2 r1 = MathUtils.Multiply(
ref
xf1.R, LocalAnchorA - b1.LocalCenter);
Vector2 r2 = MathUtils.Multiply(
ref
xf2.R, LocalAnchorB - b2.LocalCenter);
Vector2 d = b2.Sweep.C + r2 - b1.Sweep.C - r1;
float
length = d.Length();
if
(length < MaxLength && length > MinLength)
{
return
true
;
}
if
(length == 0.0f)
return
true
;
d /= length;
float
C = length - MaxLength;
C = MathUtils.Clamp(C, -Settings.MaxLinearCorrection, Settings.MaxLinearCorrection);
float
impulse = -_mass * C;
_u = d;
Vector2 P = impulse * _u;
b1.Sweep.C -= b1.InvMass * P;
b1.Sweep.A -= b1.InvI * MathUtils.Cross(r1, P);
b2.Sweep.C += b2.InvMass * P;
b2.Sweep.A += b2.InvI * MathUtils.Cross(r2, P);
b1.SynchronizeTransform();
b2.SynchronizeTransform();
return
Math.Abs(C) < Settings.LinearSlop;
}
}
}