19#define b2_pi 3.14159265359f
65static const b2Vec2 b2Vec2_zero = { 0.0f, 0.0f };
66static const b2Rot b2Rot_identity = { 1.0f, 0.0f };
67static const b2Transform b2Transform_identity = { { 0.0f, 0.0f }, { 1.0f, 0.0f } };
68static const b2Mat22 b2Mat22_zero = { { 0.0f, 0.0f }, { 0.0f, 0.0f } };
90 return a < 0 ? -a : a;
96 return a < lower ? lower : ( a > upper ? upper : a );
102 return a < b ? a : b;
108 return a > b ? a : b;
114 return a < 0 ? -a : a;
120 return a < lower ? lower : ( a > upper ? upper : a );
126 return a.
x * b.
x + a.y * b.y;
132 return a.
x * b.y - a.y * b.
x;
138 return B2_LITERAL(
b2Vec2 ){ s * v.y, -s * v.
x };
144 return B2_LITERAL(
b2Vec2 ){ -s * v.y, s * v.
x };
150 return B2_LITERAL(
b2Vec2 ){ -v.y, v.
x };
156 return B2_LITERAL(
b2Vec2 ){ v.y, -v.
x };
162 return B2_LITERAL(
b2Vec2 ){ a.
x + b.
x, a.y + b.y };
168 return B2_LITERAL(
b2Vec2 ){ a.
x - b.
x, a.y - b.y };
174 return B2_LITERAL(
b2Vec2 ){ -a.
x, -a.y };
181 return B2_LITERAL(
b2Vec2 ){ ( 1.0f - t ) * a.
x + t * b.
x, ( 1.0f - t ) * a.y + t * b.y };
187 return B2_LITERAL(
b2Vec2 ){ a.
x * b.
x, a.y * b.y };
193 return B2_LITERAL(
b2Vec2 ){ s * v.
x, s * v.y };
199 return B2_LITERAL(
b2Vec2 ){ a.
x + s * b.
x, a.y + s * b.y };
205 return B2_LITERAL(
b2Vec2 ){ a.
x - s * b.
x, a.y - s * b.y };
247 return sqrtf( v.
x * v.
x + v.y * v.y );
253 float dx = b.
x - a.
x;
254 float dy = b.y - a.y;
255 return sqrtf( dx * dx + dy * dy );
261 float length = sqrtf( v.
x * v.
x + v.y * v.y );
262 if ( length < FLT_EPSILON )
267 float invLength = 1.0f / length;
268 b2Vec2 n = { invLength * v.
x, invLength * v.y };
277 if ( *length < FLT_EPSILON )
282 float invLength = 1.0f / *length;
283 b2Vec2 n = { invLength * v.
x, invLength * v.y };
290 float mag = sqrtf( q.s * q.s + q.
c * q.
c );
291 float invMag = mag > 0.0 ? 1.0f / mag : 0.0f;
292 b2Rot qn = { q.
c * invMag, q.s * invMag };
305 b2Rot q2 = { q1.
c - deltaAngle * q1.s, q1.s + deltaAngle * q1.
c };
306 float mag = sqrtf( q2.s * q2.s + q2.
c * q2.
c );
307 float invMag = mag > 0.0 ? 1.0f / mag : 0.0f;
308 b2Rot qn = { q2.
c * invMag, q2.s * invMag };
315 return v.
x * v.
x + v.y * v.y;
321 b2Vec2 c = { b.
x - a.
x, b.y - a.y };
322 return c.
x * c.
x + c.y * c.y;
332 float qq = q.s * q.s + q.
c * q.
c;
333 return 1.0f - 0.0006f < qq && qq < 1.0f + 0.0006f;
340 float omt = 1.0f - t;
342 omt * q1.
c + t * q2.
c,
343 omt * q1.s + t * q2.s,
366 float omega = inv_h * ( q2.s * q1.
c - q2.
c * q1.s );
398 qr.s = q.s * r.
c + q.
c * r.s;
399 qr.
c = q.
c * r.
c - q.s * r.s;
411 qr.s = q.
c * r.s - q.s * r.
c;
412 qr.
c = q.
c * r.
c + q.s * r.s;
421 float s = b.s * a.
c - b.
c * a.s;
422 float c = b.
c * a.
c + b.s * a.s;
429 if ( angle < -
b2_pi )
431 return angle + 2.0f *
b2_pi;
433 else if ( angle >
b2_pi )
435 return angle - 2.0f *
b2_pi;
444 while ( angle >
b2_pi )
446 angle -= 2.0f *
b2_pi;
449 while ( angle < -
b2_pi )
451 angle += 2.0f *
b2_pi;
460 return B2_LITERAL(
b2Vec2 ){ q.
c * v.
x - q.s * v.y, q.s * v.
x + q.
c * v.y };
466 return B2_LITERAL(
b2Vec2 ){ q.
c * v.
x + q.s * v.y, -q.s * v.
x + q.
c * v.y };
472 float x = ( t.q.
c * p.
x - t.q.s * p.y ) + t.p.
x;
473 float y = ( t.q.s * p.
x + t.q.
c * p.y ) + t.p.y;
475 return B2_LITERAL(
b2Vec2 ){ x, y };
481 float vx = p.
x - t.p.
x;
482 float vy = p.y - t.p.y;
483 return B2_LITERAL(
b2Vec2 ){ t.q.
c * vx + t.q.s * vy, -t.q.s * vx + t.q.
c * vy };
510 A.
cx.
x * v.
x + A.cy.
x * v.y,
511 A.
cx.y * v.
x + A.cy.y * v.y,
519 float a = A.
cx.
x, b = A.cy.
x, c = A.
cx.y, d = A.cy.y;
520 float det = a * d - b * c;
527 { det * d, -det * c },
528 { -det * b, det * a },
537 float a11 = A.
cx.
x, a12 = A.cy.
x, a21 = A.
cx.y, a22 = A.cy.y;
538 float det = a11 * a22 - a12 * a21;
543 b2Vec2 x = { det * ( a22 * b.
x - a12 * b.y ), det * ( a11 * b.y - a21 * b.
x ) };
551 s = s && a.lowerBound.
x <= b.lowerBound.
x;
552 s = s && a.lowerBound.y <= b.lowerBound.y;
553 s = s && b.upperBound.
x <= a.upperBound.
x;
554 s = s && b.upperBound.y <= a.upperBound.y;
561 b2Vec2 b = { 0.5f * ( a.lowerBound.
x + a.upperBound.
x ), 0.5f * ( a.lowerBound.y + a.upperBound.y ) };
568 b2Vec2 b = { 0.5f * ( a.upperBound.
x - a.lowerBound.
x ), 0.5f * ( a.upperBound.y - a.lowerBound.y ) };
576 c.lowerBound.
x =
b2MinFloat( a.lowerBound.
x, b.lowerBound.
x );
577 c.lowerBound.y =
b2MinFloat( a.lowerBound.y, b.lowerBound.y );
578 c.upperBound.
x =
b2MaxFloat( a.upperBound.
x, b.upperBound.
x );
579 c.upperBound.y =
b2MaxFloat( a.upperBound.y, b.upperBound.y );
631inline void operator*=(
b2Vec2& a,
float b )
640 return { -a.
x, -a.y };
646 return { a.
x + b.
x, a.y + b.y };
652 return { a.
x - b.
x, a.y - b.y };
658 return { a * b.
x, a * b.y };
664 return { a.
x * b, a.y * b };
670 return a.
x == b.
x && a.y == b.y;
676 return a.
x != b.
x || a.y != b.y;
float c
cosine and sine
Definition math_functions.h:34
float x
coordinates
Definition math_functions.h:26
b2Vec2 cx
columns
Definition math_functions.h:48
b2Vec2 b2Rot_GetYAxis(b2Rot q)
Get the y-axis.
Definition math_functions.h:384
b2Vec2 b2GetLengthAndNormalize(float *length, b2Vec2 v)
Convert a vector into a unit vector if possible, otherwise returns the zero vector.
Definition math_functions.h:274
b2Vec2 b2Rot_GetXAxis(b2Rot q)
Get the x-axis.
Definition math_functions.h:377
b2Rot b2InvMulRot(b2Rot q, b2Rot r)
Transpose multiply two rotations: qT * r.
Definition math_functions.h:404
float b2UnwindLargeAngle(float angle)
Convert any into the range [-pi, pi] (slow)
Definition math_functions.h:442
float b2Rot_GetAngle(b2Rot q)
Get the angle in radians in the range [-pi, pi].
Definition math_functions.h:371
b2Vec2 b2Neg(b2Vec2 a)
Vector negation.
Definition math_functions.h:172
bool b2AABB_IsValid(b2AABB aabb)
Is this a valid bounding box? Not Nan or infinity. Upper bound greater than or equal to lower bound.
float b2Cross(b2Vec2 a, b2Vec2 b)
Vector cross product. In 2D this yields a scalar.
Definition math_functions.h:130
b2Vec2 b2MulAdd(b2Vec2 a, float s, b2Vec2 b)
a + s * b
Definition math_functions.h:197
float b2GetLengthUnitsPerMeter(void)
Get the current length units per meter.
b2Rot b2NormalizeRot(b2Rot q)
Normalize rotation.
Definition math_functions.h:288
bool b2AABB_Contains(b2AABB a, b2AABB b)
Does a fully contain b.
Definition math_functions.h:548
b2Vec2 b2MulMV(b2Mat22 A, b2Vec2 v)
Multiply a 2-by-2 matrix times a 2D vector.
Definition math_functions.h:507
b2Vec2 b2Mul(b2Vec2 a, b2Vec2 b)
Component-wise multiplication.
Definition math_functions.h:185
int b2MinInt(int a, int b)
Definition math_functions.h:100
#define b2_pi
https://en.wikipedia.org/wiki/Pi
Definition math_functions.h:19
int b2ClampInt(int a, int lower, int upper)
Definition math_functions.h:118
bool b2Vec2_IsValid(b2Vec2 v)
Is this a valid vector? Not NaN or infinity.
b2Rot b2MakeRot(float angle)
Make a rotation using an angle in radians.
float b2RelativeAngle(b2Rot b, b2Rot a)
relative angle between b and a (rot_b * inv(rot_a))
Definition math_functions.h:417
b2Vec2 b2Add(b2Vec2 a, b2Vec2 b)
Vector addition.
Definition math_functions.h:160
int b2AbsInt(int a)
Definition math_functions.h:112
b2Vec2 b2CrossSV(float s, b2Vec2 v)
Perform the cross product on a scalar and a vector. In 2D this produces a vector.
Definition math_functions.h:142
float b2UnwindAngle(float angle)
Convert an angle in the range [-2*pi, 2*pi] into the range [-pi, pi].
Definition math_functions.h:427
b2Vec2 b2RightPerp(b2Vec2 v)
Get a right pointing perpendicular vector. Equivalent to b2CrossVS(v, 1.0f)
Definition math_functions.h:154
b2Rot b2IntegrateRotation(b2Rot q1, float deltaAngle)
Integration rotation from angular velocity.
Definition math_functions.h:299
b2AABB b2AABB_Union(b2AABB a, b2AABB b)
Union of two AABBs.
Definition math_functions.h:573
b2Vec2 b2TransformPoint(b2Transform t, const b2Vec2 p)
Transform a point (e.g. local space to world space)
Definition math_functions.h:470
b2Vec2 b2InvTransformPoint(b2Transform t, const b2Vec2 p)
Inverse transform a point (e.g. world space to local space)
Definition math_functions.h:479
float b2Atan2(float y, float x)
Compute an approximate arctangent in the range [-pi, pi] This is hand coded for cross platform determ...
void b2SetLengthUnitsPerMeter(float lengthUnits)
Box2D bases all length units on meters, but you may need different units for your game.
b2Vec2 b2InvRotateVector(b2Rot q, b2Vec2 v)
Inverse rotate a vector.
Definition math_functions.h:464
b2Vec2 b2AABB_Center(b2AABB a)
Get the center of the AABB.
Definition math_functions.h:559
bool b2IsNormalized(b2Rot q)
Is this rotation normalized?
Definition math_functions.h:329
float b2ComputeAngularVelocity(b2Rot q1, b2Rot q2, float inv_h)
Compute the angular velocity necessary to rotate between two rotations over a give time.
Definition math_functions.h:354
b2Vec2 b2MulSub(b2Vec2 a, float s, b2Vec2 b)
a - s * b
Definition math_functions.h:203
float b2MaxFloat(float a, float b)
Definition math_functions.h:82
b2Rot b2MulRot(b2Rot q, b2Rot r)
Multiply two rotations: q * r.
Definition math_functions.h:391
b2Vec2 b2Sub(b2Vec2 a, b2Vec2 b)
Vector subtraction.
Definition math_functions.h:166
b2Vec2 b2RotateVector(b2Rot q, b2Vec2 v)
Rotate a vector.
Definition math_functions.h:458
bool b2Rot_IsValid(b2Rot q)
Is this a valid rotation? Not NaN or infinity. Is normalized.
b2Transform b2MulTransforms(b2Transform A, b2Transform B)
v2 = A.q.Rot(B.q.Rot(v1) + B.p) + A.p = (A.q * B.q).Rot(v1) + A.q.Rot(B.p) + A.p
Definition math_functions.h:488
b2Vec2 b2Max(b2Vec2 a, b2Vec2 b)
Component-wise maximum vector.
Definition math_functions.h:227
b2Vec2 b2Min(b2Vec2 a, b2Vec2 b)
Component-wise minimum vector.
Definition math_functions.h:218
b2Rot b2NLerp(b2Rot q1, b2Rot q2, float t)
Normalized linear interpolation https://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-pr...
Definition math_functions.h:338
float b2AbsFloat(float a)
Definition math_functions.h:88
float b2LengthSquared(b2Vec2 v)
Get the length squared of this vector.
Definition math_functions.h:313
float b2DistanceSquared(b2Vec2 a, b2Vec2 b)
Get the distance squared between points.
Definition math_functions.h:319
float b2ClampFloat(float a, float lower, float upper)
Definition math_functions.h:94
b2Vec2 b2MulSV(float s, b2Vec2 v)
Multiply a scalar and vector.
Definition math_functions.h:191
b2Vec2 b2Abs(b2Vec2 a)
Component-wise absolute vector.
Definition math_functions.h:209
b2Mat22 b2GetInverse22(b2Mat22 A)
Get the inverse of a 2-by-2 matrix.
Definition math_functions.h:517
int b2MaxInt(int a, int b)
Definition math_functions.h:106
float b2MinFloat(float a, float b)
Definition math_functions.h:76
float b2Length(b2Vec2 v)
Get the length of this vector (the norm)
Definition math_functions.h:245
float b2Dot(b2Vec2 a, b2Vec2 b)
Vector dot product.
Definition math_functions.h:124
float b2Distance(b2Vec2 a, b2Vec2 b)
Get the distance between two points.
Definition math_functions.h:251
b2Vec2 b2AABB_Extents(b2AABB a)
Get the extents of the AABB (half-widths).
Definition math_functions.h:566
b2Vec2 b2Lerp(b2Vec2 a, b2Vec2 b, float t)
Vector linear interpolation https://fgiesen.wordpress.com/2012/08/15/linear-interpolation-past-presen...
Definition math_functions.h:179
b2Vec2 b2Normalize(b2Vec2 v)
Convert a vector into a unit vector if possible, otherwise returns the zero vector.
Definition math_functions.h:259
b2Vec2 b2CrossVS(b2Vec2 v, float s)
Perform the cross product on a vector and a scalar. In 2D this produces a vector.
Definition math_functions.h:136
b2Vec2 b2Solve22(b2Mat22 A, b2Vec2 b)
Solve A * x = b, where b is a column vector.
Definition math_functions.h:535
b2Vec2 b2Clamp(b2Vec2 v, b2Vec2 a, b2Vec2 b)
Component-wise clamp vector v into the range [a, b].
Definition math_functions.h:236
b2Vec2 b2LeftPerp(b2Vec2 v)
Get a left pointing perpendicular vector. Equivalent to b2CrossSV(1.0f, v)
Definition math_functions.h:148
b2Transform b2InvMulTransforms(b2Transform A, b2Transform B)
v2 = A.q' * (B.q * v1 + B.p - A.p) = A.q' * B.q * v1 + A.q' * (B.p - A.p)
Definition math_functions.h:498
bool b2IsValid(float a)
Is this a valid number? Not NaN or infinity.
Axis-aligned bounding box.
Definition math_functions.h:53
A 2-by-2 Matrix.
Definition math_functions.h:46
2D rotation This is similar to using a complex number for rotation
Definition math_functions.h:32
2D vector This can be used to represent a point or free vector
Definition math_functions.h:24