31 inline bool b2IsValid(float32 x)
39 float32 infinity = std::numeric_limits<float32>::infinity();
40 return -infinity < x && x < infinity;
44 inline float32 b2InvSqrt(float32 x)
53 float32 xhalf = 0.5f * x;
54 convert.i = 0x5f3759df - (convert.i >> 1);
56 x = x * (1.5f - xhalf * x * x);
60 #define b2Sqrt(x) std::sqrt(x)
61 #define b2Atan2(y, x) std::atan2(y, x)
70 b2Vec2(float32 x, float32 y) : x(x), y(y) {}
76 void Set(float32 x_, float32 y_) { x = x_; y = y_; }
114 return b2Sqrt(x * x + y * y);
121 return x * x + y * y;
127 float32 length =
Length();
128 if (length < b2_epsilon)
132 float32 invLength = 1.0f / length;
142 return b2IsValid(x) && b2IsValid(y);
161 b2Vec3(float32 x, float32 y, float32 z) : x(x), y(y), z(z) {}
164 void SetZero() { x = 0.0f; y = 0.0f; z = 0.0f; }
167 void Set(float32 x_, float32 y_, float32 z_) { x = x_; y = y_; z = z_; }
175 x += v.x; y += v.y; z += v.z;
181 x -= v.x; y -= v.y; z -= v.z;
187 x *= s; y *= s; z *= s;
207 b2Mat22(float32 a11, float32 a12, float32 a21, float32 a22)
209 ex.x = a11; ex.y = a21;
210 ey.x = a12; ey.y = a22;
223 ex.x = 1.0f; ey.x = 0.0f;
224 ex.y = 0.0f; ey.y = 1.0f;
230 ex.x = 0.0f; ey.x = 0.0f;
231 ex.y = 0.0f; ey.y = 0.0f;
236 float32 a = ex.x, b = ey.x, c = ex.y, d = ey.y;
238 float32 det = a * d - b * c;
243 B.ex.x = det * d; B.ey.x = -det * b;
244 B.ex.y = -det * c; B.ey.y = det * a;
252 float32 a11 = ex.x, a12 = ey.x, a21 = ex.y, a22 = ey.y;
253 float32 det = a11 * a22 - a12 * a21;
259 x.x = det * (a22 * b.x - a12 * b.y);
260 x.y = det * (a11 * b.y - a21 * b.x);
340 return b2Atan2(
s, c);
414 extern const b2Vec2 b2Vec2_zero;
419 return a.x * b.x + a.y * b.y;
423 inline float32 b2Cross(
const b2Vec2& a,
const b2Vec2& b)
425 return a.x * b.y - a.y * b.x;
432 return b2Vec2(s * a.y, -s * a.x);
439 return b2Vec2(-s * a.y, s * a.x);
446 return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
453 return b2Vec2(b2Dot(v, A.ex), b2Dot(v, A.ey));
459 return b2Vec2(a.x + b.x, a.y + b.y);
465 return b2Vec2(a.x - b.x, a.y - b.y);
470 return b2Vec2(s * a.x, s * a.y);
473 inline bool operator == (
const b2Vec2& a,
const b2Vec2& b)
475 return a.x == b.x && a.y == b.y;
478 inline float32 b2Distance(
const b2Vec2& a,
const b2Vec2& b)
484 inline float32 b2DistanceSquared(
const b2Vec2& a,
const b2Vec2& b)
492 return b2Vec3(s * a.x, s * a.y, s * a.z);
498 return b2Vec3(a.x + b.x, a.y + b.y, a.z + b.z);
504 return b2Vec3(a.x - b.x, a.y - b.y, a.z - b.z);
510 return a.x * b.x + a.y * b.y + a.z * b.z;
516 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);
521 return b2Mat22(A.ex + B.ex, A.ey + B.ey);
527 return b2Mat22(b2Mul(A, B.ex), b2Mul(A, B.ey));
533 b2Vec2 c1(b2Dot(A.ex, B.ex), b2Dot(A.ey, B.ex));
534 b2Vec2 c2(b2Dot(A.ex, B.ey), b2Dot(A.ey, B.ey));
541 return v.x * A.ex + v.y * A.ey + v.z * A.ez;
547 return b2Vec2(A.ex.x * v.x + A.ey.x * v.y, A.ex.y * v.x + A.ey.y * v.y);
558 qr.
s = q.
s * r.c + q.c * r.
s;
559 qr.c = q.c * r.c - q.
s * r.
s;
571 qr.
s = q.c * r.
s - q.
s * r.c;
572 qr.c = q.c * r.c + q.
s * r.
s;
579 return b2Vec2(q.c * v.x - q.
s * v.y, q.
s * v.x + q.c * v.y);
585 return b2Vec2(q.c * v.x + q.
s * v.y, -q.
s * v.x + q.c * v.y);
590 float32 x = (T.q.c * v.x - T.q.
s * v.y) + T.p.x;
591 float32 y = (T.q.
s * v.x + T.q.c * v.y) + T.p.y;
598 float32 px = v.x - T.p.x;
599 float32 py = v.y - T.p.y;
600 float32 x = (T.q.c * px + T.q.
s * py);
601 float32 y = (-T.q.
s * px + T.q.c * py);
611 C.q = b2Mul(A.q, B.q);
612 C.p = b2Mul(A.q, B.p) + A.p;
621 C.q = b2MulT(A.q, B.q);
622 C.p = b2MulT(A.q, B.p - A.p);
626 template <
typename T>
629 return a > T(0) ? a : -a;
634 return b2Vec2(b2Abs(a.x), b2Abs(a.y));
639 return b2Mat22(b2Abs(A.ex), b2Abs(A.ey));
642 template <
typename T>
643 inline T b2Min(T a, T b)
645 return a < b ? a : b;
650 return b2Vec2(b2Min(a.x, b.x), b2Min(a.y, b.y));
653 template <
typename T>
654 inline T b2Max(T a, T b)
656 return a > b ? a : b;
661 return b2Vec2(b2Max(a.x, b.x), b2Max(a.y, b.y));
664 template <
typename T>
665 inline T b2Clamp(T a, T low, T high)
667 return b2Max(low, b2Min(a, high));
672 return b2Max(low, b2Min(a, high));
675 template<
typename T>
inline void b2Swap(T& a, T& b)
687 inline uint32 b2NextPowerOfTwo(uint32 x)
697 inline bool b2IsPowerOfTwo(uint32 x)
699 bool result = x > 0 && (x & (x - 1)) == 0;
705 xf->p = (1.0f - beta) * c0 + beta * c;
706 float32 angle = (1.0f - beta) * a0 + beta * a;
717 c0 += beta * (c - c0);
718 a0 += beta * (a - a0);
725 float32 twoPi = 2.0f * b2_pi;
726 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:161
void GetTransform(b2Transform *xfb, float32 beta) const
Definition: b2Math.h:703
A 2D column vector with 3 elements.
Definition: b2Math.h:155
float32 GetAngle() const
Get the angle in radians.
Definition: b2Math.h:338
bool IsValid() const
Does this vector contain finite coordinates?
Definition: b2Math.h:140
b2Mat22(const b2Vec2 &c1, const b2Vec2 &c2)
Construct this matrix using columns.
Definition: b2Math.h:200
b2Vec3()
Default constructor does nothing (for performance).
Definition: b2Math.h:158
A 3-by-3 matrix. Stored in column-major order.
Definition: b2Math.h:268
b2Vec2()
Default constructor does nothing (for performance).
Definition: b2Math.h:67
void Set(float32 x_, float32 y_, float32 z_)
Set this vector to some specified coordinates.
Definition: b2Math.h:167
float32 Normalize()
Convert this vector into a unit vector. Returns the length.
Definition: b2Math.h:125
b2Vec2 GetYAxis() const
Get the u-axis.
Definition: b2Math.h:350
b2Mat33()
The default constructor does nothing (for performance).
Definition: b2Math.h:271
b2Vec2 operator-() const
Negate this vector.
Definition: b2Math.h:79
b2Vec2(float32 x, float32 y)
Construct using coordinates.
Definition: b2Math.h:70
b2Mat22()
The default constructor does nothing (for performance).
Definition: b2Math.h:197
float32 s
Sine and cosine.
Definition: b2Math.h:356
float32 a
world angles
Definition: b2Math.h:406
b2Mat33(const b2Vec3 &c1, const b2Vec3 &c2, const b2Vec3 &c3)
Construct this matrix using columns.
Definition: b2Math.h:274
b2Vec2 Skew() const
Get the skew vector such that dot(skew_vec, other) == cross(vec, other)
Definition: b2Math.h:146
b2Vec2 GetXAxis() const
Get the x-axis.
Definition: b2Math.h:344
b2Vec3 operator-() const
Negate this vector.
Definition: b2Math.h:170
void Set(float32 x_, float32 y_)
Set this vector to some specified coordinates.
Definition: b2Math.h:76
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:73
void operator+=(const b2Vec2 &v)
Add a vector to this vector.
Definition: b2Math.h:94
void GetInverse22(b2Mat33 *M) const
Definition: b2Math.cpp:56
void operator*=(float32 s)
Multiply this vector by a scalar.
Definition: b2Math.h:185
void operator-=(const b2Vec2 &v)
Subtract a vector from this vector.
Definition: b2Math.h:100
void SetZero()
Set this vector to all zeros.
Definition: b2Math.h:164
void operator-=(const b2Vec3 &v)
Subtract a vector from this vector.
Definition: b2Math.h:179
b2Rot(float32 angle)
Initialize from an angle in radians.
Definition: b2Math.h:315
void operator+=(const b2Vec3 &v)
Add a vector to this vector.
Definition: b2Math.h:173
b2Vec2 Solve(const b2Vec2 &b) const
Definition: b2Math.h:250
b2Vec2 c
center world positions
Definition: b2Math.h:405
float32 Length() const
Get the length of this vector (the norm).
Definition: b2Math.h:112
b2Vec2 localCenter
local center of mass position
Definition: b2Math.h:404
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:194
void Set(const b2Vec2 &c1, const b2Vec2 &c2)
Initialize this matrix using columns.
Definition: b2Math.h:214
void SetIdentity()
Set to the identity rotation.
Definition: b2Math.h:331
float32 alpha0
Definition: b2Math.h:410
void SetIdentity()
Set this to the identity matrix.
Definition: b2Math.h:221
void Normalize()
Normalize the angles.
Definition: b2Math.h:723
b2Vec3 Solve33(const b2Vec3 &b) const
Definition: b2Math.cpp:25
void SetZero()
Set this matrix to all zeros.
Definition: b2Math.h:228
A 2D column vector.
Definition: b2Math.h:64
void Advance(float32 alpha)
Definition: b2Math.h:713
float32 LengthSquared() const
Definition: b2Math.h:119
void operator*=(float32 a)
Multiply this vector by a scalar.
Definition: b2Math.h:106
void Set(float32 angle)
Set using an angle in radians.
Definition: b2Math.h:323
void SetZero()
Set this matrix to all zeros.
Definition: b2Math.h:282
b2Mat22(float32 a11, float32 a12, float32 a21, float32 a22)
Construct this matrix using scalars.
Definition: b2Math.h:207
Rotation.
Definition: b2Math.h:310
float32 operator()(int32 i) const
Read from and indexed element.
Definition: b2Math.h:82