FixedAngleJoint.cs 2.3 KB

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