FixedDistanceJoint.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * Farseer Physics Engine based on Box2D.XNA port:
  3. * Copyright (c) 2010 Ian Qvist
  4. *
  5. * Box2D.XNA port of Box2D:
  6. * Copyright (c) 2009 Brandon Furtwangler, Nathan Furtwangler
  7. *
  8. * Original source Box2D:
  9. * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
  10. *
  11. * This software is provided 'as-is', without any express or implied
  12. * warranty. In no event will the authors be held liable for any damages
  13. * arising from the use of this software.
  14. * Permission is granted to anyone to use this software for any purpose,
  15. * including commercial applications, and to alter it and redistribute it
  16. * freely, subject to the following restrictions:
  17. * 1. The origin of this software must not be misrepresented; you must not
  18. * claim that you wrote the original software. If you use this software
  19. * in a product, an acknowledgment in the product documentation would be
  20. * appreciated but is not required.
  21. * 2. Altered source versions must be plainly marked as such, and must not be
  22. * misrepresented as being the original software.
  23. * 3. This notice may not be removed or altered from any source distribution.
  24. */
  25. using System;
  26. using System.Diagnostics;
  27. using FarseerPhysics.Common;
  28. using Microsoft.Xna.Framework;
  29. namespace FarseerPhysics.Dynamics.Joints
  30. {
  31. // 1-D rained system
  32. // m (v2 - v1) = lambda
  33. // v2 + (beta/h) * x1 + gamma * lambda = 0, gamma has units of inverse mass.
  34. // x2 = x1 + h * v2
  35. // 1-D mass-damper-spring system
  36. // m (v2 - v1) + h * d * v2 + h * k *
  37. // C = norm(p2 - p1) - L
  38. // u = (p2 - p1) / norm(p2 - p1)
  39. // Cdot = dot(u, v2 + cross(w2, r2) - v1 - cross(w1, r1))
  40. // J = [-u -cross(r1, u) u cross(r2, u)]
  41. // K = J * invM * JT
  42. // = invMass1 + invI1 * cross(r1, u)^2 + invMass2 + invI2 * cross(r2, u)^2
  43. /// <summary>
  44. /// A distance joint rains two points on two bodies
  45. /// to remain at a fixed distance from each other. You can view
  46. /// this as a massless, rigid rod.
  47. /// </summary>
  48. public class FixedDistanceJoint : Joint
  49. {
  50. /// <summary>
  51. /// The local anchor point relative to bodyA's origin.
  52. /// </summary>
  53. public Vector2 LocalAnchorA;
  54. private float _bias;
  55. private float _gamma;
  56. private float _impulse;
  57. private float _mass;
  58. private Vector2 _u;
  59. private Vector2 _worldAnchorB;
  60. /// <summary>
  61. /// This requires defining an
  62. /// anchor point on both bodies and the non-zero length of the
  63. /// distance joint. If you don't supply a length, the local anchor points
  64. /// is used so that the initial configuration can violate the constraint
  65. /// slightly. This helps when saving and loading a game.
  66. /// @warning Do not use a zero or short length.
  67. /// </summary>
  68. /// <param name="body">The body.</param>
  69. /// <param name="bodyAnchor">The body anchor.</param>
  70. /// <param name="worldAnchor">The world anchor.</param>
  71. public FixedDistanceJoint(Body body, Vector2 bodyAnchor, Vector2 worldAnchor)
  72. : base(body)
  73. {
  74. JointType = JointType.FixedDistance;
  75. LocalAnchorA = bodyAnchor;
  76. _worldAnchorB = worldAnchor;
  77. //Calculate the length
  78. Vector2 d = WorldAnchorB - WorldAnchorA;
  79. Length = d.Length();
  80. }
  81. /// <summary>
  82. /// The natural length between the anchor points.
  83. /// Manipulating the length can lead to non-physical behavior when the frequency is zero.
  84. /// </summary>
  85. public float Length { get; set; }
  86. /// <summary>
  87. /// The mass-spring-damper frequency in Hertz.
  88. /// </summary>
  89. public float Frequency { get; set; }
  90. /// <summary>
  91. /// The damping ratio. 0 = no damping, 1 = critical damping.
  92. /// </summary>
  93. public float DampingRatio { get; set; }
  94. public override sealed Vector2 WorldAnchorA
  95. {
  96. get { return BodyA.GetWorldPoint(LocalAnchorA); }
  97. }
  98. public override sealed Vector2 WorldAnchorB
  99. {
  100. get { return _worldAnchorB; }
  101. set { _worldAnchorB = value; }
  102. }
  103. public override Vector2 GetReactionForce(float invDt)
  104. {
  105. return (invDt * _impulse) * _u;
  106. }
  107. public override float GetReactionTorque(float invDt)
  108. {
  109. return 0.0f;
  110. }
  111. internal override void InitVelocityConstraints(ref TimeStep step)
  112. {
  113. Body b1 = BodyA;
  114. Transform xf1;
  115. b1.GetTransform(out xf1);
  116. // Compute the effective mass matrix.
  117. Vector2 r1 = MathUtils.Multiply(ref xf1.R, LocalAnchorA - b1.LocalCenter);
  118. Vector2 r2 = _worldAnchorB;
  119. _u = r2 - b1.Sweep.C - r1;
  120. // Handle singularity.
  121. float length = _u.Length();
  122. if (length > Settings.LinearSlop)
  123. {
  124. _u *= 1.0f / length;
  125. }
  126. else
  127. {
  128. _u = Vector2.Zero;
  129. }
  130. float cr1u = MathUtils.Cross(r1, _u);
  131. float cr2u = MathUtils.Cross(r2, _u);
  132. float invMass = b1.InvMass + b1.InvI * cr1u * cr1u + 0 * cr2u * cr2u;
  133. Debug.Assert(invMass > Settings.Epsilon);
  134. _mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
  135. if (Frequency > 0.0f)
  136. {
  137. float C = length - Length;
  138. // Frequency
  139. float omega = 2.0f * Settings.Pi * Frequency;
  140. // Damping coefficient
  141. float d = 2.0f * _mass * DampingRatio * omega;
  142. // Spring stiffness
  143. float k = _mass * omega * omega;
  144. // magic formulas
  145. _gamma = step.dt * (d + step.dt * k);
  146. _gamma = _gamma != 0.0f ? 1.0f / _gamma : 0.0f;
  147. _bias = C * step.dt * k * _gamma;
  148. _mass = invMass + _gamma;
  149. _mass = _mass != 0.0f ? 1.0f / _mass : 0.0f;
  150. }
  151. if (Settings.EnableWarmstarting)
  152. {
  153. // Scale the impulse to support a variable time step.
  154. _impulse *= step.dtRatio;
  155. Vector2 P = _impulse * _u;
  156. b1.LinearVelocityInternal -= b1.InvMass * P;
  157. b1.AngularVelocityInternal -= b1.InvI * MathUtils.Cross(r1, P);
  158. }
  159. else
  160. {
  161. _impulse = 0.0f;
  162. }
  163. }
  164. internal override void SolveVelocityConstraints(ref TimeStep step)
  165. {
  166. Body b1 = BodyA;
  167. Transform xf1;
  168. b1.GetTransform(out xf1);
  169. Vector2 r1 = MathUtils.Multiply(ref xf1.R, LocalAnchorA - b1.LocalCenter);
  170. // Cdot = dot(u, v + cross(w, r))
  171. Vector2 v1 = b1.LinearVelocityInternal + MathUtils.Cross(b1.AngularVelocityInternal, r1);
  172. Vector2 v2 = Vector2.Zero;
  173. float Cdot = Vector2.Dot(_u, v2 - v1);
  174. float impulse = -_mass * (Cdot + _bias + _gamma * _impulse);
  175. _impulse += impulse;
  176. Vector2 P = impulse * _u;
  177. b1.LinearVelocityInternal -= b1.InvMass * P;
  178. b1.AngularVelocityInternal -= b1.InvI * MathUtils.Cross(r1, P);
  179. }
  180. internal override bool SolvePositionConstraints()
  181. {
  182. if (Frequency > 0.0f)
  183. {
  184. // There is no position correction for soft distance constraints.
  185. return true;
  186. }
  187. Body b1 = BodyA;
  188. Transform xf1;
  189. b1.GetTransform(out xf1);
  190. Vector2 r1 = MathUtils.Multiply(ref xf1.R, LocalAnchorA - b1.LocalCenter);
  191. Vector2 r2 = _worldAnchorB;
  192. Vector2 d = r2 - b1.Sweep.C - r1;
  193. float length = d.Length();
  194. if (length == 0.0f)
  195. return true;
  196. d /= length;
  197. float C = length - Length;
  198. C = MathUtils.Clamp(C, -Settings.MaxLinearCorrection, Settings.MaxLinearCorrection);
  199. float impulse = -_mass * C;
  200. _u = d;
  201. Vector2 P = impulse * _u;
  202. b1.Sweep.C -= b1.InvMass * P;
  203. b1.Sweep.A -= b1.InvI * MathUtils.Cross(r1, P);
  204. b1.SynchronizeTransform();
  205. return Math.Abs(C) < Settings.LinearSlop;
  206. }
  207. }
  208. }