b2Math.h 16 KB

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