b2_math.h 16 KB

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