LineJoint.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. public class LineJoint : Joint
  32. {
  33. private Vector2 _ax, _ay;
  34. private float _bias;
  35. private bool _enableMotor;
  36. private float _gamma;
  37. private float _impulse;
  38. private Vector2 _localXAxis;
  39. private Vector2 _localYAxisA;
  40. private float _mass;
  41. private float _maxMotorTorque;
  42. private float _motorImpulse;
  43. private float _motorMass;
  44. private float _motorSpeed;
  45. private float _sAx;
  46. private float _sAy;
  47. private float _sBx;
  48. private float _sBy;
  49. private float _springImpulse;
  50. private float _springMass;
  51. // Linear constraint (point-to-line)
  52. // d = pB - pA = xB + rB - xA - rA
  53. // C = dot(ay, d)
  54. // Cdot = dot(d, cross(wA, ay)) + dot(ay, vB + cross(wB, rB) - vA - cross(wA, rA))
  55. // = -dot(ay, vA) - dot(cross(d + rA, ay), wA) + dot(ay, vB) + dot(cross(rB, ay), vB)
  56. // J = [-ay, -cross(d + rA, ay), ay, cross(rB, ay)]
  57. // Spring linear constraint
  58. // C = dot(ax, d)
  59. // Cdot = = -dot(ax, vA) - dot(cross(d + rA, ax), wA) + dot(ax, vB) + dot(cross(rB, ax), vB)
  60. // J = [-ax -cross(d+rA, ax) ax cross(rB, ax)]
  61. // Motor rotational constraint
  62. // Cdot = wB - wA
  63. // J = [0 0 -1 0 0 1]
  64. internal LineJoint()
  65. {
  66. JointType = JointType.Line;
  67. }
  68. public LineJoint(Body bA, Body bB, Vector2 anchor, Vector2 axis)
  69. : base(bA, bB)
  70. {
  71. JointType = JointType.Line;
  72. LocalAnchorA = bA.GetLocalPoint(anchor);
  73. LocalAnchorB = bB.GetLocalPoint(anchor);
  74. LocalXAxis = bA.GetLocalVector(axis);
  75. }
  76. public Vector2 LocalAnchorA { get; set; }
  77. public Vector2 LocalAnchorB { get; set; }
  78. public override Vector2 WorldAnchorA
  79. {
  80. get { return BodyA.GetWorldPoint(LocalAnchorA); }
  81. }
  82. public override Vector2 WorldAnchorB
  83. {
  84. get { return BodyB.GetWorldPoint(LocalAnchorB); }
  85. set { Debug.Assert(false, "You can't set the world anchor on this joint type."); }
  86. }
  87. public float JointTranslation
  88. {
  89. get
  90. {
  91. Body bA = BodyA;
  92. Body bB = BodyB;
  93. Vector2 pA = bA.GetWorldPoint(LocalAnchorA);
  94. Vector2 pB = bB.GetWorldPoint(LocalAnchorB);
  95. Vector2 d = pB - pA;
  96. Vector2 axis = bA.GetWorldVector(LocalXAxis);
  97. float translation = Vector2.Dot(d, axis);
  98. return translation;
  99. }
  100. }
  101. public float JointSpeed
  102. {
  103. get
  104. {
  105. float wA = BodyA.AngularVelocityInternal;
  106. float wB = BodyB.AngularVelocityInternal;
  107. return wB - wA;
  108. }
  109. }
  110. public bool MotorEnabled
  111. {
  112. get { return _enableMotor; }
  113. set
  114. {
  115. BodyA.Awake = true;
  116. BodyB.Awake = true;
  117. _enableMotor = value;
  118. }
  119. }
  120. public float MotorSpeed
  121. {
  122. set
  123. {
  124. BodyA.Awake = true;
  125. BodyB.Awake = true;
  126. _motorSpeed = value;
  127. }
  128. get { return _motorSpeed; }
  129. }
  130. public float MaxMotorTorque
  131. {
  132. set
  133. {
  134. BodyA.Awake = true;
  135. BodyB.Awake = true;
  136. _maxMotorTorque = value;
  137. }
  138. get { return _maxMotorTorque; }
  139. }
  140. public float Frequency { get; set; }
  141. public float DampingRatio { get; set; }
  142. public Vector2 LocalXAxis
  143. {
  144. get { return _localXAxis; }
  145. set
  146. {
  147. _localXAxis = value;
  148. _localYAxisA = MathUtils.Cross(1.0f, _localXAxis);
  149. }
  150. }
  151. public override Vector2 GetReactionForce(float invDt)
  152. {
  153. return invDt * (_impulse * _ay + _springImpulse * _ax);
  154. }
  155. public override float GetReactionTorque(float invDt)
  156. {
  157. return invDt * _motorImpulse;
  158. }
  159. internal override void InitVelocityConstraints(ref TimeStep step)
  160. {
  161. Body bA = BodyA;
  162. Body bB = BodyB;
  163. LocalCenterA = bA.LocalCenter;
  164. LocalCenterB = bB.LocalCenter;
  165. Transform xfA;
  166. bA.GetTransform(out xfA);
  167. Transform xfB;
  168. bB.GetTransform(out xfB);
  169. // Compute the effective masses.
  170. Vector2 rA = MathUtils.Multiply(ref xfA.R, LocalAnchorA - LocalCenterA);
  171. Vector2 rB = MathUtils.Multiply(ref xfB.R, LocalAnchorB - LocalCenterB);
  172. Vector2 d = bB.Sweep.C + rB - bA.Sweep.C - rA;
  173. InvMassA = bA.InvMass;
  174. InvIA = bA.InvI;
  175. InvMassB = bB.InvMass;
  176. InvIB = bB.InvI;
  177. // Point to line constraint
  178. {
  179. _ay = MathUtils.Multiply(ref xfA.R, _localYAxisA);
  180. _sAy = MathUtils.Cross(d + rA, _ay);
  181. _sBy = MathUtils.Cross(rB, _ay);
  182. _mass = InvMassA + InvMassB + InvIA * _sAy * _sAy + InvIB * _sBy * _sBy;
  183. if (_mass > 0.0f)
  184. {
  185. _mass = 1.0f / _mass;
  186. }
  187. }
  188. // Spring constraint
  189. _springMass = 0.0f;
  190. if (Frequency > 0.0f)
  191. {
  192. _ax = MathUtils.Multiply(ref xfA.R, LocalXAxis);
  193. _sAx = MathUtils.Cross(d + rA, _ax);
  194. _sBx = MathUtils.Cross(rB, _ax);
  195. float invMass = InvMassA + InvMassB + InvIA * _sAx * _sAx + InvIB * _sBx * _sBx;
  196. if (invMass > 0.0f)
  197. {
  198. _springMass = 1.0f / invMass;
  199. float C = Vector2.Dot(d, _ax);
  200. // Frequency
  201. float omega = 2.0f * Settings.Pi * Frequency;
  202. // Damping coefficient
  203. float da = 2.0f * _springMass * DampingRatio * omega;
  204. // Spring stiffness
  205. float k = _springMass * omega * omega;
  206. // magic formulas
  207. _gamma = step.dt * (da + step.dt * k);
  208. if (_gamma > 0.0f)
  209. {
  210. _gamma = 1.0f / _gamma;
  211. }
  212. _bias = C * step.dt * k * _gamma;
  213. _springMass = invMass + _gamma;
  214. if (_springMass > 0.0f)
  215. {
  216. _springMass = 1.0f / _springMass;
  217. }
  218. }
  219. }
  220. else
  221. {
  222. _springImpulse = 0.0f;
  223. _springMass = 0.0f;
  224. }
  225. // Rotational motor
  226. if (_enableMotor)
  227. {
  228. _motorMass = InvIA + InvIB;
  229. if (_motorMass > 0.0f)
  230. {
  231. _motorMass = 1.0f / _motorMass;
  232. }
  233. }
  234. else
  235. {
  236. _motorMass = 0.0f;
  237. _motorImpulse = 0.0f;
  238. }
  239. if (Settings.EnableWarmstarting)
  240. {
  241. // Account for variable time step.
  242. _impulse *= step.dtRatio;
  243. _springImpulse *= step.dtRatio;
  244. _motorImpulse *= step.dtRatio;
  245. Vector2 P = _impulse * _ay + _springImpulse * _ax;
  246. float LA = _impulse * _sAy + _springImpulse * _sAx + _motorImpulse;
  247. float LB = _impulse * _sBy + _springImpulse * _sBx + _motorImpulse;
  248. bA.LinearVelocityInternal -= InvMassA * P;
  249. bA.AngularVelocityInternal -= InvIA * LA;
  250. bB.LinearVelocityInternal += InvMassB * P;
  251. bB.AngularVelocityInternal += InvIB * LB;
  252. }
  253. else
  254. {
  255. _impulse = 0.0f;
  256. _springImpulse = 0.0f;
  257. _motorImpulse = 0.0f;
  258. }
  259. }
  260. internal override void SolveVelocityConstraints(ref TimeStep step)
  261. {
  262. Body bA = BodyA;
  263. Body bB = BodyB;
  264. Vector2 vA = bA.LinearVelocity;
  265. float wA = bA.AngularVelocityInternal;
  266. Vector2 vB = bB.LinearVelocityInternal;
  267. float wB = bB.AngularVelocityInternal;
  268. // Solve spring constraint
  269. {
  270. float Cdot = Vector2.Dot(_ax, vB - vA) + _sBx * wB - _sAx * wA;
  271. float impulse = -_springMass * (Cdot + _bias + _gamma * _springImpulse);
  272. _springImpulse += impulse;
  273. Vector2 P = impulse * _ax;
  274. float LA = impulse * _sAx;
  275. float LB = impulse * _sBx;
  276. vA -= InvMassA * P;
  277. wA -= InvIA * LA;
  278. vB += InvMassB * P;
  279. wB += InvIB * LB;
  280. }
  281. // Solve rotational motor constraint
  282. {
  283. float Cdot = wB - wA - _motorSpeed;
  284. float impulse = -_motorMass * Cdot;
  285. float oldImpulse = _motorImpulse;
  286. float maxImpulse = step.dt * _maxMotorTorque;
  287. _motorImpulse = MathUtils.Clamp(_motorImpulse + impulse, -maxImpulse, maxImpulse);
  288. impulse = _motorImpulse - oldImpulse;
  289. wA -= InvIA * impulse;
  290. wB += InvIB * impulse;
  291. }
  292. // Solve point to line constraint
  293. {
  294. float Cdot = Vector2.Dot(_ay, vB - vA) + _sBy * wB - _sAy * wA;
  295. float impulse = _mass * (-Cdot);
  296. _impulse += impulse;
  297. Vector2 P = impulse * _ay;
  298. float LA = impulse * _sAy;
  299. float LB = impulse * _sBy;
  300. vA -= InvMassA * P;
  301. wA -= InvIA * LA;
  302. vB += InvMassB * P;
  303. wB += InvIB * LB;
  304. }
  305. bA.LinearVelocityInternal = vA;
  306. bA.AngularVelocityInternal = wA;
  307. bB.LinearVelocityInternal = vB;
  308. bB.AngularVelocityInternal = wB;
  309. }
  310. internal override bool SolvePositionConstraints()
  311. {
  312. Body bA = BodyA;
  313. Body bB = BodyB;
  314. Vector2 xA = bA.Sweep.C;
  315. float angleA = bA.Sweep.A;
  316. Vector2 xB = bB.Sweep.C;
  317. float angleB = bB.Sweep.A;
  318. Mat22 RA = new Mat22(angleA);
  319. Mat22 RB = new Mat22(angleB);
  320. Vector2 rA = MathUtils.Multiply(ref RA, LocalAnchorA - LocalCenterA);
  321. Vector2 rB = MathUtils.Multiply(ref RB, LocalAnchorB - LocalCenterB);
  322. Vector2 d = xB + rB - xA - rA;
  323. Vector2 ay = MathUtils.Multiply(ref RA, _localYAxisA);
  324. float sAy = MathUtils.Cross(d + rA, ay);
  325. float sBy = MathUtils.Cross(rB, ay);
  326. float C = Vector2.Dot(d, ay);
  327. float k = InvMassA + InvMassB + InvIA * _sAy * _sAy + InvIB * _sBy * _sBy;
  328. float impulse;
  329. if (k != 0.0f)
  330. {
  331. impulse = -C / k;
  332. }
  333. else
  334. {
  335. impulse = 0.0f;
  336. }
  337. Vector2 P = impulse * ay;
  338. float LA = impulse * sAy;
  339. float LB = impulse * sBy;
  340. xA -= InvMassA * P;
  341. angleA -= InvIA * LA;
  342. xB += InvMassB * P;
  343. angleB += InvIB * LB;
  344. // TODO_ERIN remove need for this.
  345. bA.Sweep.C = xA;
  346. bA.Sweep.A = angleA;
  347. bB.Sweep.C = xB;
  348. bB.Sweep.A = angleB;
  349. bA.SynchronizeTransform();
  350. bB.SynchronizeTransform();
  351. return Math.Abs(C) <= Settings.LinearSlop;
  352. }
  353. public float GetMotorTorque(float invDt)
  354. {
  355. return invDt * _motorImpulse;
  356. }
  357. }
  358. }