WeldJoint.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Box2D.XNA port of Box2D:
  3. * Copyright (c) 2009 Brandon Furtwangler, Nathan Furtwangler
  4. *
  5. * Original source Box2D:
  6. * Copyright (c) 2006-2009 Erin Catto http://www.gphysics.com
  7. *
  8. * This software is provided 'as-is', without any express or implied
  9. * warranty. In no event will the authors be held liable for any damages
  10. * arising from the use of this software.
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. * 1. The origin of this software must not be misrepresented; you must not
  15. * claim that you wrote the original software. If you use this software
  16. * in a product, an acknowledgment in the product documentation would be
  17. * appreciated but is not required.
  18. * 2. Altered source versions must be plainly marked as such, and must not be
  19. * misrepresented as being the original software.
  20. * 3. This notice may not be removed or altered from any source distribution.
  21. */
  22. using System;
  23. using Microsoft.Xna.Framework;
  24. namespace Box2D.XNA
  25. {
  26. /// Weld joint definition. You need to specify local anchor points
  27. /// where they are attached and the relative body angle. The position
  28. /// of the anchor points is important for computing the reaction torque.
  29. public class WeldJointDef : JointDef
  30. {
  31. public WeldJointDef()
  32. {
  33. type = JointType.Weld;
  34. }
  35. // Point-to-point constraint
  36. // C = p2 - p1
  37. // Cdot = v2 - v1
  38. // = v2 + cross(w2, r2) - v1 - cross(w1, r1)
  39. // J = [-I -r1_skew I r2_skew ]
  40. // Identity used:
  41. // w k % (rx i + ry j) = w * (-ry i + rx j)
  42. // Angle constraint
  43. // C = angle2 - angle1 - referenceAngle
  44. // Cdot = w2 - w1
  45. // J = [0 0 -1 0 0 1]
  46. // K = invI1 + invI2
  47. public void Initialize(Body b1, Body b2, Vector2 anchor)
  48. {
  49. bodyA = b1;
  50. bodyB = b2;
  51. localAnchorA = bodyA.GetLocalPoint(anchor);
  52. localAnchorB = bodyB.GetLocalPoint(anchor);
  53. referenceAngle = bodyB.GetAngle() - bodyA.GetAngle();
  54. }
  55. /// The local anchor point relative to body1's origin.
  56. public Vector2 localAnchorA;
  57. /// The local anchor point relative to body2's origin.
  58. public Vector2 localAnchorB;
  59. /// The body2 angle minus body1 angle in the reference state (radians).
  60. public float referenceAngle;
  61. };
  62. /// A weld joint essentially glues two bodies together. A weld joint may
  63. /// distort somewhat because the island constraint solver is approximate.
  64. public class WeldJoint : Joint
  65. {
  66. public override Vector2 GetAnchorA()
  67. {
  68. return _bodyA.GetWorldPoint(_localAnchorA);
  69. }
  70. public override Vector2 GetAnchorB()
  71. {
  72. return _bodyB.GetWorldPoint(_localAnchorB);
  73. }
  74. public override Vector2 GetReactionForce(float inv_dt)
  75. {
  76. Vector2 F = (inv_dt * new Vector2(_impulse.X, _impulse.Y));
  77. return F;
  78. }
  79. public override float GetReactionTorque(float inv_dt)
  80. {
  81. float F = (inv_dt * _impulse.Z);
  82. return F;
  83. }
  84. internal WeldJoint(WeldJointDef def)
  85. : base(def)
  86. {
  87. _localAnchorA = def.localAnchorA;
  88. _localAnchorB = def.localAnchorB;
  89. _referenceAngle = def.referenceAngle;
  90. }
  91. internal override void InitVelocityConstraints(ref TimeStep step)
  92. {
  93. Body bA = _bodyA;
  94. Body bB = _bodyB;
  95. Transform xfA, xfB;
  96. bA.GetTransform(out xfA);
  97. bB.GetTransform(out xfB);
  98. // Compute the effective mass matrix.
  99. Vector2 rA = MathUtils.Multiply(ref xfA.R, _localAnchorA - bA.GetLocalCenter());
  100. Vector2 rB = MathUtils.Multiply(ref xfB.R, _localAnchorB - bB.GetLocalCenter());
  101. // J = [-I -r1_skew I r2_skew]
  102. // [ 0 -1 0 1]
  103. // r_skew = [-ry; rx]
  104. // Matlab
  105. // K = [ mA+r1y^2*iA+mB+r2y^2*iB, -r1y*iA*r1x-r2y*iB*r2x, -r1y*iA-r2y*iB]
  106. // [ -r1y*iA*r1x-r2y*iB*r2x, mA+r1x^2*iA+mB+r2x^2*iB, r1x*iA+r2x*iB]
  107. // [ -r1y*iA-r2y*iB, r1x*iA+r2x*iB, iA+iB]
  108. float mA = bA._invMass, mB = bB._invMass;
  109. float iA = bA._invI, iB = bB._invI;
  110. _mass.col1.X = mA + mB + rA.Y * rA.Y * iA + rB.Y * rB.Y * iB;
  111. _mass.col2.X = -rA.Y * rA.X * iA - rB.Y * rB.X * iB;
  112. _mass.col3.X = -rA.Y * iA - rB.Y * iB;
  113. _mass.col1.Y = _mass.col2.X;
  114. _mass.col2.Y = mA + mB + rA.X * rA.X * iA + rB.X * rB.X * iB;
  115. _mass.col3.Y = rA.X * iA + rB.X * iB;
  116. _mass.col1.Z = _mass.col3.X;
  117. _mass.col2.Z = _mass.col3.Y;
  118. _mass.col3.Z = iA + iB;
  119. if (step.warmStarting)
  120. {
  121. // Scale impulses to support a variable time step.
  122. _impulse *= step.dtRatio;
  123. Vector2 P = new Vector2(_impulse.X, _impulse.Y);
  124. bA._linearVelocity -= mA * P;
  125. bA._angularVelocity -= iA * (MathUtils.Cross(rA, P) + _impulse.Z);
  126. bB._linearVelocity += mB * P;
  127. bB._angularVelocity += iB * (MathUtils.Cross(rB, P) + _impulse.Z);
  128. }
  129. else
  130. {
  131. _impulse = Vector3.Zero;
  132. }
  133. }
  134. internal override void SolveVelocityConstraints(ref TimeStep step)
  135. {
  136. Body bA = _bodyA;
  137. Body bB = _bodyB;
  138. Vector2 vA = bA._linearVelocity;
  139. float wA = bA._angularVelocity;
  140. Vector2 vB = bB._linearVelocity;
  141. float wB = bB._angularVelocity;
  142. float mA = bA._invMass, mB = bB._invMass;
  143. float iA = bA._invI, iB = bB._invI;
  144. Transform xfA, xfB;
  145. bA.GetTransform(out xfA);
  146. bB.GetTransform(out xfB);
  147. Vector2 rA = MathUtils.Multiply(ref xfA.R, _localAnchorA - bA.GetLocalCenter());
  148. Vector2 rB = MathUtils.Multiply(ref xfB.R, _localAnchorB - bB.GetLocalCenter());
  149. // Solve point-to-point constraint
  150. Vector2 Cdot1 = vB + MathUtils.Cross(wB, rB) - vA - MathUtils.Cross(wA, rA);
  151. float Cdot2 = wB - wA;
  152. Vector3 Cdot = new Vector3(Cdot1.X, Cdot1.Y, Cdot2);
  153. Vector3 impulse = _mass.Solve33(-Cdot);
  154. _impulse += impulse;
  155. Vector2 P = new Vector2(impulse.X, impulse.Y);
  156. vA -= mA * P;
  157. wA -= iA * (MathUtils.Cross(rA, P) + impulse.Z);
  158. vB += mB * P;
  159. wB += iB * (MathUtils.Cross(rB, P) + impulse.Z);
  160. bA._linearVelocity = vA;
  161. bA._angularVelocity = wA;
  162. bB._linearVelocity = vB;
  163. bB._angularVelocity = wB;
  164. }
  165. internal override bool SolvePositionConstraints(float baumgarte)
  166. {
  167. Body bA = _bodyA;
  168. Body bB = _bodyB;
  169. float mA = bA._invMass, mB = bB._invMass;
  170. float iA = bA._invI, iB = bB._invI;
  171. Transform xfA;
  172. Transform xfB;
  173. bA.GetTransform(out xfA);
  174. bB.GetTransform(out xfB);
  175. Vector2 rA = MathUtils.Multiply(ref xfA.R, _localAnchorA - bA.GetLocalCenter());
  176. Vector2 rB = MathUtils.Multiply(ref xfB.R, _localAnchorB - bB.GetLocalCenter());
  177. Vector2 C1 = bB._sweep.c + rB - bA._sweep.c - rA;
  178. float C2 = bB._sweep.a - bA._sweep.a - _referenceAngle;
  179. // Handle large detachment.
  180. const float k_allowedStretch = 10.0f * Settings.b2_linearSlop;
  181. float positionError = C1.Length();
  182. float angularError = Math.Abs(C2);
  183. if (positionError > k_allowedStretch)
  184. {
  185. iA *= 1.0f;
  186. iB *= 1.0f;
  187. }
  188. _mass.col1.X = mA + mB + rA.Y * rA.Y * iA + rB.Y * rB.Y * iB;
  189. _mass.col2.X = -rA.Y * rA.X * iA - rB.Y * rB.X * iB;
  190. _mass.col3.X = -rA.Y * iA - rB.Y * iB;
  191. _mass.col1.Y = _mass.col2.X;
  192. _mass.col2.Y = mA + mB + rA.X * rA.X * iA + rB.X * rB.X * iB;
  193. _mass.col3.Y = rA.X * iA + rB.X * iB;
  194. _mass.col1.Z = _mass.col3.X;
  195. _mass.col2.Z = _mass.col3.Y;
  196. _mass.col3.Z = iA + iB;
  197. Vector3 C = new Vector3(C1.X, C1.Y, C2);
  198. Vector3 impulse = _mass.Solve33(-C);
  199. Vector2 P = new Vector2(impulse.X, impulse.Y);
  200. bA._sweep.c -= mA * P;
  201. bA._sweep.a -= iA * (MathUtils.Cross(rA, P) + impulse.Z);
  202. bB._sweep.c += mB * P;
  203. bB._sweep.a += iB * (MathUtils.Cross(rB, P) + impulse.Z);
  204. bA.SynchronizeTransform();
  205. bB.SynchronizeTransform();
  206. return positionError <= Settings.b2_linearSlop && angularError <= Settings.b2_angularSlop;
  207. }
  208. internal Vector2 _localAnchorA;
  209. internal Vector2 _localAnchorB;
  210. internal float _referenceAngle;
  211. internal Vector3 _impulse;
  212. internal Mat33 _mass;
  213. };
  214. }