SliderJoint.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. /// <summary>
  32. /// A distance joint contrains two points on two bodies
  33. /// to remain at a fixed distance from each other. You can view
  34. /// this as a massless, rigid rod.
  35. /// </summary>
  36. public class SliderJoint : Joint
  37. {
  38. // 1-D constrained system
  39. // m (v2 - v1) = lambda
  40. // v2 + (beta/h) * x1 + gamma * lambda = 0, gamma has units of inverse mass.
  41. // x2 = x1 + h * v2
  42. // 1-D mass-damper-spring system
  43. // m (v2 - v1) + h * d * v2 + h * k *
  44. // C = norm(p2 - p1) - L
  45. // u = (p2 - p1) / norm(p2 - p1)
  46. // Cdot = dot(u, v2 + cross(w2, r2) - v1 - cross(w1, r1))
  47. // J = [-u -cross(r1, u) u cross(r2, u)]
  48. // K = J * invM * JT
  49. // = invMass1 + invI1 * cross(r1, u)^2 + invMass2 + invI2 * cross(r2, u)^2
  50. public Vector2 LocalAnchorA;
  51. public Vector2 LocalAnchorB;
  52. private float _bias;
  53. private float _gamma;
  54. private float _impulse;
  55. private float _mass;
  56. private Vector2 _u;
  57. internal SliderJoint()
  58. {
  59. JointType = JointType.Slider;
  60. }
  61. /// <summary>
  62. /// Initializes a new instance of the <see cref="SliderJoint"/> class.
  63. /// Warning: Do not use a zero or short length.
  64. /// </summary>
  65. /// <param name="bodyA">The first body.</param>
  66. /// <param name="bodyB">The second body.</param>
  67. /// <param name="localAnchorA">The first body anchor.</param>
  68. /// <param name="localAnchorB">The second body anchor.</param>
  69. /// <param name="minLength">The minimum length between anchorpoints</param>
  70. /// <param name="maxlength">The maximum length between anchorpoints.</param>
  71. public SliderJoint(Body bodyA, Body bodyB, Vector2 localAnchorA, Vector2 localAnchorB, float minLength,
  72. float maxlength)
  73. : base(bodyA, bodyB)
  74. {
  75. JointType = JointType.Slider;
  76. LocalAnchorA = localAnchorA;
  77. LocalAnchorB = localAnchorB;
  78. MaxLength = maxlength;
  79. MinLength = minLength;
  80. }
  81. /// <summary>
  82. /// The maximum length between the anchor points.
  83. /// </summary>
  84. /// <value>The length.</value>
  85. public float MaxLength { get; set; }
  86. /// <summary>
  87. /// The minimal length between the anchor points.
  88. /// </summary>
  89. /// <value>The length.</value>
  90. public float MinLength { get; set; }
  91. /// <summary>
  92. /// The mass-spring-damper frequency in Hertz.
  93. /// </summary>
  94. /// <value>The frequency.</value>
  95. public float Frequency { get; set; }
  96. /// <summary>
  97. /// The damping ratio. 0 = no damping, 1 = critical damping.
  98. /// </summary>
  99. /// <value>The damping ratio.</value>
  100. public float DampingRatio { get; set; }
  101. public override Vector2 WorldAnchorA
  102. {
  103. get { return BodyA.GetWorldPoint(LocalAnchorA); }
  104. }
  105. public override Vector2 WorldAnchorB
  106. {
  107. get { return BodyB.GetWorldPoint(LocalAnchorB); }
  108. set { Debug.Assert(false, "You can't set the world anchor on this joint type."); }
  109. }
  110. public override Vector2 GetReactionForce(float inv_dt)
  111. {
  112. Vector2 F = (inv_dt * _impulse) * _u;
  113. return F;
  114. }
  115. public override float GetReactionTorque(float inv_dt)
  116. {
  117. return 0.0f;
  118. }
  119. internal override void InitVelocityConstraints(ref TimeStep step)
  120. {
  121. Body b1 = BodyA;
  122. Body b2 = BodyB;
  123. Transform xf1, xf2;
  124. b1.GetTransform(out xf1);
  125. b2.GetTransform(out xf2);
  126. // Compute the effective mass matrix.
  127. Vector2 r1 = MathUtils.Multiply(ref xf1.R, LocalAnchorA - b1.LocalCenter);
  128. Vector2 r2 = MathUtils.Multiply(ref xf2.R, LocalAnchorB - b2.LocalCenter);
  129. _u = b2.Sweep.C + r2 - b1.Sweep.C - r1;
  130. // Handle singularity.
  131. float length = _u.Length();
  132. if (length < MaxLength && length > MinLength)
  133. {
  134. return;
  135. }
  136. if (length > Settings.LinearSlop)
  137. {
  138. _u *= 1.0f / length;
  139. }
  140. else
  141. {
  142. _u = Vector2.Zero;
  143. }
  144. float cr1u = MathUtils.Cross(r1, _u);
  145. float cr2u = MathUtils.Cross(r2, _u);
  146. float invMass = b1.InvMass + b1.InvI * cr1u * cr1u + b2.InvMass + b2.InvI * cr2u * cr2u;
  147. Debug.Assert(invMass > Settings.Epsilon);
  148. _mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
  149. if (Frequency > 0.0f)
  150. {
  151. float C = length - MaxLength;
  152. // Frequency
  153. float omega = 2.0f * Settings.Pi * Frequency;
  154. // Damping coefficient
  155. float d = 2.0f * _mass * DampingRatio * omega;
  156. // Spring stiffness
  157. float k = _mass * omega * omega;
  158. // magic formulas
  159. _gamma = step.dt * (d + step.dt * k);
  160. _gamma = _gamma != 0.0f ? 1.0f / _gamma : 0.0f;
  161. _bias = C * step.dt * k * _gamma;
  162. _mass = invMass + _gamma;
  163. _mass = _mass != 0.0f ? 1.0f / _mass : 0.0f;
  164. }
  165. if (Settings.EnableWarmstarting)
  166. {
  167. // Scale the impulse to support a variable time step.
  168. _impulse *= step.dtRatio;
  169. Vector2 P = _impulse * _u;
  170. b1.LinearVelocityInternal -= b1.InvMass * P;
  171. b1.AngularVelocityInternal -= b1.InvI * MathUtils.Cross(r1, P);
  172. b2.LinearVelocityInternal += b2.InvMass * P;
  173. b2.AngularVelocityInternal += b2.InvI * MathUtils.Cross(r2, P);
  174. }
  175. else
  176. {
  177. _impulse = 0.0f;
  178. }
  179. }
  180. internal override void SolveVelocityConstraints(ref TimeStep step)
  181. {
  182. Body b1 = BodyA;
  183. Body b2 = BodyB;
  184. Transform xf1, xf2;
  185. b1.GetTransform(out xf1);
  186. b2.GetTransform(out xf2);
  187. Vector2 r1 = MathUtils.Multiply(ref xf1.R, LocalAnchorA - b1.LocalCenter);
  188. Vector2 r2 = MathUtils.Multiply(ref xf2.R, LocalAnchorB - b2.LocalCenter);
  189. Vector2 d = b2.Sweep.C + r2 - b1.Sweep.C - r1;
  190. float length = d.Length();
  191. if (length < MaxLength && length > MinLength)
  192. {
  193. return;
  194. }
  195. // Cdot = dot(u, v + cross(w, r))
  196. Vector2 v1 = b1.LinearVelocityInternal + MathUtils.Cross(b1.AngularVelocityInternal, r1);
  197. Vector2 v2 = b2.LinearVelocityInternal + MathUtils.Cross(b2.AngularVelocityInternal, r2);
  198. float Cdot = Vector2.Dot(_u, v2 - v1);
  199. float impulse = -_mass * (Cdot + _bias + _gamma * _impulse);
  200. _impulse += impulse;
  201. Vector2 P = impulse * _u;
  202. b1.LinearVelocityInternal -= b1.InvMass * P;
  203. b1.AngularVelocityInternal -= b1.InvI * MathUtils.Cross(r1, P);
  204. b2.LinearVelocityInternal += b2.InvMass * P;
  205. b2.AngularVelocityInternal += b2.InvI * MathUtils.Cross(r2, P);
  206. }
  207. internal override bool SolvePositionConstraints()
  208. {
  209. if (Frequency > 0.0f)
  210. {
  211. // There is no position correction for soft distance constraints.
  212. return true;
  213. }
  214. Body b1 = BodyA;
  215. Body b2 = BodyB;
  216. Transform xf1, xf2;
  217. b1.GetTransform(out xf1);
  218. b2.GetTransform(out xf2);
  219. Vector2 r1 = MathUtils.Multiply(ref xf1.R, LocalAnchorA - b1.LocalCenter);
  220. Vector2 r2 = MathUtils.Multiply(ref xf2.R, LocalAnchorB - b2.LocalCenter);
  221. Vector2 d = b2.Sweep.C + r2 - b1.Sweep.C - r1;
  222. float length = d.Length();
  223. if (length < MaxLength && length > MinLength)
  224. {
  225. return true;
  226. }
  227. if (length == 0.0f)
  228. return true;
  229. d /= length;
  230. float C = length - MaxLength;
  231. C = MathUtils.Clamp(C, -Settings.MaxLinearCorrection, Settings.MaxLinearCorrection);
  232. float impulse = -_mass * C;
  233. _u = d;
  234. Vector2 P = impulse * _u;
  235. b1.Sweep.C -= b1.InvMass * P;
  236. b1.Sweep.A -= b1.InvI * MathUtils.Cross(r1, P);
  237. b2.Sweep.C += b2.InvMass * P;
  238. b2.Sweep.A += b2.InvI * MathUtils.Cross(r2, P);
  239. b1.SynchronizeTransform();
  240. b2.SynchronizeTransform();
  241. return Math.Abs(C) < Settings.LinearSlop;
  242. }
  243. }
  244. }