AngleJoint.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Diagnostics;
  3. using Microsoft.Xna.Framework;
  4. namespace FarseerPhysics.Dynamics.Joints
  5. {
  6. /// <summary>
  7. /// Maintains a fixed angle between two bodies
  8. /// </summary>
  9. public class AngleJoint : Joint
  10. {
  11. public float BiasFactor;
  12. public float MaxImpulse;
  13. public float Softness;
  14. private float _bias;
  15. private float _jointError;
  16. private float _massFactor;
  17. private float _targetAngle;
  18. internal AngleJoint()
  19. {
  20. JointType = JointType.Angle;
  21. }
  22. public AngleJoint(Body bodyA, Body bodyB)
  23. : base(bodyA, bodyB)
  24. {
  25. JointType = JointType.Angle;
  26. TargetAngle = 0;
  27. BiasFactor = .2f;
  28. Softness = 0f;
  29. MaxImpulse = float.MaxValue;
  30. }
  31. public float TargetAngle
  32. {
  33. get { return _targetAngle; }
  34. set
  35. {
  36. if (value != _targetAngle)
  37. {
  38. _targetAngle = value;
  39. WakeBodies();
  40. }
  41. }
  42. }
  43. public override Vector2 WorldAnchorA
  44. {
  45. get { return BodyA.Position; }
  46. }
  47. public override Vector2 WorldAnchorB
  48. {
  49. get { return BodyB.Position; }
  50. set { Debug.Assert(false, "You can't set the world anchor on this joint type."); }
  51. }
  52. public override Vector2 GetReactionForce(float inv_dt)
  53. {
  54. //TODO
  55. //return _inv_dt * _impulse;
  56. return Vector2.Zero;
  57. }
  58. public override float GetReactionTorque(float inv_dt)
  59. {
  60. return 0;
  61. }
  62. internal override void InitVelocityConstraints(ref TimeStep step)
  63. {
  64. _jointError = (BodyB.Sweep.A - BodyA.Sweep.A - TargetAngle);
  65. _bias = -BiasFactor * step.inv_dt * _jointError;
  66. _massFactor = (1 - Softness) / (BodyA.InvI + BodyB.InvI);
  67. }
  68. internal override void SolveVelocityConstraints(ref TimeStep step)
  69. {
  70. float p = (_bias - BodyB.AngularVelocity + BodyA.AngularVelocity) * _massFactor;
  71. BodyA.AngularVelocity -= BodyA.InvI * Math.Sign(p) * Math.Min(Math.Abs(p), MaxImpulse);
  72. BodyB.AngularVelocity += BodyB.InvI * Math.Sign(p) * Math.Min(Math.Abs(p), MaxImpulse);
  73. }
  74. internal override bool SolvePositionConstraints()
  75. {
  76. //no position solving for this joint
  77. return true;
  78. }
  79. }
  80. }