26 inline bool b2IsValid(float32 x)
32 return (v.i & 0x7f800000) != 0x7f800000;
36 inline float32 b2InvSqrt(float32 x)
45 float32 xhalf = 0.5f * x;
46 convert.i = 0x5f3759df - (convert.i >> 1);
48 x = x * (1.5f - xhalf * x * x);
52 #define b2Sqrt(x) sqrtf(x)
53 #define b2Atan2(y, x) atan2f(y, x)
62 b2Vec2(float32 x, float32 y) : x(x), y(y) {}
68 void Set(float32 x_, float32 y_) { x = x_; y = y_; }
106 return b2Sqrt(x * x + y * y);
113 return x * x + y * y;
119 float32 length =
Length();
120 if (length < b2_epsilon)
124 float32 invLength = 1.0f / length;
134 return b2IsValid(x) && b2IsValid(y);
149 return b2Vec2(v.x + f, v.y + f);
155 return b2Vec2(v.x - f, v.y - f);
161 return b2Vec2(v.x * f, v.y * f);
167 return b2Vec2(v.x / f, v.y / f);
177 b2Vec3(float32 x, float32 y, float32 z) : x(x), y(y), z(z) {}
180 void SetZero() { x = 0.0f; y = 0.0f; z = 0.0f; }
183 void Set(float32 x_, float32 y_, float32 z_) { x = x_; y = y_; z = z_; }
191 x += v.x; y += v.y; z += v.z;
197 x -= v.x; y -= v.y; z -= v.z;
203 x *= s; y *= s; z *= s;
209 return b2Sqrt(x * x + y * y + z * z);
215 float32 length =
Length();
216 if (length < b2_epsilon)
220 float32 invLength = 1.0f / length;
238 b2Vec4(float32 x, float32 y, float32 z, float32 w) : x(x), y(y), z(z), w(w) {}
257 b2Mat22(float32 a11, float32 a12, float32 a21, float32 a22)
259 ex.x = a11; ex.y = a21;
260 ey.x = a12; ey.y = a22;
273 ex.x = 1.0f; ey.x = 0.0f;
274 ex.y = 0.0f; ey.y = 1.0f;
280 ex.x = 0.0f; ey.x = 0.0f;
281 ex.y = 0.0f; ey.y = 0.0f;
286 float32 a = ex.x, b = ey.x, c = ex.y, d = ey.y;
288 float32 det = a * d - b * c;
293 B.ex.x = det * d; B.ey.x = -det * b;
294 B.ex.y = -det * c; B.ey.y = det * a;
302 float32 a11 = ex.x, a12 = ey.x, a21 = ex.y, a22 = ey.y;
303 float32 det = a11 * a22 - a12 * a21;
309 x.x = det * (a22 * b.x - a12 * b.y);
310 x.y = det * (a11 * b.y - a21 * b.x);
390 return b2Atan2(
s, c);
433 #if LIQUIDFUN_EXTERNAL_LANGUAGE_API
434 float32 GetPositionX()
const {
return p.x; }
438 float32 GetPositionY()
const {
return p.y; }
441 float32 GetRotationSin()
const {
return q.
s; }
444 float32 GetRotationCos()
const {
return q.c; }
445 #endif // LIQUIDFUN_EXTERNAL_LANGUAGE_API
478 extern const b2Vec2 b2Vec2_zero;
483 return a.x * b.x + a.y * b.y;
487 inline float32 b2Cross(
const b2Vec2& a,
const b2Vec2& b)
489 return a.x * b.y - a.y * b.x;
496 return b2Vec2(s * a.y, -s * a.x);
503 return b2Vec2(-s * a.y, s * a.x);
510 return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
517 return b2Vec2(b2Dot(v, A.ex), b2Dot(v, A.ey));
523 return b2Vec2(a.x + b.x, a.y + b.y);
529 return b2Vec2(a.x - b.x, a.y - b.y);
534 return b2Vec2(s * a.x, s * a.y);
537 inline bool operator == (
const b2Vec2& a,
const b2Vec2& b)
539 return a.x == b.x && a.y == b.y;
542 inline bool operator != (
const b2Vec2& a,
const b2Vec2& b)
544 return !operator==(a, b);
547 inline float32 b2Distance(
const b2Vec2& a,
const b2Vec2& b)
553 inline float32 b2DistanceSquared(
const b2Vec2& a,
const b2Vec2& b)
561 return b2Vec3(s * a.x, s * a.y, s * a.z);
567 return b2Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
573 return b2Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
579 return a.x * b.x + a.y * b.y + a.z * b.z;
585 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);
590 return b2Mat22(A.ex + B.ex, A.ey + B.ey);
596 return b2Mat22(b2Mul(A, B.ex), b2Mul(A, B.ey));
602 b2Vec2 c1(b2Dot(A.ex, B.ex), b2Dot(A.ey, B.ex));
603 b2Vec2 c2(b2Dot(A.ex, B.ey), b2Dot(A.ey, B.ey));
610 return v.x * A.ex + v.y * A.ey + v.z * A.ez;
616 return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
627 qr.
s = q.
s * r.c + q.c * r.
s;
628 qr.c = q.c * r.c - q.
s * r.
s;
640 qr.
s = q.c * r.
s - q.
s * r.c;
641 qr.c = q.c * r.c + q.
s * r.
s;
648 return b2Vec2(q.c * v.x - q.
s * v.y, q.
s * v.x + q.c * v.y);
654 return b2Vec2(q.c * v.x + q.
s * v.y, -q.
s * v.x + q.c * v.y);
659 float32 x = (T.q.c * v.x - T.q.
s * v.y) + T.p.x;
660 float32 y = (T.q.
s * v.x + T.q.c * v.y) + T.p.y;
667 float32 px = v.x - T.p.x;
668 float32 py = v.y - T.p.y;
669 float32 x = (T.q.c * px + T.q.
s * py);
670 float32 y = (-T.q.
s * px + T.q.c * py);
680 C.q = b2Mul(A.q, B.q);
681 C.p = b2Mul(A.q, B.p) + A.p;
690 C.q = b2MulT(A.q, B.q);
691 C.p = b2MulT(A.q, B.p - A.p);
695 template <
typename T>
698 return a > T(0) ? a : -a;
703 return b2Vec2(b2Abs(a.x), b2Abs(a.y));
708 return b2Mat22(b2Abs(A.ex), b2Abs(A.ey));
711 template <
typename T>
712 inline T b2Min(T a, T b)
714 return a < b ? a : b;
719 return b2Vec2(b2Min(a.x, b.x), b2Min(a.y, b.y));
722 template <
typename T>
723 inline T b2Max(T a, T b)
725 return a > b ? a : b;
730 return b2Vec2(b2Max(a.x, b.x), b2Max(a.y, b.y));
733 template <
typename T>
734 inline T b2Clamp(T a, T low, T high)
736 return b2Max(low, b2Min(a, high));
741 return b2Max(low, b2Min(a, high));
744 template<
typename T>
inline void b2Swap(T& a, T& b)
756 inline uint32 b2NextPowerOfTwo(uint32 x)
766 inline bool b2IsPowerOfTwo(uint32 x)
768 bool result = x > 0 && (x & (x - 1)) == 0;
774 xf->p = (1.0f - beta) * c0 + beta * c;
775 float32 angle = (1.0f - beta) * a0 + beta * a;
786 c0 += beta * (c - c0);
787 a0 += beta * (a - a0);
794 float32 twoPi = 2.0f * b2_pi;
795 float32 d = twoPi * floorf(a0 / twoPi);
b2Vec2 Solve22(const b2Vec2 &b) const
Definition: b2Math.cpp:41
b2Vec3(float32 x, float32 y, float32 z)
Construct using coordinates.
Definition: b2Math.h:177
void GetTransform(b2Transform *xfb, float32 beta) const
Definition: b2Math.h:772
b2Vec4()
Default constructor does nothing (for performance).
Definition: b2Math.h:235
A 3D column vector with 3 elements.
Definition: b2Math.h:171
float32 GetAngle() const
Get the angle in radians.
Definition: b2Math.h:388
bool IsValid() const
Does this vector contain finite coordinates?
Definition: b2Math.h:132
b2Mat22(const b2Vec2 &c1, const b2Vec2 &c2)
Construct this matrix using columns.
Definition: b2Math.h:250
float32 Length() const
Get the length of this vector (the norm).
Definition: b2Math.h:207
b2Vec3()
Default constructor does nothing (for performance).
Definition: b2Math.h:174
A 3-by-3 matrix. Stored in column-major order.
Definition: b2Math.h:318
b2Vec2()
Default constructor does nothing (for performance).
Definition: b2Math.h:59
void Set(float32 x_, float32 y_, float32 z_)
Set this vector to some specified coordinates.
Definition: b2Math.h:183
float32 Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2Math.h:117
b2Vec2 GetYAxis() const
Get the u-axis.
Definition: b2Math.h:400
b2Mat33()
The default constructor does nothing (for performance).
Definition: b2Math.h:321
b2Vec2 operator-() const
Negate this vector.
Definition: b2Math.h:71
A 4D column vector with 4 elements.
Definition: b2Math.h:232
b2Vec2(float32 x, float32 y)
Construct using coordinates.
Definition: b2Math.h:62
b2Mat22()
The default constructor does nothing (for performance).
Definition: b2Math.h:247
float32 s
Sine and cosine.
Definition: b2Math.h:406
float32 a
world angles
Definition: b2Math.h:470
b2Mat33(const b2Vec3 &c1, const b2Vec3 &c2, const b2Vec3 &c3)
Construct this matrix using columns.
Definition: b2Math.h:324
b2Vec2 Skew() const
Get the skew vector such that dot(skew_vec, other) == cross(vec, other)
Definition: b2Math.h:138
b2Vec2 GetXAxis() const
Get the x-axis.
Definition: b2Math.h:394
b2Vec3 operator-() const
Negate this vector.
Definition: b2Math.h:186
void Set(float32 x_, float32 y_)
Set this vector to some specified coordinates.
Definition: b2Math.h:68
float32 Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2Math.h:213
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:65
void operator+=(const b2Vec2 &v)
Add a vector to this vector.
Definition: b2Math.h:86
void GetInverse22(b2Mat33 *M) const
Definition: b2Math.cpp:56
void operator*=(float32 s)
Multiply this vector by a scalar.
Definition: b2Math.h:201
void operator-=(const b2Vec2 &v)
Subtract a vector from this vector.
Definition: b2Math.h:92
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:180
void operator-=(const b2Vec3 &v)
Subtract a vector from this vector.
Definition: b2Math.h:195
b2Rot(float32 angle)
Initialize from an angle in radians.
Definition: b2Math.h:365
void operator+=(const b2Vec3 &v)
Add a vector to this vector.
Definition: b2Math.h:189
b2Vec2 Solve(const b2Vec2 &b) const
Definition: b2Math.h:300
b2Vec2 c
center world positions
Definition: b2Math.h:469
float32 Length() const
Get the length of this vector (the norm).
Definition: b2Math.h:104
b2Vec2 localCenter
local center of mass position
Definition: b2Math.h:468
void GetSymInverse33(b2Mat33 *M) const
Returns the zero matrix if singular.
Definition: b2Math.cpp:71
A 2-by-2 matrix. Stored in column-major order.
Definition: b2Math.h:244
void Set(const b2Vec2 &c1, const b2Vec2 &c2)
Initialize this matrix using columns.
Definition: b2Math.h:264
b2Vec4(float32 x, float32 y, float32 z, float32 w)
Construct using coordinates.
Definition: b2Math.h:238
void SetIdentity()
Set to the identity rotation.
Definition: b2Math.h:381
float32 alpha0
Definition: b2Math.h:474
void SetIdentity()
Set this to the identity matrix.
Definition: b2Math.h:271
void Normalize()
Normalize the angles.
Definition: b2Math.h:792
b2Vec3 Solve33(const b2Vec3 &b) const
Definition: b2Math.cpp:25
void SetZero()
Set this matrix to all zeros.
Definition: b2Math.h:278
A 2D column vector.
Definition: b2Math.h:56
void Advance(float32 alpha)
Definition: b2Math.h:782
float32 LengthSquared() const
Definition: b2Math.h:111
void operator*=(float32 a)
Multiply this vector by a scalar.
Definition: b2Math.h:98
void Set(float32 angle)
Set using an angle in radians.
Definition: b2Math.h:373
void SetZero()
Set this matrix to all zeros.
Definition: b2Math.h:332
b2Mat22(float32 a11, float32 a12, float32 a21, float32 a22)
Construct this matrix using scalars.
Definition: b2Math.h:257
Rotation.
Definition: b2Math.h:360
float32 operator()(int32 i) const
Read from and indexed element.
Definition: b2Math.h:74