123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
-
- using System.Diagnostics;
- using Microsoft.Xna.Framework;
- namespace Box2D.XNA
- {
- public class MouseJointDef : JointDef
- {
- public MouseJointDef()
- {
- type = JointType.Mouse;
- target = new Vector2(0.0f, 0.0f);
- maxForce = 0.0f;
- frequencyHz = 5.0f;
- dampingRatio = 0.7f;
- }
-
-
- public Vector2 target;
-
-
-
- public float maxForce;
-
- public float frequencyHz;
-
- public float dampingRatio;
- };
- public class MouseJoint : Joint
- {
-
- public override Vector2 GetAnchorA()
- {
- return _target;
- }
-
- public override Vector2 GetAnchorB()
- {
- return _bodyB.GetWorldPoint(_localAnchor);
- }
-
- public override Vector2 GetReactionForce(float inv_dt)
- {
- return inv_dt * _impulse;
- }
-
- public override float GetReactionTorque(float inv_dt)
- {
- return inv_dt * 0.0f;
- }
-
- public void SetTarget(Vector2 target)
- {
- if (_bodyB.IsAwake() == false)
- {
- _bodyB.SetAwake(true);
- }
- _target = target;
- }
- public Vector2 GetTarget()
- {
- return _target;
- }
-
- public void SetMaxForce(float force)
- {
- _maxForce = force;
- }
- public float GetMaxForce()
- {
- return _maxForce;
- }
-
- public void SetFrequency(float hz)
- {
- _frequencyHz = hz;
- }
- public float GetFrequency()
- {
- return _frequencyHz;
- }
-
- public void SetDampingRatio(float ratio)
- {
- _dampingRatio = ratio;
- }
- public float GetDampingRatio()
- {
- return _dampingRatio;
- }
- internal MouseJoint(MouseJointDef def)
- : base(def)
- {
- Debug.Assert(def.target.IsValid());
- Debug.Assert(MathUtils.IsValid(def.maxForce) && def.maxForce >= 0.0f);
- Debug.Assert(MathUtils.IsValid(def.frequencyHz) && def.frequencyHz >= 0.0f);
- Debug.Assert(MathUtils.IsValid(def.dampingRatio) && def.dampingRatio >= 0.0f);
- Transform xf1;
- _bodyB.GetTransform(out xf1);
- _target = def.target;
- _localAnchor = MathUtils.MultiplyT(ref xf1, _target);
- _maxForce = def.maxForce;
- _impulse = Vector2.Zero;
- _frequencyHz = def.frequencyHz;
- _dampingRatio = def.dampingRatio;
- _beta = 0.0f;
- _gamma = 0.0f;
- }
- internal override void InitVelocityConstraints(ref TimeStep step)
- {
- Body b = _bodyB;
- float mass = b.GetMass();
-
- float omega = 2.0f * Settings.b2_pi * _frequencyHz;
-
- float d = 2.0f * mass * _dampingRatio * omega;
-
- float k = mass * (omega * omega);
-
-
-
- Debug.Assert(d + step.dt * k > Settings.b2_epsilon);
- _gamma = step.dt * (d + step.dt * k);
- if (_gamma != 0.0f)
- {
- _gamma = 1.0f / _gamma;
- }
- _beta = step.dt * k * _gamma;
-
- Transform xf1;
- b.GetTransform(out xf1);
- Vector2 r = MathUtils.Multiply(ref xf1.R, _localAnchor - b.GetLocalCenter());
-
-
-
- float invMass = b._invMass;
- float invI = b._invI;
- Mat22 K1 = new Mat22(new Vector2(invMass, 0.0f), new Vector2(0.0f, invMass));
- Mat22 K2 = new Mat22(new Vector2(invI * r.Y * r.Y, -invI * r.X * r.Y), new Vector2(-invI * r.X * r.Y, invI * r.X * r.X));
- Mat22 K;
- Mat22.Add(ref K1, ref K2, out K);
- K.col1.X += _gamma;
- K.col2.Y += _gamma;
- _mass = K.GetInverse();
- _C = b._sweep.c + r - _target;
-
- b._angularVelocity *= 0.98f;
-
- _impulse *= step.dtRatio;
- b._linearVelocity += invMass * _impulse;
- b._angularVelocity += invI * MathUtils.Cross(r, _impulse);
- }
- internal override void SolveVelocityConstraints(ref TimeStep step)
- {
- Body b = _bodyB;
- Transform xf1;
- b.GetTransform(out xf1);
- Vector2 r = MathUtils.Multiply(ref xf1.R, _localAnchor - b.GetLocalCenter());
-
- Vector2 Cdot = b._linearVelocity + MathUtils.Cross(b._angularVelocity, r);
- Vector2 impulse = MathUtils.Multiply(ref _mass, -(Cdot + _beta * _C + _gamma * _impulse));
- Vector2 oldImpulse = _impulse;
- _impulse += impulse;
- float maxImpulse = step.dt * _maxForce;
- if (_impulse.LengthSquared() > maxImpulse * maxImpulse)
- {
- _impulse *= maxImpulse / _impulse.Length();
- }
- impulse = _impulse - oldImpulse;
- b._linearVelocity += b._invMass * impulse;
- b._angularVelocity += b._invI * MathUtils.Cross(r, impulse);
- }
- internal override bool SolvePositionConstraints(float baumgarte)
- {
- return true;
- }
- public Vector2 _localAnchor;
- public Vector2 _target;
- public Vector2 _impulse;
- public Mat22 _mass;
- public Vector2 _C;
- public float _maxForce;
- public float _frequencyHz;
- public float _dampingRatio;
- public float _beta;
- public float _gamma;
- };
- }
|