RopeJoint.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright (c) 2006-2010 Erin Catto http://www.gphysics.com
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. using System;
  19. using System.Diagnostics;
  20. using FarseerPhysics.Common;
  21. using Microsoft.Xna.Framework;
  22. namespace FarseerPhysics.Dynamics.Joints
  23. {
  24. // Limit:
  25. // C = norm(pB - pA) - L
  26. // u = (pB - pA) / norm(pB - pA)
  27. // Cdot = dot(u, vB + cross(wB, rB) - vA - cross(wA, rA))
  28. // J = [-u -cross(rA, u) u cross(rB, u)]
  29. // K = J * invM * JT
  30. // = invMassA + invIA * cross(rA, u)^2 + invMassB + invIB * cross(rB, u)^2
  31. /// <summary>
  32. /// A rope joint enforces a maximum distance between two points
  33. /// on two bodies. It has no other effect.
  34. /// Warning: if you attempt to change the maximum length during
  35. /// the simulation you will get some non-physical behavior.
  36. /// A model that would allow you to dynamically modify the length
  37. /// would have some sponginess, so I chose not to implement it
  38. /// that way. See b2DistanceJoint if you want to dynamically
  39. /// control length.
  40. /// </summary>
  41. public class RopeJoint : Joint
  42. {
  43. public Vector2 LocalAnchorA;
  44. public Vector2 LocalAnchorB;
  45. private float _impulse;
  46. private float _length;
  47. private float _mass;
  48. private Vector2 _rA, _rB;
  49. private LimitState _state;
  50. private Vector2 _u;
  51. internal RopeJoint()
  52. {
  53. JointType = JointType.Rope;
  54. }
  55. public RopeJoint(Body bodyA, Body bodyB, Vector2 localAnchorA, Vector2 localAnchorB)
  56. : base(bodyA, bodyB)
  57. {
  58. JointType = JointType.Rope;
  59. LocalAnchorA = localAnchorA;
  60. LocalAnchorB = localAnchorB;
  61. Vector2 d = WorldAnchorB - WorldAnchorA;
  62. MaxLength = d.Length();
  63. _mass = 0.0f;
  64. _impulse = 0.0f;
  65. _state = LimitState.Inactive;
  66. _length = 0.0f;
  67. }
  68. /// Get the maximum length of the rope.
  69. public float MaxLength { get; set; }
  70. public LimitState State
  71. {
  72. get { return _state; }
  73. }
  74. public override sealed Vector2 WorldAnchorA
  75. {
  76. get { return BodyA.GetWorldPoint(LocalAnchorA); }
  77. }
  78. public override sealed Vector2 WorldAnchorB
  79. {
  80. get { return BodyB.GetWorldPoint(LocalAnchorB); }
  81. set { Debug.Assert(false, "You can't set the world anchor on this joint type."); }
  82. }
  83. public override Vector2 GetReactionForce(float invDt)
  84. {
  85. return (invDt * _impulse) * _u;
  86. }
  87. public override float GetReactionTorque(float invDt)
  88. {
  89. return 0;
  90. }
  91. internal override void InitVelocityConstraints(ref TimeStep step)
  92. {
  93. Body bA = BodyA;
  94. Body bB = BodyB;
  95. Transform xf1;
  96. bA.GetTransform(out xf1);
  97. Transform xf2;
  98. bB.GetTransform(out xf2);
  99. _rA = MathUtils.Multiply(ref xf1.R, LocalAnchorA - bA.LocalCenter);
  100. _rB = MathUtils.Multiply(ref xf2.R, LocalAnchorB - bB.LocalCenter);
  101. // Rope axis
  102. _u = bB.Sweep.C + _rB - bA.Sweep.C - _rA;
  103. _length = _u.Length();
  104. float C = _length - MaxLength;
  105. if (C > 0.0f)
  106. {
  107. _state = LimitState.AtUpper;
  108. }
  109. else
  110. {
  111. _state = LimitState.Inactive;
  112. }
  113. if (_length > Settings.LinearSlop)
  114. {
  115. _u *= 1.0f / _length;
  116. }
  117. else
  118. {
  119. _u = Vector2.Zero;
  120. _mass = 0.0f;
  121. _impulse = 0.0f;
  122. return;
  123. }
  124. // Compute effective mass.
  125. float crA = MathUtils.Cross(_rA, _u);
  126. float crB = MathUtils.Cross(_rB, _u);
  127. float invMass = bA.InvMass + bA.InvI * crA * crA + bB.InvMass + bB.InvI * crB * crB;
  128. _mass = invMass != 0.0f ? 1.0f / invMass : 0.0f;
  129. if (Settings.EnableWarmstarting)
  130. {
  131. // Scale the impulse to support a variable time step.
  132. _impulse *= step.dtRatio;
  133. Vector2 P = _impulse * _u;
  134. bA.LinearVelocity -= bA.InvMass * P;
  135. bA.AngularVelocity -= bA.InvI * MathUtils.Cross(_rA, P);
  136. bB.LinearVelocity += bB.InvMass * P;
  137. bB.AngularVelocity += bB.InvI * MathUtils.Cross(_rB, P);
  138. }
  139. else
  140. {
  141. _impulse = 0.0f;
  142. }
  143. }
  144. internal override void SolveVelocityConstraints(ref TimeStep step)
  145. {
  146. Body bA = BodyA;
  147. Body bB = BodyB;
  148. // Cdot = dot(u, v + cross(w, r))
  149. Vector2 vA = bA.LinearVelocity + MathUtils.Cross(bA.AngularVelocity, _rA);
  150. Vector2 vB = bB.LinearVelocity + MathUtils.Cross(bB.AngularVelocity, _rB);
  151. float C = _length - MaxLength;
  152. float Cdot = Vector2.Dot(_u, vB - vA);
  153. // Predictive constraint.
  154. if (C < 0.0f)
  155. {
  156. Cdot += step.inv_dt * C;
  157. }
  158. float impulse = -_mass * Cdot;
  159. float oldImpulse = _impulse;
  160. _impulse = Math.Min(0.0f, _impulse + impulse);
  161. impulse = _impulse - oldImpulse;
  162. Vector2 P = impulse * _u;
  163. bA.LinearVelocity -= bA.InvMass * P;
  164. bA.AngularVelocity -= bA.InvI * MathUtils.Cross(_rA, P);
  165. bB.LinearVelocity += bB.InvMass * P;
  166. bB.AngularVelocity += bB.InvI * MathUtils.Cross(_rB, P);
  167. }
  168. internal override bool SolvePositionConstraints()
  169. {
  170. Body bA = BodyA;
  171. Body bB = BodyB;
  172. Transform xf1;
  173. bA.GetTransform(out xf1);
  174. Transform xf2;
  175. bB.GetTransform(out xf2);
  176. Vector2 rA = MathUtils.Multiply(ref xf1.R, LocalAnchorA - bA.LocalCenter);
  177. Vector2 rB = MathUtils.Multiply(ref xf2.R, LocalAnchorB - bB.LocalCenter);
  178. Vector2 u = bB.Sweep.C + rB - bA.Sweep.C - rA;
  179. float length = u.Length();
  180. u.Normalize();
  181. float C = length - MaxLength;
  182. C = MathUtils.Clamp(C, 0.0f, Settings.MaxLinearCorrection);
  183. float impulse = -_mass * C;
  184. Vector2 P = impulse * u;
  185. bA.Sweep.C -= bA.InvMass * P;
  186. bA.Sweep.A -= bA.InvI * MathUtils.Cross(rA, P);
  187. bB.Sweep.C += bB.InvMass * P;
  188. bB.Sweep.A += bB.InvI * MathUtils.Cross(rB, P);
  189. bA.SynchronizeTransform();
  190. bB.SynchronizeTransform();
  191. return length - MaxLength < Settings.LinearSlop;
  192. }
  193. }
  194. }