The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

732 lines
17KB

  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 "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. juce::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 xCoord, float32 yCoord) : x(xCoord), y(yCoord) {}
  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 () (juce::int32 i) const
  69. {
  70. return (&x)[i];
  71. }
  72. /// Write to an indexed element.
  73. float32& operator () (juce::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 xCoord, float32 yCoord, float32 zCoord) : x(xCoord), y(yCoord), z(zCoord) {}
  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. /// Get the inverse of this matrix as a 2-by-2.
  250. /// Returns the zero matrix if singular.
  251. void GetInverse22(b2Mat33* M) const;
  252. /// Get the symmetric inverse of this matrix as a 3-by-3.
  253. /// Returns the zero matrix if singular.
  254. void GetSymInverse33(b2Mat33* M) const;
  255. b2Vec3 ex, ey, ez;
  256. };
  257. /// Rotation
  258. struct b2Rot
  259. {
  260. b2Rot() {}
  261. /// Initialize from an angle in radians
  262. explicit b2Rot(float32 angle)
  263. {
  264. /// TODO_ERIN optimize
  265. s = sinf(angle);
  266. c = cosf(angle);
  267. }
  268. /// Set using an angle in radians.
  269. void Set(float32 angle)
  270. {
  271. /// TODO_ERIN optimize
  272. s = sinf(angle);
  273. c = cosf(angle);
  274. }
  275. /// Set to the identity rotation
  276. void SetIdentity()
  277. {
  278. s = 0.0f;
  279. c = 1.0f;
  280. }
  281. /// Get the angle in radians
  282. float32 GetAngle() const
  283. {
  284. return b2Atan2(s, c);
  285. }
  286. /// Get the x-axis
  287. b2Vec2 GetXAxis() const
  288. {
  289. return b2Vec2(c, s);
  290. }
  291. /// Get the u-axis
  292. b2Vec2 GetYAxis() const
  293. {
  294. return b2Vec2(-s, c);
  295. }
  296. /// Sine and cosine
  297. float32 s, c;
  298. };
  299. /// A transform contains translation and rotation. It is used to represent
  300. /// the position and orientation of rigid frames.
  301. struct b2Transform
  302. {
  303. /// The default constructor does nothing.
  304. b2Transform() {}
  305. /// Initialize using a position vector and a rotation.
  306. b2Transform(const b2Vec2& position, const b2Rot& rotation) : p(position), q(rotation) {}
  307. /// Set this to the identity transform.
  308. void SetIdentity()
  309. {
  310. p.SetZero();
  311. q.SetIdentity();
  312. }
  313. /// Set this based on the position and angle.
  314. void Set(const b2Vec2& position, float32 angle)
  315. {
  316. p = position;
  317. q.Set(angle);
  318. }
  319. b2Vec2 p;
  320. b2Rot q;
  321. };
  322. /// This describes the motion of a body/shape for TOI computation.
  323. /// Shapes are defined with respect to the body origin, which may
  324. /// no coincide with the center of mass. However, to support dynamics
  325. /// we must interpolate the center of mass position.
  326. struct b2Sweep
  327. {
  328. /// Get the interpolated transform at a specific time.
  329. /// @param beta is a factor in [0,1], where 0 indicates alpha0.
  330. void GetTransform(b2Transform* xfb, float32 beta) const;
  331. /// Advance the sweep forward, yielding a new initial state.
  332. /// @param alpha the new initial time.
  333. void Advance(float32 alpha);
  334. /// Normalize the angles.
  335. void Normalize();
  336. b2Vec2 localCenter; ///< local center of mass position
  337. b2Vec2 c0, c; ///< center world positions
  338. float32 a0, a; ///< world angles
  339. /// Fraction of the current time step in the range [0,1]
  340. /// c0 and a0 are the positions at alpha0.
  341. float32 alpha0;
  342. };
  343. /// Useful constant
  344. extern const b2Vec2 b2Vec2_zero;
  345. /// Perform the dot product on two vectors.
  346. inline float32 b2Dot(const b2Vec2& a, const b2Vec2& b)
  347. {
  348. return a.x * b.x + a.y * b.y;
  349. }
  350. /// Perform the cross product on two vectors. In 2D this produces a scalar.
  351. inline float32 b2Cross(const b2Vec2& a, const b2Vec2& b)
  352. {
  353. return a.x * b.y - a.y * b.x;
  354. }
  355. /// Perform the cross product on a vector and a scalar. In 2D this produces
  356. /// a vector.
  357. inline b2Vec2 b2Cross(const b2Vec2& a, float32 s)
  358. {
  359. return b2Vec2(s * a.y, -s * a.x);
  360. }
  361. /// Perform the cross product on a scalar and a vector. In 2D this produces
  362. /// a vector.
  363. inline b2Vec2 b2Cross(float32 s, const b2Vec2& a)
  364. {
  365. return b2Vec2(-s * a.y, s * a.x);
  366. }
  367. /// Multiply a matrix times a vector. If a rotation matrix is provided,
  368. /// then this transforms the vector from one frame to another.
  369. inline b2Vec2 b2Mul(const b2Mat22& A, const b2Vec2& v)
  370. {
  371. return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
  372. }
  373. /// Multiply a matrix transpose times a vector. If a rotation matrix is provided,
  374. /// then this transforms the vector from one frame to another (inverse transform).
  375. inline b2Vec2 b2MulT(const b2Mat22& A, const b2Vec2& v)
  376. {
  377. return b2Vec2(b2Dot(v, A.ex), b2Dot(v, A.ey));
  378. }
  379. /// Add two vectors component-wise.
  380. inline b2Vec2 operator + (const b2Vec2& a, const b2Vec2& b)
  381. {
  382. return b2Vec2(a.x + b.x, a.y + b.y);
  383. }
  384. /// Subtract two vectors component-wise.
  385. inline b2Vec2 operator - (const b2Vec2& a, const b2Vec2& b)
  386. {
  387. return b2Vec2(a.x - b.x, a.y - b.y);
  388. }
  389. inline b2Vec2 operator * (float32 s, const b2Vec2& a)
  390. {
  391. return b2Vec2(s * a.x, s * a.y);
  392. }
  393. inline bool operator == (const b2Vec2& a, const b2Vec2& b)
  394. {
  395. return a.x == b.x && a.y == b.y;
  396. }
  397. inline float32 b2Distance(const b2Vec2& a, const b2Vec2& b)
  398. {
  399. b2Vec2 c = a - b;
  400. return c.Length();
  401. }
  402. inline float32 b2DistanceSquared(const b2Vec2& a, const b2Vec2& b)
  403. {
  404. b2Vec2 c = a - b;
  405. return b2Dot(c, c);
  406. }
  407. inline b2Vec3 operator * (float32 s, const b2Vec3& a)
  408. {
  409. return b2Vec3(s * a.x, s * a.y, s * a.z);
  410. }
  411. /// Add two vectors component-wise.
  412. inline b2Vec3 operator + (const b2Vec3& a, const b2Vec3& b)
  413. {
  414. return b2Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
  415. }
  416. /// Subtract two vectors component-wise.
  417. inline b2Vec3 operator - (const b2Vec3& a, const b2Vec3& b)
  418. {
  419. return b2Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
  420. }
  421. /// Perform the dot product on two vectors.
  422. inline float32 b2Dot(const b2Vec3& a, const b2Vec3& b)
  423. {
  424. return a.x * b.x + a.y * b.y + a.z * b.z;
  425. }
  426. /// Perform the cross product on two vectors.
  427. inline b2Vec3 b2Cross(const b2Vec3& a, const b2Vec3& b)
  428. {
  429. 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);
  430. }
  431. inline b2Mat22 operator + (const b2Mat22& A, const b2Mat22& B)
  432. {
  433. return b2Mat22(A.ex + B.ex, A.ey + B.ey);
  434. }
  435. // A * B
  436. inline b2Mat22 b2Mul(const b2Mat22& A, const b2Mat22& B)
  437. {
  438. return b2Mat22(b2Mul(A, B.ex), b2Mul(A, B.ey));
  439. }
  440. // A^T * B
  441. inline b2Mat22 b2MulT(const b2Mat22& A, const b2Mat22& B)
  442. {
  443. b2Vec2 c1(b2Dot(A.ex, B.ex), b2Dot(A.ey, B.ex));
  444. b2Vec2 c2(b2Dot(A.ex, B.ey), b2Dot(A.ey, B.ey));
  445. return b2Mat22(c1, c2);
  446. }
  447. /// Multiply a matrix times a vector.
  448. inline b2Vec3 b2Mul(const b2Mat33& A, const b2Vec3& v)
  449. {
  450. return v.x * A.ex + v.y * A.ey + v.z * A.ez;
  451. }
  452. /// Multiply a matrix times a vector.
  453. inline b2Vec2 b2Mul22(const b2Mat33& A, const b2Vec2& v)
  454. {
  455. return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
  456. }
  457. /// Multiply two rotations: q * r
  458. inline b2Rot b2Mul(const b2Rot& q, const b2Rot& r)
  459. {
  460. // [qc -qs] * [rc -rs] = [qc*rc-qs*rs -qc*rs-qs*rc]
  461. // [qs qc] [rs rc] [qs*rc+qc*rs -qs*rs+qc*rc]
  462. // s = qs * rc + qc * rs
  463. // c = qc * rc - qs * rs
  464. b2Rot qr;
  465. qr.s = q.s * r.c + q.c * r.s;
  466. qr.c = q.c * r.c - q.s * r.s;
  467. return qr;
  468. }
  469. /// Transpose multiply two rotations: qT * r
  470. inline b2Rot b2MulT(const b2Rot& q, const b2Rot& r)
  471. {
  472. // [ qc qs] * [rc -rs] = [qc*rc+qs*rs -qc*rs+qs*rc]
  473. // [-qs qc] [rs rc] [-qs*rc+qc*rs qs*rs+qc*rc]
  474. // s = qc * rs - qs * rc
  475. // c = qc * rc + qs * rs
  476. b2Rot qr;
  477. qr.s = q.c * r.s - q.s * r.c;
  478. qr.c = q.c * r.c + q.s * r.s;
  479. return qr;
  480. }
  481. /// Rotate a vector
  482. inline b2Vec2 b2Mul(const b2Rot& q, const b2Vec2& v)
  483. {
  484. return b2Vec2(q.c * v.x - q.s * v.y, q.s * v.x + q.c * v.y);
  485. }
  486. /// Inverse rotate a vector
  487. inline b2Vec2 b2MulT(const b2Rot& q, const b2Vec2& v)
  488. {
  489. return b2Vec2(q.c * v.x + q.s * v.y, -q.s * v.x + q.c * v.y);
  490. }
  491. inline b2Vec2 b2Mul(const b2Transform& T, const b2Vec2& v)
  492. {
  493. float32 x = (T.q.c * v.x - T.q.s * v.y) + T.p.x;
  494. float32 y = (T.q.s * v.x + T.q.c * v.y) + T.p.y;
  495. return b2Vec2(x, y);
  496. }
  497. inline b2Vec2 b2MulT(const b2Transform& T, const b2Vec2& v)
  498. {
  499. float32 px = v.x - T.p.x;
  500. float32 py = v.y - T.p.y;
  501. float32 x = (T.q.c * px + T.q.s * py);
  502. float32 y = (-T.q.s * px + T.q.c * py);
  503. return b2Vec2(x, y);
  504. }
  505. // v2 = A.q.Rot(B.q.Rot(v1) + B.p) + A.p
  506. // = (A.q * B.q).Rot(v1) + A.q.Rot(B.p) + A.p
  507. inline b2Transform b2Mul(const b2Transform& A, const b2Transform& B)
  508. {
  509. b2Transform C;
  510. C.q = b2Mul(A.q, B.q);
  511. C.p = b2Mul(A.q, B.p) + A.p;
  512. return C;
  513. }
  514. // v2 = A.q' * (B.q * v1 + B.p - A.p)
  515. // = A.q' * B.q * v1 + A.q' * (B.p - A.p)
  516. inline b2Transform b2MulT(const b2Transform& A, const b2Transform& B)
  517. {
  518. b2Transform C;
  519. C.q = b2MulT(A.q, B.q);
  520. C.p = b2MulT(A.q, B.p - A.p);
  521. return C;
  522. }
  523. template <typename T>
  524. inline T b2Abs(T a)
  525. {
  526. return a > T(0) ? a : -a;
  527. }
  528. inline b2Vec2 b2Abs(const b2Vec2& a)
  529. {
  530. return b2Vec2(b2Abs(a.x), b2Abs(a.y));
  531. }
  532. inline b2Mat22 b2Abs(const b2Mat22& A)
  533. {
  534. return b2Mat22(b2Abs(A.ex), b2Abs(A.ey));
  535. }
  536. template <typename T>
  537. inline T b2Min(T a, T b)
  538. {
  539. return a < b ? a : b;
  540. }
  541. inline b2Vec2 b2Min(const b2Vec2& a, const b2Vec2& b)
  542. {
  543. return b2Vec2(b2Min(a.x, b.x), b2Min(a.y, b.y));
  544. }
  545. template <typename T>
  546. inline T b2Max(T a, T b)
  547. {
  548. return a > b ? a : b;
  549. }
  550. inline b2Vec2 b2Max(const b2Vec2& a, const b2Vec2& b)
  551. {
  552. return b2Vec2(b2Max(a.x, b.x), b2Max(a.y, b.y));
  553. }
  554. template <typename T>
  555. inline T b2Clamp(T a, T low, T high)
  556. {
  557. return b2Max(low, b2Min(a, high));
  558. }
  559. inline b2Vec2 b2Clamp(const b2Vec2& a, const b2Vec2& low, const b2Vec2& high)
  560. {
  561. return b2Max(low, b2Min(a, high));
  562. }
  563. template<typename T> inline void b2Swap(T& a, T& b)
  564. {
  565. T tmp = a;
  566. a = b;
  567. b = tmp;
  568. }
  569. /// "Next Largest Power of 2
  570. /// Given a binary integer value x, the next largest power of 2 can be computed by a SWAR algorithm
  571. /// that recursively "folds" the upper bits into the lower bits. This process yields a bit vector with
  572. /// the same most significant 1 as x, but all 1's below it. Adding 1 to that value yields the next
  573. /// largest power of 2. For a 32-bit value:"
  574. inline juce::uint32 b2NextPowerOfTwo(juce::uint32 x)
  575. {
  576. x |= (x >> 1);
  577. x |= (x >> 2);
  578. x |= (x >> 4);
  579. x |= (x >> 8);
  580. x |= (x >> 16);
  581. return x + 1;
  582. }
  583. inline bool b2IsPowerOfTwo(juce::uint32 x)
  584. {
  585. bool result = x > 0 && (x & (x - 1)) == 0;
  586. return result;
  587. }
  588. inline void b2Sweep::GetTransform(b2Transform* xf, float32 beta) const
  589. {
  590. xf->p = (1.0f - beta) * c0 + beta * c;
  591. float32 angle = (1.0f - beta) * a0 + beta * a;
  592. xf->q.Set(angle);
  593. // Shift to origin
  594. xf->p -= b2Mul(xf->q, localCenter);
  595. }
  596. inline void b2Sweep::Advance(float32 alpha)
  597. {
  598. b2Assert(alpha0 < 1.0f);
  599. float32 beta = (alpha - alpha0) / (1.0f - alpha0);
  600. c0 = (1.0f - beta) * c0 + beta * c;
  601. a0 = (1.0f - beta) * a0 + beta * a;
  602. alpha0 = alpha;
  603. }
  604. /// Normalize an angle in radians to be between -pi and pi
  605. inline void b2Sweep::Normalize()
  606. {
  607. float32 twoPi = 2.0f * b2_pi;
  608. float32 d = twoPi * floorf(a0 / twoPi);
  609. a0 -= d;
  610. a -= d;
  611. }
  612. #endif