79#define B2_PI 3.14159265359f
81static const b2Vec2 b2Vec2_zero = { 0.0f, 0.0f };
82static const b2Rot b2Rot_identity = { 1.0f, 0.0f };
83static const b2Transform b2Transform_identity = { { 0.0f, 0.0f }, { 1.0f, 0.0f } };
84static const b2Mat22 b2Mat22_zero = { { 0.0f, 0.0f }, { 0.0f, 0.0f } };
101 return a < 0 ? -a : a;
107 return a < lower ? lower : ( a > upper ? upper : a );
113 return a < b ? a : b;
119 return a > b ? a : b;
125 return a < 0 ? -a : a;
131 return a < lower ? lower : ( a > upper ? upper : a );
147 return a.
x * b.
x + a.y * b.y;
153 return a.
x * b.y - a.y * b.
x;
159 return B2_LITERAL(
b2Vec2 ){ s * v.y, -s * v.
x };
165 return B2_LITERAL(
b2Vec2 ){ -s * v.y, s * v.
x };
171 return B2_LITERAL(
b2Vec2 ){ -v.y, v.
x };
177 return B2_LITERAL(
b2Vec2 ){ v.y, -v.
x };
183 return B2_LITERAL(
b2Vec2 ){ a.
x + b.
x, a.y + b.y };
189 return B2_LITERAL(
b2Vec2 ){ a.
x - b.
x, a.y - b.y };
195 return B2_LITERAL(
b2Vec2 ){ -a.
x, -a.y };
202 return B2_LITERAL(
b2Vec2 ){ ( 1.0f - t ) * a.
x + t * b.
x, ( 1.0f - t ) * a.y + t * b.y };
208 return B2_LITERAL(
b2Vec2 ){ a.
x * b.
x, a.y * b.y };
214 return B2_LITERAL(
b2Vec2 ){ s * v.
x, s * v.y };
220 return B2_LITERAL(
b2Vec2 ){ a.
x + s * b.
x, a.y + s * b.y };
226 return B2_LITERAL(
b2Vec2 ){ a.
x - s * b.
x, a.y - s * b.y };
268 return sqrtf( v.
x * v.
x + v.y * v.y );
274 float dx = b.
x - a.
x;
275 float dy = b.y - a.y;
276 return sqrtf( dx * dx + dy * dy );
283 float length = sqrtf( v.
x * v.
x + v.y * v.y );
284 if ( length < FLT_EPSILON )
286 return B2_LITERAL(
b2Vec2 ){ 0.0f, 0.0f };
289 float invLength = 1.0f / length;
290 b2Vec2 n = { invLength * v.
x, invLength * v.y };
297 float aa =
b2Dot( a, a );
298 return b2AbsFloat( 1.0f - aa ) < 10.0f * FLT_EPSILON;
305 *length = sqrtf( v.
x * v.
x + v.y * v.y );
306 if ( *length < FLT_EPSILON )
308 return B2_LITERAL(
b2Vec2 ){ 0.0f, 0.0f };
311 float invLength = 1.0f / *length;
312 b2Vec2 n = { invLength * v.
x, invLength * v.y };
319 float mag = sqrtf( q.s * q.s + q.
c * q.
c );
320 float invMag = mag > 0.0 ? 1.0f / mag : 0.0f;
321 b2Rot qn = { q.
c * invMag, q.s * invMag };
334 b2Rot q2 = { q1.
c - deltaAngle * q1.s, q1.s + deltaAngle * q1.
c };
335 float mag = sqrtf( q2.s * q2.s + q2.
c * q2.
c );
336 float invMag = mag > 0.0 ? 1.0f / mag : 0.0f;
337 b2Rot qn = { q2.
c * invMag, q2.s * invMag };
344 return v.
x * v.
x + v.y * v.y;
350 b2Vec2 c = { b.
x - a.
x, b.y - a.y };
351 return c.
x * c.
x + c.y * c.y;
368 float qq = q.s * q.s + q.
c * q.
c;
369 return 1.0f - 0.0006f < qq && qq < 1.0f + 0.0006f;
377 float omt = 1.0f - t;
379 omt * q1.
c + t * q2.
c,
380 omt * q1.s + t * q2.s,
383 float mag = sqrtf( q.s * q.s + q.
c * q.
c );
384 float invMag = mag > 0.0 ? 1.0f / mag : 0.0f;
385 b2Rot qn = { q.
c * invMag, q.s * invMag };
405 float omega = inv_h * ( q2.s * q1.
c - q2.
c * q1.s );
437 qr.s = q.s * r.
c + q.
c * r.s;
438 qr.
c = q.
c * r.
c - q.s * r.s;
450 qr.s = q.
c * r.s - q.s * r.
c;
451 qr.
c = q.
c * r.
c + q.s * r.s;
460 float s = b.s * a.
c - b.
c * a.s;
461 float c = b.
c * a.
c + b.s * a.s;
468 if ( radians < -
B2_PI )
470 return radians + 2.0f *
B2_PI;
472 else if ( radians >
B2_PI )
474 return radians - 2.0f *
B2_PI;
483 while ( radians >
B2_PI )
485 radians -= 2.0f *
B2_PI;
488 while ( radians < -
B2_PI )
490 radians += 2.0f *
B2_PI;
499 return B2_LITERAL(
b2Vec2 ){ q.
c * v.
x - q.s * v.y, q.s * v.
x + q.
c * v.y };
505 return B2_LITERAL(
b2Vec2 ){ q.
c * v.
x + q.s * v.y, -q.s * v.
x + q.
c * v.y };
511 float x = ( t.q.
c * p.
x - t.q.s * p.y ) + t.p.
x;
512 float y = ( t.q.s * p.
x + t.q.
c * p.y ) + t.p.y;
514 return B2_LITERAL(
b2Vec2 ){ x, y };
520 float vx = p.
x - t.p.
x;
521 float vy = p.y - t.p.y;
522 return B2_LITERAL(
b2Vec2 ){ t.q.
c * vx + t.q.s * vy, -t.q.s * vx + t.q.
c * vy };
553 A.
cx.
x * v.
x + A.cy.
x * v.y,
554 A.
cx.y * v.
x + A.cy.y * v.y,
562 float a = A.
cx.
x, b = A.cy.
x, c = A.
cx.y, d = A.cy.y;
563 float det = a * d - b * c;
570 { det * d, -det * c },
571 { -det * b, det * a },
580 float a11 = A.
cx.
x, a12 = A.cy.
x, a21 = A.
cx.y, a22 = A.cy.y;
581 float det = a11 * a22 - a12 * a21;
586 b2Vec2 x = { det * ( a22 * b.
x - a12 * b.y ), det * ( a11 * b.y - a21 * b.
x ) };
594 s = s && a.lowerBound.
x <= b.lowerBound.
x;
595 s = s && a.lowerBound.y <= b.lowerBound.y;
596 s = s && b.upperBound.
x <= a.upperBound.
x;
597 s = s && b.upperBound.y <= a.upperBound.y;
604 b2Vec2 b = { 0.5f * ( a.lowerBound.
x + a.upperBound.
x ), 0.5f * ( a.lowerBound.y + a.upperBound.y ) };
611 b2Vec2 b = { 0.5f * ( a.upperBound.
x - a.lowerBound.
x ), 0.5f * ( a.upperBound.y - a.lowerBound.y ) };
619 c.lowerBound.
x =
b2MinFloat( a.lowerBound.
x, b.lowerBound.
x );
620 c.lowerBound.y =
b2MinFloat( a.lowerBound.y, b.lowerBound.y );
621 c.upperBound.
x =
b2MaxFloat( a.upperBound.
x, b.upperBound.
x );
622 c.upperBound.y =
b2MaxFloat( a.upperBound.y, b.upperBound.y );
629 B2_ASSERT( count > 0 );
630 b2AABB a = { points[0], points[0] };
631 for (
int i = 1; i < count; ++i )
633 a.lowerBound =
b2Min( a.lowerBound, points[i] );
634 a.upperBound =
b2Max( a.upperBound, points[i] );
637 b2Vec2 r = { radius, radius };
638 a.lowerBound =
b2Sub( a.lowerBound, r );
639 a.upperBound =
b2Add( a.upperBound, r );
647 return b2Dot( plane.normal, point ) - plane.offset;
711inline void operator*=(
b2Vec2& a,
float b )
720 return { -a.
x, -a.y };
726 return { a.
x + b.
x, a.y + b.y };
732 return { a.
x - b.
x, a.y - b.y };
738 return { a * b.
x, a * b.y };
744 return { a.
x * b, a.y * b };
750 return a.
x == b.
x && a.y == b.y;
756 return a.
x != b.
x || a.y != b.y;
float c
cosine and sine
Definition math_functions.h:40
float x
coordinates
Definition math_functions.h:23
b2Vec2 cx
columns
Definition math_functions.h:54
float cosine
cosine and sine
Definition math_functions.h:31
b2Vec2 b2Rot_GetYAxis(b2Rot q)
Get the y-axis.
Definition math_functions.h:423
b2Vec2 b2GetLengthAndNormalize(float *length, b2Vec2 v)
Convert a vector into a unit vector if possible, otherwise returns the zero vector.
Definition math_functions.h:303
b2Vec2 b2Rot_GetXAxis(b2Rot q)
Get the x-axis.
Definition math_functions.h:416
b2Rot b2InvMulRot(b2Rot q, b2Rot r)
Transpose multiply two rotations: qT * r.
Definition math_functions.h:443
float b2Rot_GetAngle(b2Rot q)
Get the angle in radians in the range [-pi, pi].
Definition math_functions.h:410
b2Vec2 b2Neg(b2Vec2 a)
Vector negation.
Definition math_functions.h:193
float b2Cross(b2Vec2 a, b2Vec2 b)
Vector cross product. In 2D this yields a scalar.
Definition math_functions.h:151
b2Vec2 b2MulAdd(b2Vec2 a, float s, b2Vec2 b)
a + s * b
Definition math_functions.h:218
float b2PlaneSeparation(b2Plane plane, b2Vec2 point)
Signed separation of a point from a plane.
Definition math_functions.h:645
float b2GetLengthUnitsPerMeter(void)
Get the current length units per meter.
b2Rot b2NormalizeRot(b2Rot q)
Normalize rotation.
Definition math_functions.h:317
bool b2AABB_Contains(b2AABB a, b2AABB b)
Does a fully contain b.
Definition math_functions.h:591
b2Vec2 b2MulMV(b2Mat22 A, b2Vec2 v)
Multiply a 2-by-2 matrix times a 2D vector.
Definition math_functions.h:550
b2Vec2 b2Mul(b2Vec2 a, b2Vec2 b)
Component-wise multiplication.
Definition math_functions.h:206
int b2MinInt(int a, int b)
Definition math_functions.h:87
bool b2IsValidPlane(b2Plane a)
Is this a valid plane? Normal is a unit vector. Not Nan or infinity.
float b2UnwindLargeAngle(float radians)
Convert any into the range [-pi, pi] (slow)
Definition math_functions.h:481
int b2ClampInt(int a, int lower, int upper)
Definition math_functions.h:105
float b2RelativeAngle(b2Rot b, b2Rot a)
relative angle between b and a (rot_b * inv(rot_a))
Definition math_functions.h:456
b2Vec2 b2Add(b2Vec2 a, b2Vec2 b)
Vector addition.
Definition math_functions.h:181
int b2AbsInt(int a)
Definition math_functions.h:99
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:163
float b2UnwindAngle(float radians)
Convert an angle in the range [-2*pi, 2*pi] into the range [-pi, pi].
Definition math_functions.h:466
b2Vec2 b2RightPerp(b2Vec2 v)
Get a right pointing perpendicular vector. Equivalent to b2CrossVS(v, 1.0f)
Definition math_functions.h:175
b2Rot b2IntegrateRotation(b2Rot q1, float deltaAngle)
Integrate rotation from angular velocity.
Definition math_functions.h:328
b2AABB b2AABB_Union(b2AABB a, b2AABB b)
Union of two AABBs.
Definition math_functions.h:616
b2Vec2 b2TransformPoint(b2Transform t, const b2Vec2 p)
Transform a point (e.g. local space to world space)
Definition math_functions.h:509
b2Vec2 b2InvTransformPoint(b2Transform t, const b2Vec2 p)
Inverse transform a point (e.g. world space to local space)
Definition math_functions.h:518
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.
bool b2IsValidVec2(b2Vec2 v)
Is this a valid vector? Not NaN or infinity.
b2Vec2 b2InvRotateVector(b2Rot q, b2Vec2 v)
Inverse rotate a vector.
Definition math_functions.h:503
b2Vec2 b2AABB_Center(b2AABB a)
Get the center of the AABB.
Definition math_functions.h:602
bool b2IsValidFloat(float a)
Is this a valid number? Not NaN or infinity.
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:393
b2Vec2 b2MulSub(b2Vec2 a, float s, b2Vec2 b)
a - s * b
Definition math_functions.h:224
bool b2IsNormalizedRot(b2Rot q)
Is this rotation normalized?
Definition math_functions.h:365
bool b2IsNormalized(b2Vec2 a)
Determines if the provided vector is normalized (norm(a) == 1).
Definition math_functions.h:295
float b2MaxFloat(float a, float b)
Definition math_functions.h:117
b2Rot b2MulRot(b2Rot q, b2Rot r)
Multiply two rotations: q * r.
Definition math_functions.h:430
b2Vec2 b2Sub(b2Vec2 a, b2Vec2 b)
Vector subtraction.
Definition math_functions.h:187
b2Vec2 b2RotateVector(b2Rot q, b2Vec2 v)
Rotate a vector.
Definition math_functions.h:497
b2Transform b2MulTransforms(b2Transform A, b2Transform B)
Multiply two transforms.
Definition math_functions.h:530
bool b2IsValidRotation(b2Rot q)
Is this a valid rotation? Not NaN or infinity. Is normalized.
b2Vec2 b2Max(b2Vec2 a, b2Vec2 b)
Component-wise maximum vector.
Definition math_functions.h:248
b2Vec2 b2Min(b2Vec2 a, b2Vec2 b)
Component-wise minimum vector.
Definition math_functions.h:239
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:375
float b2AbsFloat(float a)
Definition math_functions.h:123
float b2LengthSquared(b2Vec2 v)
Get the length squared of this vector.
Definition math_functions.h:342
float b2DistanceSquared(b2Vec2 a, b2Vec2 b)
Get the distance squared between points.
Definition math_functions.h:348
float b2ClampFloat(float a, float lower, float upper)
Definition math_functions.h:129
b2CosSin b2ComputeCosSin(float radians)
Compute the cosine and sine of an angle in radians.
b2Vec2 b2MulSV(float s, b2Vec2 v)
Multiply a scalar and vector.
Definition math_functions.h:212
b2Vec2 b2Abs(b2Vec2 a)
Component-wise absolute vector.
Definition math_functions.h:230
b2Mat22 b2GetInverse22(b2Mat22 A)
Get the inverse of a 2-by-2 matrix.
Definition math_functions.h:560
int b2MaxInt(int a, int b)
Definition math_functions.h:93
float b2MinFloat(float a, float b)
Definition math_functions.h:111
b2AABB b2MakeAABB(const b2Vec2 *points, int count, float radius)
Compute the bounding box of an array of circles.
Definition math_functions.h:627
float b2Length(b2Vec2 v)
Get the length of this vector (the norm)
Definition math_functions.h:266
float b2Dot(b2Vec2 a, b2Vec2 b)
Vector dot product.
Definition math_functions.h:145
b2Rot b2MakeRot(float radians)
Make a rotation using an angle in radians.
Definition math_functions.h:355
float b2Distance(b2Vec2 a, b2Vec2 b)
Get the distance between two points.
Definition math_functions.h:272
b2Vec2 b2AABB_Extents(b2AABB a)
Get the extents of the AABB (half-widths).
Definition math_functions.h:609
bool b2IsValidAABB(b2AABB aabb)
Is this a valid bounding box? Not Nan or infinity. Upper bound greater than or equal to lower bound.
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:200
b2Vec2 b2Normalize(b2Vec2 v)
Convert a vector into a unit vector if possible, otherwise returns the zero vector.
Definition math_functions.h:281
#define B2_PI
https://en.wikipedia.org/wiki/Pi
Definition math_functions.h:79
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:157
b2Vec2 b2Solve22(b2Mat22 A, b2Vec2 b)
Solve A * x = b, where b is a column vector.
Definition math_functions.h:578
b2Vec2 b2Clamp(b2Vec2 v, b2Vec2 a, b2Vec2 b)
Component-wise clamp vector v into the range [a, b].
Definition math_functions.h:257
b2Vec2 b2LeftPerp(b2Vec2 v)
Get a left pointing perpendicular vector. Equivalent to b2CrossSV(1.0f, v)
Definition math_functions.h:169
b2Transform b2InvMulTransforms(b2Transform A, b2Transform B)
Creates a transform that converts a local point in frame B to a local point in frame A.
Definition math_functions.h:541
b2Rot b2ComputeRotationBetweenUnitVectors(b2Vec2 v1, b2Vec2 v2)
Compute the rotation between two unit vectors.
Axis-aligned bounding box.
Definition math_functions.h:59
Cosine and sine pair This uses a custom implementation designed for cross-platform determinism.
Definition math_functions.h:29
A 2-by-2 Matrix.
Definition math_functions.h:52
separation = dot(normal, point) - offset
Definition math_functions.h:66
2D rotation This is similar to using a complex number for rotation
Definition math_functions.h:38
2D vector This can be used to represent a point or free vector
Definition math_functions.h:21