Math.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 System.Runtime.InteropServices;
  24. using Microsoft.Xna.Framework;
  25. namespace Box2D.XNA
  26. {
  27. public static class MathUtils
  28. {
  29. public static float Cross(Vector2 a, Vector2 b)
  30. {
  31. return a.X * b.Y - a.Y * b.X;
  32. }
  33. public static Vector2 Cross(Vector2 a, float s)
  34. {
  35. return new Vector2(s * a.Y, -s * a.X);
  36. }
  37. public static Vector2 Cross(float s, Vector2 a)
  38. {
  39. return new Vector2(-s * a.Y, s * a.X);
  40. }
  41. public static Vector2 Abs(Vector2 v)
  42. {
  43. return new Vector2(Math.Abs(v.X), Math.Abs(v.Y));
  44. }
  45. public static Vector2 Multiply(ref Mat22 A, Vector2 v)
  46. {
  47. return new Vector2(A.col1.X * v.X + A.col2.X * v.Y, A.col1.Y * v.X + A.col2.Y * v.Y);
  48. }
  49. public static Vector2 MultiplyT(ref Mat22 A, Vector2 v)
  50. {
  51. return new Vector2(Vector2.Dot(v, A.col1), Vector2.Dot(v, A.col2));
  52. }
  53. public static Vector2 Multiply(ref Transform T, Vector2 v)
  54. {
  55. float x = T.Position.X + T.R.col1.X * v.X + T.R.col2.X * v.Y;
  56. float y = T.Position.Y + T.R.col1.Y * v.X + T.R.col2.Y * v.Y;
  57. return new Vector2(x, y);
  58. }
  59. public static Vector2 MultiplyT(ref Transform T, Vector2 v)
  60. {
  61. return MultiplyT(ref T.R, v - T.Position);
  62. }
  63. // A^T * B
  64. public static void MultiplyT(ref Mat22 A, ref Mat22 B, out Mat22 C)
  65. {
  66. Vector2 c1 = new Vector2(Vector2.Dot(A.col1, B.col1), Vector2.Dot(A.col2, B.col1));
  67. Vector2 c2 = new Vector2(Vector2.Dot(A.col1, B.col2), Vector2.Dot(A.col2, B.col2));
  68. C = new Mat22(c1, c2);
  69. }
  70. public static void MultiplyT(ref Transform A, ref Transform B, out Transform C)
  71. {
  72. Mat22 R;
  73. MultiplyT(ref A.R, ref B.R, out R);
  74. C = new Transform(B.Position - A.Position, ref R);
  75. }
  76. public static void Swap<T>(ref T a, ref T b)
  77. {
  78. T tmp = a;
  79. a = b;
  80. b = tmp;
  81. }
  82. /// This function is used to ensure that a floating point number is
  83. /// not a NaN or infinity.
  84. public static bool IsValid(float x)
  85. {
  86. if (float.IsNaN(x))
  87. {
  88. // NaN.
  89. return false;
  90. }
  91. return !float.IsInfinity(x);
  92. }
  93. public static bool IsValid(this Vector2 x)
  94. {
  95. return IsValid(x.X) && IsValid(x.Y);
  96. }
  97. [StructLayout(LayoutKind.Explicit)]
  98. internal struct FloatConverter
  99. {
  100. [FieldOffset(0)]
  101. public float x;
  102. [FieldOffset(0)]
  103. public int i;
  104. };
  105. /// This is a approximate yet fast inverse square-root.
  106. public static float InvSqrt(float x)
  107. {
  108. FloatConverter convert = new FloatConverter();
  109. convert.x = x;
  110. float xhalf = 0.5f * x;
  111. convert.i = 0x5f3759df - (convert.i >> 1);
  112. x = convert.x;
  113. x = x * (1.5f - xhalf * x * x);
  114. return x;
  115. }
  116. public static int Clamp(int a, int low, int high)
  117. {
  118. return Math.Max(low, Math.Min(a, high));
  119. }
  120. public static float Clamp(float a, float low, float high)
  121. {
  122. return Math.Max(low, Math.Min(a, high));
  123. }
  124. public static Vector2 Clamp(Vector2 a, Vector2 low, Vector2 high)
  125. {
  126. return Vector2.Max(low, Vector2.Min(a, high));
  127. }
  128. }
  129. /// A 2-by-2 matrix. Stored in column-major order.
  130. public struct Mat22
  131. {
  132. /// construct this matrix using columns.
  133. public Mat22(Vector2 c1, Vector2 c2)
  134. {
  135. col1 = c1;
  136. col2 = c2;
  137. }
  138. /// construct this matrix using scalars.
  139. public Mat22(float a11, float a12, float a21, float a22)
  140. {
  141. col1 = new Vector2(a11, a21);
  142. col2 = new Vector2(a12, a22);
  143. }
  144. /// construct this matrix using an angle. This matrix becomes
  145. /// an orthonormal rotation matrix.
  146. public Mat22(float angle)
  147. {
  148. // TODO_ERIN compute sin+cos together.
  149. float c = (float)Math.Cos(angle), s = (float)Math.Sin(angle);
  150. col1 = new Vector2(c, s);
  151. col2 = new Vector2(-s, c);
  152. }
  153. /// Initialize this matrix using columns.
  154. public void Set(Vector2 c1, Vector2 c2)
  155. {
  156. col1 = c1;
  157. col2 = c2;
  158. }
  159. /// Initialize this matrix using an angle. This matrix becomes
  160. /// an orthonormal rotation matrix.
  161. public void Set(float angle)
  162. {
  163. float c = (float)Math.Cos(angle), s = (float)Math.Sin(angle);
  164. col1.X = c; col2.X = -s;
  165. col1.Y = s; col2.Y = c;
  166. }
  167. /// Set this to the identity matrix.
  168. public void SetIdentity()
  169. {
  170. col1.X = 1.0f; col2.X = 0.0f;
  171. col1.Y = 0.0f; col2.Y = 1.0f;
  172. }
  173. /// Set this matrix to all zeros.
  174. public void SetZero()
  175. {
  176. col1.X = 0.0f; col2.X = 0.0f;
  177. col1.Y = 0.0f; col2.Y = 0.0f;
  178. }
  179. /// Extract the angle from this matrix (assumed to be
  180. /// a rotation matrix).
  181. public float GetAngle()
  182. {
  183. return (float)Math.Atan2((double)col1.Y, (double)col1.X);
  184. }
  185. public Mat22 GetInverse()
  186. {
  187. float a = col1.X, b = col2.X, c = col1.Y, d = col2.Y;
  188. float det = a * d - b * c;
  189. if (det != 0.0f)
  190. {
  191. det = 1.0f / det;
  192. }
  193. return new Mat22(new Vector2(det * d, -det * c), new Vector2(-det * b, det * a));
  194. }
  195. /// Solve A * x = b, where b is a column vector. This is more efficient
  196. /// than computing the inverse in one-shot cases.
  197. public Vector2 Solve(Vector2 b)
  198. {
  199. float a11 = col1.X, a12 = col2.X, a21 = col1.Y, a22 = col2.Y;
  200. float det = a11 * a22 - a12 * a21;
  201. if (det != 0.0f)
  202. {
  203. det = 1.0f / det;
  204. }
  205. return new Vector2(det * (a22 * b.X - a12 * b.Y), det * (a11 * b.Y - a21 * b.X));
  206. }
  207. public static void Add (ref Mat22 A, ref Mat22 B, out Mat22 R)
  208. {
  209. R = new Mat22(A.col1 + B.col1, A.col2 + B.col2);
  210. }
  211. public Vector2 col1, col2;
  212. };
  213. /// A 3-by-3 matrix. Stored in column-major order.
  214. public struct Mat33
  215. {
  216. /// construct this matrix using columns.
  217. public Mat33(Vector3 c1, Vector3 c2, Vector3 c3)
  218. {
  219. col1 = c1;
  220. col2 = c2;
  221. col3 = c3;
  222. }
  223. /// Set this matrix to all zeros.
  224. public void SetZero()
  225. {
  226. col1 = Vector3.Zero;
  227. col2 = Vector3.Zero;
  228. col3 = Vector3.Zero;
  229. }
  230. /// Solve A * x = b, where b is a column vector. This is more efficient
  231. /// than computing the inverse in one-shot cases.
  232. public Vector3 Solve33(Vector3 b)
  233. {
  234. float det = Vector3.Dot(col1, Vector3.Cross(col2, col3));
  235. if (det != 0.0f)
  236. {
  237. det = 1.0f / det;
  238. }
  239. return new Vector3( det * Vector3.Dot(b, Vector3.Cross(col2, col3)),
  240. det * Vector3.Dot(col1, Vector3.Cross(b, col3)),
  241. det * Vector3.Dot(col1, Vector3.Cross(col2, b)));
  242. }
  243. /// Solve A * x = b, where b is a column vector. This is more efficient
  244. /// than computing the inverse in one-shot cases. Solve only the upper
  245. /// 2-by-2 matrix equation.
  246. public Vector2 Solve22(Vector2 b)
  247. {
  248. float a11 = col1.X, a12 = col2.X, a21 = col1.Y, a22 = col2.Y;
  249. float det = a11 * a22 - a12 * a21;
  250. if (det != 0.0f)
  251. {
  252. det = 1.0f / det;
  253. }
  254. return new Vector2(det * (a22 * b.X - a12 * b.Y), det * (a11 * b.Y - a21 * b.X));
  255. }
  256. public Vector3 col1, col2, col3;
  257. }
  258. /// A transform contains translation and rotation. It is used to represent
  259. /// the position and orientation of rigid frames.
  260. public struct Transform
  261. {
  262. /// Initialize using a position vector and a rotation matrix.
  263. public Transform(Vector2 position, ref Mat22 r)
  264. {
  265. Position = position;
  266. R = r;
  267. }
  268. /// Set this to the identity transform.
  269. public void SetIdentity()
  270. {
  271. Position = Vector2.Zero;
  272. R.SetIdentity();
  273. }
  274. /// Set this based on the position and angle.
  275. public void Set(Vector2 p, float angle)
  276. {
  277. Position = p;
  278. R.Set(angle);
  279. }
  280. /// Calculate the angle that the rotation matrix represents.
  281. public float GetAngle()
  282. {
  283. return (float)Math.Atan2((double)R.col1.Y, (double)R.col1.X);
  284. }
  285. public Vector2 Position;
  286. public Mat22 R;
  287. }
  288. /// This describes the motion of a body/shape for TOI computation.
  289. /// Shapes are defined with respect to the body origin, which may
  290. /// no coincide with the center of mass. However, to support dynamics
  291. /// we must interpolate the center of mass position.
  292. public struct Sweep
  293. {
  294. /// Get the interpolated transform at a specific time.
  295. /// @param alpha is a factor in [0,1], where 0 indicates t0.
  296. public void GetTransform(out Transform xf, float alpha)
  297. {
  298. xf = new Transform();
  299. xf.Position = (1.0f - alpha) * c0 + alpha * c;
  300. float angle = (1.0f - alpha) * a0 + alpha * a;
  301. xf.R.Set(angle);
  302. // Shift to origin
  303. xf.Position -= MathUtils.Multiply(ref xf.R, localCenter);
  304. }
  305. /// Advance the sweep forward, yielding a new initial state.
  306. /// @param t the new initial time.
  307. public void Advance(float t)
  308. {
  309. c0 = (1.0f - t) * c0 + t * c;
  310. a0 = (1.0f - t) * a0 + t * a;
  311. }
  312. /// Normalize the angles.
  313. public void Normalize()
  314. {
  315. float twoPi = 2.0f * (float)Math.PI;
  316. float d = twoPi * (float)Math.Floor(a0 / twoPi);
  317. a0 -= d;
  318. a -= d;
  319. }
  320. public Vector2 localCenter; ///< local center of mass position
  321. public Vector2 c0, c; ///< center world positions
  322. public float a0, a; ///< world angles
  323. }
  324. }