123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
-
- using System;
- using System.Diagnostics;
- using Microsoft.Xna.Framework;
- namespace Box2D.XNA
- {
-
-
-
-
-
-
- public class DistanceJointDef : JointDef
- {
- public DistanceJointDef()
- {
- type = JointType.Distance;
- localAnchorA = new Vector2(0.0f, 0.0f);
- localAnchorB = new Vector2(0.0f, 0.0f);
- length = 1.0f;
- frequencyHz = 0.0f;
- dampingRatio = 0.0f;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public void Initialize(Body b1, Body b2,
- Vector2 anchor1, Vector2 anchor2)
- {
- bodyA = b1;
- bodyB = b2;
- localAnchorA = bodyA.GetLocalPoint(anchor1);
- localAnchorB = bodyB.GetLocalPoint(anchor2);
- Vector2 d = anchor2 - anchor1;
- length = d.Length();
- }
-
- public Vector2 localAnchorA;
-
- public Vector2 localAnchorB;
-
- public float length;
-
- public float frequencyHz;
-
- public float dampingRatio;
- };
-
-
-
- public class DistanceJoint : Joint
- {
-
-
- public void SetLength(float length)
- {
- _length = length;
- }
- public float GetLength()
- {
- return _length;
- }
-
- public void SetFrequency(float hz)
- {
- _frequencyHz = hz;
- }
- public float GetFrequency()
- {
- return _frequencyHz;
- }
-
- public void SetDampingRatio(float ratio)
- {
- _dampingRatio = ratio;
- }
- public float GetDampingRatio()
- {
- return _dampingRatio;
- }
- public override Vector2 GetAnchorA()
- {
- return _bodyA.GetWorldPoint(_localAnchor1);
- }
- public override Vector2 GetAnchorB()
- {
- return _bodyB.GetWorldPoint(_localAnchor2);
- }
- 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 DistanceJoint(DistanceJointDef def)
- : base(def)
- {
- _localAnchor1 = def.localAnchorA;
- _localAnchor2 = def.localAnchorB;
- _length = def.length;
- _frequencyHz = def.frequencyHz;
- _dampingRatio = def.dampingRatio;
- _impulse = 0.0f;
- _gamma = 0.0f;
- _bias = 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, _localAnchor1 - b1.GetLocalCenter());
- Vector2 r2 = MathUtils.Multiply(ref xf2.R, _localAnchor2 - b2.GetLocalCenter());
- _u = b2._sweep.c + r2 - b1._sweep.c - r1;
-
- float length = _u.Length();
- if (length > Settings.b2_linearSlop)
- {
- _u *= 1.0f / length;
- }
- else
- {
- _u = new Vector2(0.0f, 0.0f);
- }
- 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.b2_epsilon);
- _mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
- if (_frequencyHz > 0.0f)
- {
- float C = length - _length;
-
- float omega = 2.0f * Settings.b2_pi * _frequencyHz;
-
- 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 (step.warmStarting)
- {
-
- _impulse *= step.dtRatio;
- Vector2 P = _impulse * _u;
- b1._linearVelocity -= b1._invMass * P;
- b1._angularVelocity -= b1._invI * MathUtils.Cross(r1, P);
- b2._linearVelocity += b2._invMass * P;
- b2._angularVelocity += 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, _localAnchor1 - b1.GetLocalCenter());
- Vector2 r2 = MathUtils.Multiply(ref xf2.R, _localAnchor2 - b2.GetLocalCenter());
-
- Vector2 v1 = b1._linearVelocity + MathUtils.Cross(b1._angularVelocity, r1);
- Vector2 v2 = b2._linearVelocity + MathUtils.Cross(b2._angularVelocity, r2);
- float Cdot = Vector2.Dot(_u, v2 - v1);
- float impulse = -_mass * (Cdot + _bias + _gamma * _impulse);
- _impulse += impulse;
- Vector2 P = impulse * _u;
- b1._linearVelocity -= b1._invMass * P;
- b1._angularVelocity -= b1._invI * MathUtils.Cross(r1, P);
- b2._linearVelocity += b2._invMass * P;
- b2._angularVelocity += b2._invI * MathUtils.Cross(r2, P);
- }
- internal override bool SolvePositionConstraints(float baumgarte)
- {
- if (_frequencyHz > 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, _localAnchor1 - b1.GetLocalCenter());
- Vector2 r2 = MathUtils.Multiply(ref xf2.R, _localAnchor2 - b2.GetLocalCenter());
- Vector2 d = b2._sweep.c + r2 - b1._sweep.c - r1;
- float length = d.Length();
- if (length == 0.0f)
- return true;
- d /= length;
- float C = length - _length;
- C = MathUtils.Clamp(C, -Settings.b2_maxLinearCorrection, Settings.b2_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.b2_linearSlop;
- }
- internal Vector2 _localAnchor1;
- internal Vector2 _localAnchor2;
- internal Vector2 _u;
- internal float _frequencyHz;
- internal float _dampingRatio;
- internal float _gamma;
- internal float _bias;
- internal float _impulse;
- internal float _mass;
- internal float _length;
- };
- }
|