b2Math.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it
  9. * freely, subject to the following restrictions:
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software
  12. * in a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. * 2. Altered source versions must be plainly marked as such, and must not be
  15. * misrepresented as being the original software.
  16. * 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef B2_MATH_H
  19. #define B2_MATH_H
  20. #include <Box2D/Common/b2Settings.h>
  21. #include <cmath>
  22. #include <cfloat>
  23. #include <cstddef>
  24. #include <limits>
  25. /// This function is used to ensure that a floating point number is
  26. /// not a NaN or infinity.
  27. inline bool b2IsValid(float32 x)
  28. {
  29. if (x != x)
  30. {
  31. // NaN.
  32. return false;
  33. }
  34. float32 infinity = std::numeric_limits<float32>::infinity();
  35. return -infinity < x && x < infinity;
  36. }
  37. /// This is a approximate yet fast inverse square-root.
  38. inline float32 b2InvSqrt(float32 x)
  39. {
  40. union
  41. {
  42. float32 x;
  43. int32 i;
  44. } convert;
  45. convert.x = x;
  46. float32 xhalf = 0.5f * x;
  47. convert.i = 0x5f3759df - (convert.i >> 1);
  48. x = convert.x;
  49. x = x * (1.5f - xhalf * x * x);
  50. return x;
  51. }
  52. #define b2Sqrt(x) std::sqrt(x)
  53. #define b2Atan2(y, x) std::atan2(y, x)
  54. /// A 2D column vector.
  55. struct b2Vec2
  56. {
  57. /// Default constructor does nothing (for performance).
  58. b2Vec2() {}
  59. /// Construct using coordinates.
  60. b2Vec2(float32 x, float32 y) : x(x), y(y) {}
  61. /// Set this vector to all zeros.
  62. void SetZero() { x = 0.0f; y = 0.0f; }
  63. /// Set this vector to some specified coordinates.
  64. void Set(float32 x_, float32 y_) { x = x_; y = y_; }
  65. /// Negate this vector.
  66. b2Vec2 operator -() const { b2Vec2 v; v.Set(-x, -y); return v; }
  67. /// Read from and indexed element.
  68. float32 operator () (int32 i) const
  69. {
  70. return (&x)[i];
  71. }
  72. /// Write to an indexed element.
  73. float32& operator () (int32 i)
  74. {
  75. return (&x)[i];
  76. }
  77. /// Add a vector to this vector.
  78. void operator += (const b2Vec2& v)
  79. {
  80. x += v.x; y += v.y;
  81. }
  82. /// Subtract a vector from this vector.
  83. void operator -= (const b2Vec2& v)
  84. {
  85. x -= v.x; y -= v.y;
  86. }
  87. /// Multiply this vector by a scalar.
  88. void operator *= (float32 a)
  89. {
  90. x *= a; y *= a;
  91. }
  92. /// Get the length of this vector (the norm).
  93. float32 Length() const
  94. {
  95. return b2Sqrt(x * x + y * y);
  96. }
  97. /// Get the length squared. For performance, use this instead of
  98. /// b2Vec2::Length (if possible).
  99. float32 LengthSquared() const
  100. {
  101. return x * x + y * y;
  102. }
  103. /// Convert this vector into a unit vector. Returns the length.
  104. float32 Normalize()
  105. {
  106. float32 length = Length();
  107. if (length < b2_epsilon)
  108. {
  109. return 0.0f;
  110. }
  111. float32 invLength = 1.0f / length;
  112. x *= invLength;
  113. y *= invLength;
  114. return length;
  115. }
  116. /// Does this vector contain finite coordinates?
  117. bool IsValid() const
  118. {
  119. return b2IsValid(x) && b2IsValid(y);
  120. }
  121. /// Get the skew vector such that dot(skew_vec, other) == cross(vec, other)
  122. b2Vec2 Skew() const
  123. {
  124. return b2Vec2(-y, x);
  125. }
  126. float32 x, y;
  127. };
  128. /// A 2D column vector with 3 elements.
  129. struct b2Vec3
  130. {
  131. /// Default constructor does nothing (for performance).
  132. b2Vec3() {}
  133. /// Construct using coordinates.
  134. b2Vec3(float32 x, float32 y, float32 z) : x(x), y(y), z(z) {}
  135. /// Set this vector to all zeros.
  136. void SetZero() { x = 0.0f; y = 0.0f; z = 0.0f; }
  137. /// Set this vector to some specified coordinates.
  138. void Set(float32 x_, float32 y_, float32 z_) { x = x_; y = y_; z = z_; }
  139. /// Negate this vector.
  140. b2Vec3 operator -() const { b2Vec3 v; v.Set(-x, -y, -z); return v; }
  141. /// Add a vector to this vector.
  142. void operator += (const b2Vec3& v)
  143. {
  144. x += v.x; y += v.y; z += v.z;
  145. }
  146. /// Subtract a vector from this vector.
  147. void operator -= (const b2Vec3& v)
  148. {
  149. x -= v.x; y -= v.y; z -= v.z;
  150. }
  151. /// Multiply this vector by a scalar.
  152. void operator *= (float32 s)
  153. {
  154. x *= s; y *= s; z *= s;
  155. }
  156. float32 x, y, z;
  157. };
  158. /// A 2-by-2 matrix. Stored in column-major order.
  159. struct b2Mat22
  160. {
  161. /// The default constructor does nothing (for performance).
  162. b2Mat22() {}
  163. /// Construct this matrix using columns.
  164. b2Mat22(const b2Vec2& c1, const b2Vec2& c2)
  165. {
  166. ex = c1;
  167. ey = c2;
  168. }
  169. /// Construct this matrix using scalars.
  170. b2Mat22(float32 a11, float32 a12, float32 a21, float32 a22)
  171. {
  172. ex.x = a11; ex.y = a21;
  173. ey.x = a12; ey.y = a22;
  174. }
  175. /// Initialize this matrix using columns.
  176. void Set(const b2Vec2& c1, const b2Vec2& c2)
  177. {
  178. ex = c1;
  179. ey = c2;
  180. }
  181. /// Set this to the identity matrix.
  182. void SetIdentity()
  183. {
  184. ex.x = 1.0f; ey.x = 0.0f;
  185. ex.y = 0.0f; ey.y = 1.0f;
  186. }
  187. /// Set this matrix to all zeros.
  188. void SetZero()
  189. {
  190. ex.x = 0.0f; ey.x = 0.0f;
  191. ex.y = 0.0f; ey.y = 0.0f;
  192. }
  193. b2Mat22 GetInverse() const
  194. {
  195. float32 a = ex.x, b = ey.x, c = ex.y, d = ey.y;
  196. b2Mat22 B;
  197. float32 det = a * d - b * c;
  198. if (det != 0.0f)
  199. {
  200. det = 1.0f / det;
  201. }
  202. B.ex.x = det * d; B.ey.x = -det * b;
  203. B.ex.y = -det * c; B.ey.y = det * a;
  204. return B;
  205. }
  206. /// Solve A * x = b, where b is a column vector. This is more efficient
  207. /// than computing the inverse in one-shot cases.
  208. b2Vec2 Solve(const b2Vec2& b) const
  209. {
  210. float32 a11 = ex.x, a12 = ey.x, a21 = ex.y, a22 = ey.y;
  211. float32 det = a11 * a22 - a12 * a21;
  212. if (det != 0.0f)
  213. {
  214. det = 1.0f / det;
  215. }
  216. b2Vec2 x;
  217. x.x = det * (a22 * b.x - a12 * b.y);
  218. x.y = det * (a11 * b.y - a21 * b.x);
  219. return x;
  220. }
  221. b2Vec2 ex, ey;
  222. };
  223. /// A 3-by-3 matrix. Stored in column-major order.
  224. struct b2Mat33
  225. {
  226. /// The default constructor does nothing (for performance).
  227. b2Mat33() {}
  228. /// Construct this matrix using columns.
  229. b2Mat33(const b2Vec3& c1, const b2Vec3& c2, const b2Vec3& c3)
  230. {
  231. ex = c1;
  232. ey = c2;
  233. ez = c3;
  234. }
  235. /// Set this matrix to all zeros.
  236. void SetZero()
  237. {
  238. ex.SetZero();
  239. ey.SetZero();
  240. ez.SetZero();
  241. }
  242. /// Solve A * x = b, where b is a column vector. This is more efficient
  243. /// than computing the inverse in one-shot cases.
  244. b2Vec3 Solve33(const b2Vec3& b) const;
  245. /// Solve A * x = b, where b is a column vector. This is more efficient
  246. /// than computing the inverse in one-shot cases. Solve only the upper
  247. /// 2-by-2 matrix equation.
  248. b2Vec2 Solve22(const b2Vec2& b) const;
  249. b2Vec3 ex, ey, ez;
  250. };
  251. /// Rotation
  252. struct b2Rot
  253. {
  254. b2Rot() {}
  255. /// Initialize from an angle in radians
  256. explicit b2Rot(float32 angle)
  257. {
  258. /// TODO_ERIN optimize
  259. s = sinf(angle);
  260. c = cosf(angle);
  261. }
  262. /// Set using an angle in radians.
  263. void Set(float32 angle)
  264. {
  265. /// TODO_ERIN optimize
  266. s = sinf(angle);
  267. c = cosf(angle);
  268. }
  269. /// Set to the identity rotation
  270. void SetIdentity()
  271. {
  272. s = 0.0f;
  273. c = 1.0f;
  274. }
  275. /// Get the angle in radians
  276. float32 GetAngle() const
  277. {
  278. return b2Atan2(s, c);
  279. }
  280. /// Get the x-axis
  281. b2Vec2 GetXAxis() const
  282. {
  283. return b2Vec2(c, s);
  284. }
  285. /// Get the u-axis
  286. b2Vec2 GetYAxis() const
  287. {
  288. return b2Vec2(-s, c);
  289. }
  290. /// Sine and cosine
  291. float32 s, c;
  292. };
  293. /// A transform contains translation and rotation. It is used to represent
  294. /// the position and orientation of rigid frames.
  295. struct b2Transform
  296. {
  297. /// The default constructor does nothing.
  298. b2Transform() {}
  299. /// Initialize using a position vector and a rotation.
  300. b2Transform(const b2Vec2& position, const b2Rot& rotation) : p(position), q(rotation) {}
  301. /// Set this to the identity transform.
  302. void SetIdentity()
  303. {
  304. p.SetZero();
  305. q.SetIdentity();
  306. }
  307. /// Set this based on the position and angle.
  308. void Set(const b2Vec2& position, float32 angle)
  309. {
  310. p = position;
  311. q.Set(angle);
  312. }
  313. b2Vec2 p;
  314. b2Rot q;
  315. };
  316. /// This describes the motion of a body/shape for TOI computation.
  317. /// Shapes are defined with respect to the body origin, which may
  318. /// no coincide with the center of mass. However, to support dynamics
  319. /// we must interpolate the center of mass position.
  320. struct b2Sweep
  321. {
  322. /// Get the interpolated transform at a specific time.
  323. /// @param beta is a factor in [0,1], where 0 indicates alpha0.
  324. void GetTransform(b2Transform* xfb, float32 beta) const;
  325. /// Advance the sweep forward, yielding a new initial state.
  326. /// @param alpha the new initial time.
  327. void Advance(float32 alpha);
  328. /// Normalize the angles.
  329. void Normalize();
  330. b2Vec2 localCenter; ///< local center of mass position
  331. b2Vec2 c0, c; ///< center world positions
  332. float32 a0, a; ///< world angles
  333. /// Fraction of the current time step in the range [0,1]
  334. /// c0 and a0 are the positions at alpha0.
  335. float32 alpha0;
  336. };
  337. /// Useful constant
  338. extern const b2Vec2 b2Vec2_zero;
  339. /// Perform the dot product on two vectors.
  340. inline float32 b2Dot(const b2Vec2& a, const b2Vec2& b)
  341. {
  342. return a.x * b.x + a.y * b.y;
  343. }
  344. /// Perform the cross product on two vectors. In 2D this produces a scalar.
  345. inline float32 b2Cross(const b2Vec2& a, const b2Vec2& b)
  346. {
  347. return a.x * b.y - a.y * b.x;
  348. }
  349. /// Perform the cross product on a vector and a scalar. In 2D this produces
  350. /// a vector.
  351. inline b2Vec2 b2Cross(const b2Vec2& a, float32 s)
  352. {
  353. return b2Vec2(s * a.y, -s * a.x);
  354. }
  355. /// Perform the cross product on a scalar and a vector. In 2D this produces
  356. /// a vector.
  357. inline b2Vec2 b2Cross(float32 s, const b2Vec2& a)
  358. {
  359. return b2Vec2(-s * a.y, s * a.x);
  360. }
  361. /// Multiply a matrix times a vector. If a rotation matrix is provided,
  362. /// then this transforms the vector from one frame to another.
  363. inline b2Vec2 b2Mul(const b2Mat22& A, const b2Vec2& v)
  364. {
  365. return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
  366. }
  367. /// Multiply a matrix transpose times a vector. If a rotation matrix is provided,
  368. /// then this transforms the vector from one frame to another (inverse transform).
  369. inline b2Vec2 b2MulT(const b2Mat22& A, const b2Vec2& v)
  370. {
  371. return b2Vec2(b2Dot(v, A.ex), b2Dot(v, A.ey));
  372. }
  373. /// Add two vectors component-wise.
  374. inline b2Vec2 operator + (const b2Vec2& a, const b2Vec2& b)
  375. {
  376. return b2Vec2(a.x + b.x, a.y + b.y);
  377. }
  378. /// Subtract two vectors component-wise.
  379. inline b2Vec2 operator - (const b2Vec2& a, const b2Vec2& b)
  380. {
  381. return b2Vec2(a.x - b.x, a.y - b.y);
  382. }
  383. inline b2Vec2 operator * (float32 s, const b2Vec2& a)
  384. {
  385. return b2Vec2(s * a.x, s * a.y);
  386. }
  387. inline bool operator == (const b2Vec2& a, const b2Vec2& b)
  388. {
  389. return a.x == b.x && a.y == b.y;
  390. }
  391. inline float32 b2Distance(const b2Vec2& a, const b2Vec2& b)
  392. {
  393. b2Vec2 c = a - b;
  394. return c.Length();
  395. }
  396. inline float32 b2DistanceSquared(const b2Vec2& a, const b2Vec2& b)
  397. {
  398. b2Vec2 c = a - b;
  399. return b2Dot(c, c);
  400. }
  401. inline b2Vec3 operator * (float32 s, const b2Vec3& a)
  402. {
  403. return b2Vec3(s * a.x, s * a.y, s * a.z);
  404. }
  405. /// Add two vectors component-wise.
  406. inline b2Vec3 operator + (const b2Vec3& a, const b2Vec3& b)
  407. {
  408. return b2Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
  409. }
  410. /// Subtract two vectors component-wise.
  411. inline b2Vec3 operator - (const b2Vec3& a, const b2Vec3& b)
  412. {
  413. return b2Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
  414. }
  415. /// Perform the dot product on two vectors.
  416. inline float32 b2Dot(const b2Vec3& a, const b2Vec3& b)
  417. {
  418. return a.x * b.x + a.y * b.y + a.z * b.z;
  419. }
  420. /// Perform the cross product on two vectors.
  421. inline b2Vec3 b2Cross(const b2Vec3& a, const b2Vec3& b)
  422. {
  423. return b2Vec3(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
  424. }
  425. inline b2Mat22 operator + (const b2Mat22& A, const b2Mat22& B)
  426. {
  427. return b2Mat22(A.ex + B.ex, A.ey + B.ey);
  428. }
  429. // A * B
  430. inline b2Mat22 b2Mul(const b2Mat22& A, const b2Mat22& B)
  431. {
  432. return b2Mat22(b2Mul(A, B.ex), b2Mul(A, B.ey));
  433. }
  434. // A^T * B
  435. inline b2Mat22 b2MulT(const b2Mat22& A, const b2Mat22& B)
  436. {
  437. b2Vec2 c1(b2Dot(A.ex, B.ex), b2Dot(A.ey, B.ex));
  438. b2Vec2 c2(b2Dot(A.ex, B.ey), b2Dot(A.ey, B.ey));
  439. return b2Mat22(c1, c2);
  440. }
  441. /// Multiply a matrix times a vector.
  442. inline b2Vec3 b2Mul(const b2Mat33& A, const b2Vec3& v)
  443. {
  444. return v.x * A.ex + v.y * A.ey + v.z * A.ez;
  445. }
  446. /// Multiply two rotations: q * r
  447. inline b2Rot b2Mul(const b2Rot& q, const b2Rot& r)
  448. {
  449. // [qc -qs] * [rc -rs] = [qc*rc-qs*rs -qc*rs-qs*rc]
  450. // [qs qc] [rs rc] [qs*rc+qc*rs -qs*rs+qc*rc]
  451. // s = qs * rc + qc * rs
  452. // c = qc * rc - qs * rs
  453. b2Rot qr;
  454. qr.s = q.s * r.c + q.c * r.s;
  455. qr.c = q.c * r.c - q.s * r.s;
  456. return qr;
  457. }
  458. /// Transpose multiply two rotations: qT * r
  459. inline b2Rot b2MulT(const b2Rot& q, const b2Rot& r)
  460. {
  461. // [ qc qs] * [rc -rs] = [qc*rc-qs*rs -qc*rs-qs*rc]
  462. // [-qs qc] [rs rc] [qs*rc+qc*rs -qs*rs+qc*rc]
  463. // s = qs * rc + qc * rs
  464. // c = qc * rc - qs * rs
  465. b2Rot qr;
  466. qr.s = q.s * r.c + q.c * r.s;
  467. qr.c = q.c * r.c - q.s * r.s;
  468. return qr;
  469. }
  470. /// Rotate a vector
  471. inline b2Vec2 b2Mul(const b2Rot& q, const b2Vec2& v)
  472. {
  473. return b2Vec2(q.c * v.x - q.s * v.y, q.s * v.x + q.c * v.y);
  474. }
  475. /// Inverse rotate a vector
  476. inline b2Vec2 b2MulT(const b2Rot& q, const b2Vec2& v)
  477. {
  478. return b2Vec2(q.c * v.x + q.s * v.y, -q.s * v.x + q.c * v.y);
  479. }
  480. inline b2Vec2 b2Mul(const b2Transform& T, const b2Vec2& v)
  481. {
  482. float32 x = (T.q.c * v.x - T.q.s * v.y) + T.p.x;
  483. float32 y = (T.q.s * v.x + T.q.c * v.y) + T.p.y;
  484. return b2Vec2(x, y);
  485. }
  486. inline b2Vec2 b2MulT(const b2Transform& T, const b2Vec2& v)
  487. {
  488. float32 px = v.x - T.p.x;
  489. float32 py = v.y - T.p.y;
  490. float32 x = (T.q.c * px + T.q.s * py);
  491. float32 y = (-T.q.s * px + T.q.c * py);
  492. return b2Vec2(x, y);
  493. }
  494. // v2 = A.q.Rot(B.q.Rot(v1) + B.p) + A.p
  495. // = (A.q * B.q).Rot(v1) + A.q.Rot(B.p) + A.p
  496. inline b2Transform b2Mul(const b2Transform& A, const b2Transform& B)
  497. {
  498. b2Transform C;
  499. C.q = b2Mul(A.q, B.q);
  500. C.p = b2Mul(A.q, B.p) + A.p;
  501. return C;
  502. }
  503. // v2 = A.q' * (B.q * v1 + B.p - A.p)
  504. // = A.q' * B.q * v1 + A.q' * (B.p - A.p)
  505. inline b2Transform b2MulT(const b2Transform& A, const b2Transform& B)
  506. {
  507. b2Transform C;
  508. C.q = b2MulT(A.q, B.q);
  509. C.p = b2MulT(A.q, B.p - A.p);
  510. return C;
  511. }
  512. template <typename T>
  513. inline T b2Abs(T a)
  514. {
  515. return a > T(0) ? a : -a;
  516. }
  517. inline b2Vec2 b2Abs(const b2Vec2& a)
  518. {
  519. return b2Vec2(b2Abs(a.x), b2Abs(a.y));
  520. }
  521. inline b2Mat22 b2Abs(const b2Mat22& A)
  522. {
  523. return b2Mat22(b2Abs(A.ex), b2Abs(A.ey));
  524. }
  525. template <typename T>
  526. inline T b2Min(T a, T b)
  527. {
  528. return a < b ? a : b;
  529. }
  530. inline b2Vec2 b2Min(const b2Vec2& a, const b2Vec2& b)
  531. {
  532. return b2Vec2(b2Min(a.x, b.x), b2Min(a.y, b.y));
  533. }
  534. template <typename T>
  535. inline T b2Max(T a, T b)
  536. {
  537. return a > b ? a : b;
  538. }
  539. inline b2Vec2 b2Max(const b2Vec2& a, const b2Vec2& b)
  540. {
  541. return b2Vec2(b2Max(a.x, b.x), b2Max(a.y, b.y));
  542. }
  543. template <typename T>
  544. inline T b2Clamp(T a, T low, T high)
  545. {
  546. return b2Max(low, b2Min(a, high));
  547. }
  548. inline b2Vec2 b2Clamp(const b2Vec2& a, const b2Vec2& low, const b2Vec2& high)
  549. {
  550. return b2Max(low, b2Min(a, high));
  551. }
  552. template<typename T> inline void b2Swap(T& a, T& b)
  553. {
  554. T tmp = a;
  555. a = b;
  556. b = tmp;
  557. }
  558. /// "Next Largest Power of 2
  559. /// Given a binary integer value x, the next largest power of 2 can be computed by a SWAR algorithm
  560. /// that recursively "folds" the upper bits into the lower bits. This process yields a bit vector with
  561. /// the same most significant 1 as x, but all 1's below it. Adding 1 to that value yields the next
  562. /// largest power of 2. For a 32-bit value:"
  563. inline uint32 b2NextPowerOfTwo(uint32 x)
  564. {
  565. x |= (x >> 1);
  566. x |= (x >> 2);
  567. x |= (x >> 4);
  568. x |= (x >> 8);
  569. x |= (x >> 16);
  570. return x + 1;
  571. }
  572. inline bool b2IsPowerOfTwo(uint32 x)
  573. {
  574. bool result = x > 0 && (x & (x - 1)) == 0;
  575. return result;
  576. }
  577. inline void b2Sweep::GetTransform(b2Transform* xf, float32 beta) const
  578. {
  579. xf->p = (1.0f - beta) * c0 + beta * c;
  580. float32 angle = (1.0f - beta) * a0 + beta * a;
  581. xf->q.Set(angle);
  582. // Shift to origin
  583. xf->p -= b2Mul(xf->q, localCenter);
  584. }
  585. inline void b2Sweep::Advance(float32 alpha)
  586. {
  587. b2Assert(alpha0 < 1.0f);
  588. float32 beta = (alpha - alpha0) / (1.0f - alpha0);
  589. c0 = (1.0f - beta) * c0 + beta * c;
  590. a0 = (1.0f - beta) * a0 + beta * a;
  591. alpha0 = alpha;
  592. }
  593. /// Normalize an angle in radians to be between -pi and pi
  594. inline void b2Sweep::Normalize()
  595. {
  596. float32 twoPi = 2.0f * b2_pi;
  597. float32 d = twoPi * floorf(a0 / twoPi);
  598. a0 -= d;
  599. a -= d;
  600. }
  601. #endif