FrictionJoint.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.Diagnostics;
  26. using FarseerPhysics.Common;
  27. using Microsoft.Xna.Framework;
  28. namespace FarseerPhysics.Dynamics.Joints
  29. {
  30. // Point-to-point constraint
  31. // Cdot = v2 - v1
  32. // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
  33. // J = [-I -r1_skew I r2_skew ]
  34. // Identity used:
  35. // w k % (rx i + ry j) = w * (-ry i + rx j)
  36. // Angle constraint
  37. // Cdot = w2 - w1
  38. // J = [0 0 -1 0 0 1]
  39. // K = invI1 + invI2
  40. /// <summary>
  41. /// Friction joint. This is used for top-down friction.
  42. /// It provides 2D translational friction and angular friction.
  43. /// </summary>
  44. public class FrictionJoint : Joint
  45. {
  46. public Vector2 LocalAnchorA;
  47. public Vector2 LocalAnchorB;
  48. private float _angularImpulse;
  49. private float _angularMass;
  50. private Vector2 _linearImpulse;
  51. private Mat22 _linearMass;
  52. internal FrictionJoint()
  53. {
  54. JointType = JointType.Friction;
  55. }
  56. public FrictionJoint(Body bodyA, Body bodyB, Vector2 localAnchorA, Vector2 localAnchorB)
  57. : base(bodyA, bodyB)
  58. {
  59. JointType = JointType.Friction;
  60. LocalAnchorA = localAnchorA;
  61. LocalAnchorB = localAnchorB;
  62. }
  63. public override Vector2 WorldAnchorA
  64. {
  65. get { return BodyA.GetWorldPoint(LocalAnchorA); }
  66. }
  67. public override Vector2 WorldAnchorB
  68. {
  69. get { return BodyB.GetWorldPoint(LocalAnchorB); }
  70. set { Debug.Assert(false, "You can't set the world anchor on this joint type."); }
  71. }
  72. /// <summary>
  73. /// The maximum friction force in N.
  74. /// </summary>
  75. public float MaxForce { get; set; }
  76. /// <summary>
  77. /// The maximum friction torque in N-m.
  78. /// </summary>
  79. public float MaxTorque { get; set; }
  80. public override Vector2 GetReactionForce(float inv_dt)
  81. {
  82. return inv_dt * _linearImpulse;
  83. }
  84. public override float GetReactionTorque(float inv_dt)
  85. {
  86. return inv_dt * _angularImpulse;
  87. }
  88. internal override void InitVelocityConstraints(ref TimeStep step)
  89. {
  90. Body bA = BodyA;
  91. Body bB = BodyB;
  92. Transform xfA, xfB;
  93. bA.GetTransform(out xfA);
  94. bB.GetTransform(out xfB);
  95. // Compute the effective mass matrix.
  96. Vector2 rA = MathUtils.Multiply(ref xfA.R, LocalAnchorA - bA.LocalCenter);
  97. Vector2 rB = MathUtils.Multiply(ref xfB.R, LocalAnchorB - bB.LocalCenter);
  98. // J = [-I -r1_skew I r2_skew]
  99. // [ 0 -1 0 1]
  100. // r_skew = [-ry; rx]
  101. // Matlab
  102. // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
  103. // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
  104. // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
  105. float mA = bA.InvMass, mB = bB.InvMass;
  106. float iA = bA.InvI, iB = bB.InvI;
  107. Mat22 K1 = new Mat22();
  108. K1.Col1.X = mA + mB;
  109. K1.Col2.X = 0.0f;
  110. K1.Col1.Y = 0.0f;
  111. K1.Col2.Y = mA + mB;
  112. Mat22 K2 = new Mat22();
  113. K2.Col1.X = iA * rA.Y * rA.Y;
  114. K2.Col2.X = -iA * rA.X * rA.Y;
  115. K2.Col1.Y = -iA * rA.X * rA.Y;
  116. K2.Col2.Y = iA * rA.X * rA.X;
  117. Mat22 K3 = new Mat22();
  118. K3.Col1.X = iB * rB.Y * rB.Y;
  119. K3.Col2.X = -iB * rB.X * rB.Y;
  120. K3.Col1.Y = -iB * rB.X * rB.Y;
  121. K3.Col2.Y = iB * rB.X * rB.X;
  122. Mat22 K12;
  123. Mat22.Add(ref K1, ref K2, out K12);
  124. Mat22 K;
  125. Mat22.Add(ref K12, ref K3, out K);
  126. _linearMass = K.Inverse;
  127. _angularMass = iA + iB;
  128. if (_angularMass > 0.0f)
  129. {
  130. _angularMass = 1.0f / _angularMass;
  131. }
  132. if (Settings.EnableWarmstarting)
  133. {
  134. // Scale impulses to support a variable time step.
  135. _linearImpulse *= step.dtRatio;
  136. _angularImpulse *= step.dtRatio;
  137. Vector2 P = new Vector2(_linearImpulse.X, _linearImpulse.Y);
  138. bA.LinearVelocityInternal -= mA * P;
  139. bA.AngularVelocityInternal -= iA * (MathUtils.Cross(rA, P) + _angularImpulse);
  140. bB.LinearVelocityInternal += mB * P;
  141. bB.AngularVelocityInternal += iB * (MathUtils.Cross(rB, P) + _angularImpulse);
  142. }
  143. else
  144. {
  145. _linearImpulse = Vector2.Zero;
  146. _angularImpulse = 0.0f;
  147. }
  148. }
  149. internal override void SolveVelocityConstraints(ref TimeStep step)
  150. {
  151. Body bA = BodyA;
  152. Body bB = BodyB;
  153. Vector2 vA = bA.LinearVelocityInternal;
  154. float wA = bA.AngularVelocityInternal;
  155. Vector2 vB = bB.LinearVelocityInternal;
  156. float wB = bB.AngularVelocityInternal;
  157. float mA = bA.InvMass, mB = bB.InvMass;
  158. float iA = bA.InvI, iB = bB.InvI;
  159. Transform xfA, xfB;
  160. bA.GetTransform(out xfA);
  161. bB.GetTransform(out xfB);
  162. Vector2 rA = MathUtils.Multiply(ref xfA.R, LocalAnchorA - bA.LocalCenter);
  163. Vector2 rB = MathUtils.Multiply(ref xfB.R, LocalAnchorB - bB.LocalCenter);
  164. // Solve angular friction
  165. {
  166. float Cdot = wB - wA;
  167. float impulse = -_angularMass * Cdot;
  168. float oldImpulse = _angularImpulse;
  169. float maxImpulse = step.dt * MaxTorque;
  170. _angularImpulse = MathUtils.Clamp(_angularImpulse + impulse, -maxImpulse, maxImpulse);
  171. impulse = _angularImpulse - oldImpulse;
  172. wA -= iA * impulse;
  173. wB += iB * impulse;
  174. }
  175. // Solve linear friction
  176. {
  177. Vector2 Cdot = vB + MathUtils.Cross(wB, rB) - vA - MathUtils.Cross(wA, rA);
  178. Vector2 impulse = -MathUtils.Multiply(ref _linearMass, Cdot);
  179. Vector2 oldImpulse = _linearImpulse;
  180. _linearImpulse += impulse;
  181. float maxImpulse = step.dt * MaxForce;
  182. if (_linearImpulse.LengthSquared() > maxImpulse * maxImpulse)
  183. {
  184. _linearImpulse.Normalize();
  185. _linearImpulse *= maxImpulse;
  186. }
  187. impulse = _linearImpulse - oldImpulse;
  188. vA -= mA * impulse;
  189. wA -= iA * MathUtils.Cross(rA, impulse);
  190. vB += mB * impulse;
  191. wB += iB * MathUtils.Cross(rB, impulse);
  192. }
  193. bA.LinearVelocityInternal = vA;
  194. bA.AngularVelocityInternal = wA;
  195. bB.LinearVelocityInternal = vB;
  196. bB.AngularVelocityInternal = wB;
  197. }
  198. internal override bool SolvePositionConstraints()
  199. {
  200. return true;
  201. }
  202. }
  203. }