WeldJoint.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // Point-to-point constraint
  32. // C = p2 - p1
  33. // Cdot = v2 - v1
  34. // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
  35. // J = [-I -r1_skew I r2_skew ]
  36. // Identity used:
  37. // w k % (rx i + ry j) = w * (-ry i + rx j)
  38. // Angle constraint
  39. // C = angle2 - angle1 - referenceAngle
  40. // Cdot = w2 - w1
  41. // J = [0 0 -1 0 0 1]
  42. // K = invI1 + invI2
  43. /// <summary>
  44. /// A weld joint essentially glues two bodies together. A weld joint may
  45. /// distort somewhat because the island constraint solver is approximate.
  46. /// </summary>
  47. public class WeldJoint : Joint
  48. {
  49. public Vector2 LocalAnchorA;
  50. public Vector2 LocalAnchorB;
  51. private Vector3 _impulse;
  52. private Mat33 _mass;
  53. internal WeldJoint()
  54. {
  55. JointType = JointType.Weld;
  56. }
  57. /// <summary>
  58. /// You need to specify a local anchor point
  59. /// where they are attached and the relative body angle. The position
  60. /// of the anchor point is important for computing the reaction torque.
  61. /// You can change the anchor points relative to bodyA or bodyB by changing LocalAnchorA
  62. /// and/or LocalAnchorB.
  63. /// </summary>
  64. /// <param name="bodyA">The first body</param>
  65. /// <param name="bodyB">The second body</param>
  66. /// <param name="localAnchorA">The first body anchor.</param>
  67. /// <param name="localAnchorB">The second body anchor.</param>
  68. public WeldJoint(Body bodyA, Body bodyB, Vector2 localAnchorA, Vector2 localAnchorB)
  69. : base(bodyA, bodyB)
  70. {
  71. JointType = JointType.Weld;
  72. LocalAnchorA = localAnchorA;
  73. LocalAnchorB = localAnchorB;
  74. ReferenceAngle = BodyB.Rotation - BodyA.Rotation;
  75. }
  76. public override Vector2 WorldAnchorA
  77. {
  78. get { return BodyA.GetWorldPoint(LocalAnchorA); }
  79. }
  80. public override Vector2 WorldAnchorB
  81. {
  82. get { return BodyB.GetWorldPoint(LocalAnchorB); }
  83. set { Debug.Assert(false, "You can't set the world anchor on this joint type."); }
  84. }
  85. /// <summary>
  86. /// The body2 angle minus body1 angle in the reference state (radians).
  87. /// </summary>
  88. public float ReferenceAngle { get; private set; }
  89. public override Vector2 GetReactionForce(float inv_dt)
  90. {
  91. return inv_dt * new Vector2(_impulse.X, _impulse.Y);
  92. }
  93. public override float GetReactionTorque(float inv_dt)
  94. {
  95. return inv_dt * _impulse.Z;
  96. }
  97. internal override void InitVelocityConstraints(ref TimeStep step)
  98. {
  99. Body bA = BodyA;
  100. Body bB = BodyB;
  101. Transform xfA, xfB;
  102. bA.GetTransform(out xfA);
  103. bB.GetTransform(out xfB);
  104. // Compute the effective mass matrix.
  105. Vector2 rA = MathUtils.Multiply(ref xfA.R, LocalAnchorA - bA.LocalCenter);
  106. Vector2 rB = MathUtils.Multiply(ref xfB.R, LocalAnchorB - bB.LocalCenter);
  107. // J = [-I -r1_skew I r2_skew]
  108. // [ 0 -1 0 1]
  109. // r_skew = [-ry; rx]
  110. // Matlab
  111. // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
  112. // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
  113. // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
  114. float mA = bA.InvMass, mB = bB.InvMass;
  115. float iA = bA.InvI, iB = bB.InvI;
  116. _mass.Col1.X = mA + mB + rA.Y * rA.Y * iA + rB.Y * rB.Y * iB;
  117. _mass.Col2.X = -rA.Y * rA.X * iA - rB.Y * rB.X * iB;
  118. _mass.Col3.X = -rA.Y * iA - rB.Y * iB;
  119. _mass.Col1.Y = _mass.Col2.X;
  120. _mass.Col2.Y = mA + mB + rA.X * rA.X * iA + rB.X * rB.X * iB;
  121. _mass.Col3.Y = rA.X * iA + rB.X * iB;
  122. _mass.Col1.Z = _mass.Col3.X;
  123. _mass.Col2.Z = _mass.Col3.Y;
  124. _mass.Col3.Z = iA + iB;
  125. if (Settings.EnableWarmstarting)
  126. {
  127. // Scale impulses to support a variable time step.
  128. _impulse *= step.dtRatio;
  129. Vector2 P = new Vector2(_impulse.X, _impulse.Y);
  130. bA.LinearVelocityInternal -= mA * P;
  131. bA.AngularVelocityInternal -= iA * (MathUtils.Cross(rA, P) + _impulse.Z);
  132. bB.LinearVelocityInternal += mB * P;
  133. bB.AngularVelocityInternal += iB * (MathUtils.Cross(rB, P) + _impulse.Z);
  134. }
  135. else
  136. {
  137. _impulse = Vector3.Zero;
  138. }
  139. }
  140. internal override void SolveVelocityConstraints(ref TimeStep step)
  141. {
  142. Body bA = BodyA;
  143. Body bB = BodyB;
  144. Vector2 vA = bA.LinearVelocityInternal;
  145. float wA = bA.AngularVelocityInternal;
  146. Vector2 vB = bB.LinearVelocityInternal;
  147. float wB = bB.AngularVelocityInternal;
  148. float mA = bA.InvMass, mB = bB.InvMass;
  149. float iA = bA.InvI, iB = bB.InvI;
  150. Transform xfA, xfB;
  151. bA.GetTransform(out xfA);
  152. bB.GetTransform(out xfB);
  153. Vector2 rA = MathUtils.Multiply(ref xfA.R, LocalAnchorA - bA.LocalCenter);
  154. Vector2 rB = MathUtils.Multiply(ref xfB.R, LocalAnchorB - bB.LocalCenter);
  155. // Solve point-to-point constraint
  156. Vector2 Cdot1 = vB + MathUtils.Cross(wB, rB) - vA - MathUtils.Cross(wA, rA);
  157. float Cdot2 = wB - wA;
  158. Vector3 Cdot = new Vector3(Cdot1.X, Cdot1.Y, Cdot2);
  159. Vector3 impulse = _mass.Solve33(-Cdot);
  160. _impulse += impulse;
  161. Vector2 P = new Vector2(impulse.X, impulse.Y);
  162. vA -= mA * P;
  163. wA -= iA * (MathUtils.Cross(rA, P) + impulse.Z);
  164. vB += mB * P;
  165. wB += iB * (MathUtils.Cross(rB, P) + impulse.Z);
  166. bA.LinearVelocityInternal = vA;
  167. bA.AngularVelocityInternal = wA;
  168. bB.LinearVelocityInternal = vB;
  169. bB.AngularVelocityInternal = wB;
  170. }
  171. internal override bool SolvePositionConstraints()
  172. {
  173. Body bA = BodyA;
  174. Body bB = BodyB;
  175. float mA = bA.InvMass, mB = bB.InvMass;
  176. float iA = bA.InvI, iB = bB.InvI;
  177. Transform xfA;
  178. Transform xfB;
  179. bA.GetTransform(out xfA);
  180. bB.GetTransform(out xfB);
  181. Vector2 rA = MathUtils.Multiply(ref xfA.R, LocalAnchorA - bA.LocalCenter);
  182. Vector2 rB = MathUtils.Multiply(ref xfB.R, LocalAnchorB - bB.LocalCenter);
  183. Vector2 C1 = bB.Sweep.C + rB - bA.Sweep.C - rA;
  184. float C2 = bB.Sweep.A - bA.Sweep.A - ReferenceAngle;
  185. // Handle large detachment.
  186. const float k_allowedStretch = 10.0f * Settings.LinearSlop;
  187. float positionError = C1.Length();
  188. float angularError = Math.Abs(C2);
  189. if (positionError > k_allowedStretch)
  190. {
  191. iA *= 1.0f;
  192. iB *= 1.0f;
  193. }
  194. _mass.Col1.X = mA + mB + rA.Y * rA.Y * iA + rB.Y * rB.Y * iB;
  195. _mass.Col2.X = -rA.Y * rA.X * iA - rB.Y * rB.X * iB;
  196. _mass.Col3.X = -rA.Y * iA - rB.Y * iB;
  197. _mass.Col1.Y = _mass.Col2.X;
  198. _mass.Col2.Y = mA + mB + rA.X * rA.X * iA + rB.X * rB.X * iB;
  199. _mass.Col3.Y = rA.X * iA + rB.X * iB;
  200. _mass.Col1.Z = _mass.Col3.X;
  201. _mass.Col2.Z = _mass.Col3.Y;
  202. _mass.Col3.Z = iA + iB;
  203. Vector3 C = new Vector3(C1.X, C1.Y, C2);
  204. Vector3 impulse = _mass.Solve33(-C);
  205. Vector2 P = new Vector2(impulse.X, impulse.Y);
  206. bA.Sweep.C -= mA * P;
  207. bA.Sweep.A -= iA * (MathUtils.Cross(rA, P) + impulse.Z);
  208. bB.Sweep.C += mB * P;
  209. bB.Sweep.A += iB * (MathUtils.Cross(rB, P) + impulse.Z);
  210. bA.SynchronizeTransform();
  211. bB.SynchronizeTransform();
  212. return positionError <= Settings.LinearSlop && angularError <= Settings.AngularSlop;
  213. }
  214. }
  215. }