DistanceJoint.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 DistanceJoint : Joint
  49. {
  50. /// <summary>
  51. /// The local anchor point relative to bodyA's origin.
  52. /// </summary>
  53. public Vector2 LocalAnchorA;
  54. /// <summary>
  55. /// The local anchor point relative to bodyB's origin.
  56. /// </summary>
  57. public Vector2 LocalAnchorB;
  58. private float _bias;
  59. private float _gamma;
  60. private float _impulse;
  61. private float _mass;
  62. private float _tmpFloat1;
  63. private Vector2 _tmpVector1;
  64. private Vector2 _u;
  65. internal DistanceJoint()
  66. {
  67. JointType = JointType.Distance;
  68. }
  69. /// <summary>
  70. /// This requires defining an
  71. /// anchor point on both bodies and the non-zero length of the
  72. /// distance joint. If you don't supply a length, the local anchor points
  73. /// is used so that the initial configuration can violate the constraint
  74. /// slightly. This helps when saving and loading a game.
  75. /// @warning Do not use a zero or short length.
  76. /// </summary>
  77. /// <param name="bodyA">The first body</param>
  78. /// <param name="bodyB">The second body</param>
  79. /// <param name="localAnchorA">The first body anchor</param>
  80. /// <param name="localAnchorB">The second body anchor</param>
  81. public DistanceJoint(Body bodyA, Body bodyB, Vector2 localAnchorA, Vector2 localAnchorB)
  82. : base(bodyA, bodyB)
  83. {
  84. JointType = JointType.Distance;
  85. LocalAnchorA = localAnchorA;
  86. LocalAnchorB = localAnchorB;
  87. Vector2 d = WorldAnchorB - WorldAnchorA;
  88. Length = d.Length();
  89. }
  90. /// <summary>
  91. /// The natural length between the anchor points.
  92. /// Manipulating the length can lead to non-physical behavior when the frequency is zero.
  93. /// </summary>
  94. public float Length { get; set; }
  95. /// <summary>
  96. /// The mass-spring-damper frequency in Hertz.
  97. /// </summary>
  98. public float Frequency { get; set; }
  99. /// <summary>
  100. /// The damping ratio. 0 = no damping, 1 = critical damping.
  101. /// </summary>
  102. public float DampingRatio { get; set; }
  103. public override sealed Vector2 WorldAnchorA
  104. {
  105. get { return BodyA.GetWorldPoint(LocalAnchorA); }
  106. }
  107. public override sealed Vector2 WorldAnchorB
  108. {
  109. get { return BodyB.GetWorldPoint(LocalAnchorB); }
  110. set { Debug.Assert(false, "You can't set the world anchor on this joint type."); }
  111. }
  112. public override Vector2 GetReactionForce(float inv_dt)
  113. {
  114. Vector2 F = (inv_dt * _impulse) * _u;
  115. return F;
  116. }
  117. public override float GetReactionTorque(float inv_dt)
  118. {
  119. return 0.0f;
  120. }
  121. internal override void InitVelocityConstraints(ref TimeStep step)
  122. {
  123. Body b1 = BodyA;
  124. Body b2 = BodyB;
  125. // Compute the effective mass matrix.
  126. Vector2 r1 = MathUtils.Multiply(ref b1.Xf.R, LocalAnchorA - b1.LocalCenter);
  127. Vector2 r2 = MathUtils.Multiply(ref b2.Xf.R, LocalAnchorB - b2.LocalCenter);
  128. _u = b2.Sweep.C + r2 - b1.Sweep.C - r1;
  129. // Handle singularity.
  130. float length = _u.Length();
  131. if (length > Settings.LinearSlop)
  132. {
  133. _u *= 1.0f / length;
  134. }
  135. else
  136. {
  137. _u = Vector2.Zero;
  138. }
  139. float cr1u, cr2u;
  140. MathUtils.Cross(ref r1, ref _u, out cr1u);
  141. MathUtils.Cross(ref r2, ref _u, out cr2u);
  142. float invMass = b1.InvMass + b1.InvI * cr1u * cr1u + b2.InvMass + b2.InvI * cr2u * cr2u;
  143. Debug.Assert(invMass > Settings.Epsilon);
  144. _mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
  145. if (Frequency > 0.0f)
  146. {
  147. float C = length - Length;
  148. // Frequency
  149. float omega = 2.0f * Settings.Pi * Frequency;
  150. // Damping coefficient
  151. float d = 2.0f * _mass * DampingRatio * omega;
  152. // Spring stiffness
  153. float k = _mass * omega * omega;
  154. // magic formulas
  155. _gamma = step.dt * (d + step.dt * k);
  156. _gamma = _gamma != 0.0f ? 1.0f / _gamma : 0.0f;
  157. _bias = C * step.dt * k * _gamma;
  158. _mass = invMass + _gamma;
  159. _mass = _mass != 0.0f ? 1.0f / _mass : 0.0f;
  160. }
  161. if (Settings.EnableWarmstarting)
  162. {
  163. // Scale the impulse to support a variable time step.
  164. _impulse *= step.dtRatio;
  165. Vector2 P = _impulse * _u;
  166. b1.LinearVelocityInternal -= b1.InvMass * P;
  167. MathUtils.Cross(ref r1, ref P, out _tmpFloat1);
  168. b1.AngularVelocityInternal -= b1.InvI * /* r1 x P */ _tmpFloat1;
  169. b2.LinearVelocityInternal += b2.InvMass * P;
  170. MathUtils.Cross(ref r2, ref P, out _tmpFloat1);
  171. b2.AngularVelocityInternal += b2.InvI * /* r2 x P */ _tmpFloat1;
  172. }
  173. else
  174. {
  175. _impulse = 0.0f;
  176. }
  177. }
  178. internal override void SolveVelocityConstraints(ref TimeStep step)
  179. {
  180. Body b1 = BodyA;
  181. Body b2 = BodyB;
  182. Transform xf1, xf2;
  183. b1.GetTransform(out xf1);
  184. b2.GetTransform(out xf2);
  185. Vector2 r1 = MathUtils.Multiply(ref xf1.R, LocalAnchorA - b1.LocalCenter);
  186. Vector2 r2 = MathUtils.Multiply(ref xf2.R, LocalAnchorB - b2.LocalCenter);
  187. // Cdot = dot(u, v + cross(w, r))
  188. MathUtils.Cross(b1.AngularVelocityInternal, ref r1, out _tmpVector1);
  189. Vector2 v1 = b1.LinearVelocityInternal + _tmpVector1;
  190. MathUtils.Cross(b2.AngularVelocityInternal, ref r2, out _tmpVector1);
  191. Vector2 v2 = b2.LinearVelocityInternal + _tmpVector1;
  192. float Cdot = Vector2.Dot(_u, v2 - v1);
  193. float impulse = -_mass * (Cdot + _bias + _gamma * _impulse);
  194. _impulse += impulse;
  195. Vector2 P = impulse * _u;
  196. b1.LinearVelocityInternal -= b1.InvMass * P;
  197. MathUtils.Cross(ref r1, ref P, out _tmpFloat1);
  198. b1.AngularVelocityInternal -= b1.InvI * _tmpFloat1;
  199. b2.LinearVelocityInternal += b2.InvMass * P;
  200. MathUtils.Cross(ref r2, ref P, out _tmpFloat1);
  201. b2.AngularVelocityInternal += b2.InvI * _tmpFloat1;
  202. }
  203. internal override bool SolvePositionConstraints()
  204. {
  205. if (Frequency > 0.0f)
  206. {
  207. // There is no position correction for soft distance constraints.
  208. return true;
  209. }
  210. Body b1 = BodyA;
  211. Body b2 = BodyB;
  212. Transform xf1, xf2;
  213. b1.GetTransform(out xf1);
  214. b2.GetTransform(out xf2);
  215. Vector2 r1 = MathUtils.Multiply(ref xf1.R, LocalAnchorA - b1.LocalCenter);
  216. Vector2 r2 = MathUtils.Multiply(ref xf2.R, LocalAnchorB - b2.LocalCenter);
  217. Vector2 d = b2.Sweep.C + r2 - b1.Sweep.C - r1;
  218. float length = d.Length();
  219. if (length == 0.0f)
  220. return true;
  221. d /= length;
  222. float C = length - Length;
  223. C = MathUtils.Clamp(C, -Settings.MaxLinearCorrection, Settings.MaxLinearCorrection);
  224. float impulse = -_mass * C;
  225. _u = d;
  226. Vector2 P = impulse * _u;
  227. b1.Sweep.C -= b1.InvMass * P;
  228. MathUtils.Cross(ref r1, ref P, out _tmpFloat1);
  229. b1.Sweep.A -= b1.InvI * _tmpFloat1;
  230. b2.Sweep.C += b2.InvMass * P;
  231. MathUtils.Cross(ref r2, ref P, out _tmpFloat1);
  232. b2.Sweep.A += b2.InvI * _tmpFloat1;
  233. b1.SynchronizeTransform();
  234. b2.SynchronizeTransform();
  235. return Math.Abs(C) < Settings.LinearSlop;
  236. }
  237. }
  238. }