b2Math.h 18 KB

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