2
0

VelocityLimitController.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using FarseerPhysics.Dynamics;
  4. namespace FarseerPhysics.Controllers
  5. {
  6. /// <summary>
  7. /// Put a limit on the linear (translation - the movespeed) and angular (rotation) velocity
  8. /// of bodies added to this controller.
  9. /// </summary>
  10. public class VelocityLimitController : Controller
  11. {
  12. public bool LimitAngularVelocity = true;
  13. public bool LimitLinearVelocity = true;
  14. private List<Body> _bodies = new List<Body>();
  15. private float _maxAngularSqared;
  16. private float _maxAngularVelocity;
  17. private float _maxLinearSqared;
  18. private float _maxLinearVelocity;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="VelocityLimitController"/> class.
  21. /// Sets the max linear velocity to Settings.MaxTranslation
  22. /// Sets the max angular velocity to Settings.MaxRotation
  23. /// </summary>
  24. public VelocityLimitController()
  25. : base(ControllerType.VelocityLimitController)
  26. {
  27. MaxLinearVelocity = Settings.MaxTranslation;
  28. MaxAngularVelocity = Settings.MaxRotation;
  29. }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="VelocityLimitController"/> class.
  32. /// Pass in 0 or float.MaxValue to disable the limit.
  33. /// maxAngularVelocity = 0 will disable the angular velocity limit.
  34. /// </summary>
  35. /// <param name="maxLinearVelocity">The max linear velocity.</param>
  36. /// <param name="maxAngularVelocity">The max angular velocity.</param>
  37. public VelocityLimitController(float maxLinearVelocity, float maxAngularVelocity)
  38. : base(ControllerType.VelocityLimitController)
  39. {
  40. if (maxLinearVelocity == 0 || maxLinearVelocity == float.MaxValue)
  41. LimitLinearVelocity = false;
  42. if (maxAngularVelocity == 0 || maxAngularVelocity == float.MaxValue)
  43. LimitAngularVelocity = false;
  44. MaxLinearVelocity = maxLinearVelocity;
  45. MaxAngularVelocity = maxAngularVelocity;
  46. }
  47. /// <summary>
  48. /// Gets or sets the max angular velocity.
  49. /// </summary>
  50. /// <value>The max angular velocity.</value>
  51. public float MaxAngularVelocity
  52. {
  53. get { return _maxAngularVelocity; }
  54. set
  55. {
  56. _maxAngularVelocity = value;
  57. _maxAngularSqared = _maxAngularVelocity * _maxAngularVelocity;
  58. }
  59. }
  60. /// <summary>
  61. /// Gets or sets the max linear velocity.
  62. /// </summary>
  63. /// <value>The max linear velocity.</value>
  64. public float MaxLinearVelocity
  65. {
  66. get { return _maxLinearVelocity; }
  67. set
  68. {
  69. _maxLinearVelocity = value;
  70. _maxLinearSqared = _maxLinearVelocity * _maxLinearVelocity;
  71. }
  72. }
  73. public override void Update(float dt)
  74. {
  75. foreach (Body body in _bodies)
  76. {
  77. if (!IsActiveOn(body))
  78. continue;
  79. if (LimitLinearVelocity)
  80. {
  81. //Translation
  82. // Check for large velocities.
  83. float translationX = dt * body.LinearVelocityInternal.X;
  84. float translationY = dt * body.LinearVelocityInternal.Y;
  85. float result = translationX * translationX + translationY * translationY;
  86. if (result > dt * _maxLinearSqared)
  87. {
  88. float sq = (float)Math.Sqrt(result);
  89. float ratio = _maxLinearVelocity / sq;
  90. body.LinearVelocityInternal.X *= ratio;
  91. body.LinearVelocityInternal.Y *= ratio;
  92. }
  93. }
  94. if (LimitAngularVelocity)
  95. {
  96. //Rotation
  97. float rotation = dt * body.AngularVelocityInternal;
  98. if (rotation * rotation > _maxAngularSqared)
  99. {
  100. float ratio = _maxAngularVelocity / Math.Abs(rotation);
  101. body.AngularVelocityInternal *= ratio;
  102. }
  103. }
  104. }
  105. }
  106. public void AddBody(Body body)
  107. {
  108. _bodies.Add(body);
  109. }
  110. public void RemoveBody(Body body)
  111. {
  112. _bodies.Remove(body);
  113. }
  114. }
  115. }