Box2D 3.1.0
A 2D physics engine for games
 
Loading...
Searching...
No Matches
math_functions.h
1// SPDX-FileCopyrightText: 2023 Erin Catto
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include "base.h"
7
8#include <float.h>
9#include <math.h>
10#include <stdbool.h>
11
17
20typedef struct b2Vec2
21{
23 float x, y;
24} b2Vec2;
25
28typedef struct b2CosSin
29{
31 float cosine;
32 float sine;
33} b2CosSin;
34
37typedef struct b2Rot
38{
40 float c, s;
41} b2Rot;
42
44typedef struct b2Transform
45{
46 b2Vec2 p;
47 b2Rot q;
49
51typedef struct b2Mat22
52{
55} b2Mat22;
56
58typedef struct b2AABB
59{
60 b2Vec2 lowerBound;
61 b2Vec2 upperBound;
62} b2AABB;
63
65typedef struct b2Plane
66{
67 b2Vec2 normal;
68 float offset;
69} b2Plane;
70
72
77
79#define B2_PI 3.14159265359f
80
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 } };
85
87B2_INLINE int b2MinInt( int a, int b )
88{
89 return a < b ? a : b;
90}
91
93B2_INLINE int b2MaxInt( int a, int b )
94{
95 return a > b ? a : b;
96}
97
99B2_INLINE int b2AbsInt( int a )
100{
101 return a < 0 ? -a : a;
102}
103
105B2_INLINE int b2ClampInt( int a, int lower, int upper )
106{
107 return a < lower ? lower : ( a > upper ? upper : a );
108}
109
111B2_INLINE float b2MinFloat( float a, float b )
112{
113 return a < b ? a : b;
114}
115
117B2_INLINE float b2MaxFloat( float a, float b )
118{
119 return a > b ? a : b;
120}
121
123B2_INLINE float b2AbsFloat( float a )
124{
125 return a < 0 ? -a : a;
126}
127
129B2_INLINE float b2ClampFloat( float a, float lower, float upper )
130{
131 return a < lower ? lower : ( a > upper ? upper : a );
132}
133
138B2_API float b2Atan2( float y, float x );
139
142B2_API b2CosSin b2ComputeCosSin( float radians );
143
145B2_INLINE float b2Dot( b2Vec2 a, b2Vec2 b )
146{
147 return a.x * b.x + a.y * b.y;
148}
149
151B2_INLINE float b2Cross( b2Vec2 a, b2Vec2 b )
152{
153 return a.x * b.y - a.y * b.x;
154}
155
157B2_INLINE b2Vec2 b2CrossVS( b2Vec2 v, float s )
158{
159 return B2_LITERAL( b2Vec2 ){ s * v.y, -s * v.x };
160}
161
163B2_INLINE b2Vec2 b2CrossSV( float s, b2Vec2 v )
164{
165 return B2_LITERAL( b2Vec2 ){ -s * v.y, s * v.x };
166}
167
170{
171 return B2_LITERAL( b2Vec2 ){ -v.y, v.x };
172}
173
176{
177 return B2_LITERAL( b2Vec2 ){ v.y, -v.x };
178}
179
181B2_INLINE b2Vec2 b2Add( b2Vec2 a, b2Vec2 b )
182{
183 return B2_LITERAL( b2Vec2 ){ a.x + b.x, a.y + b.y };
184}
185
187B2_INLINE b2Vec2 b2Sub( b2Vec2 a, b2Vec2 b )
188{
189 return B2_LITERAL( b2Vec2 ){ a.x - b.x, a.y - b.y };
190}
191
193B2_INLINE b2Vec2 b2Neg( b2Vec2 a )
194{
195 return B2_LITERAL( b2Vec2 ){ -a.x, -a.y };
196}
197
200B2_INLINE b2Vec2 b2Lerp( b2Vec2 a, b2Vec2 b, float t )
201{
202 return B2_LITERAL( b2Vec2 ){ ( 1.0f - t ) * a.x + t * b.x, ( 1.0f - t ) * a.y + t * b.y };
203}
204
206B2_INLINE b2Vec2 b2Mul( b2Vec2 a, b2Vec2 b )
207{
208 return B2_LITERAL( b2Vec2 ){ a.x * b.x, a.y * b.y };
209}
210
212B2_INLINE b2Vec2 b2MulSV( float s, b2Vec2 v )
213{
214 return B2_LITERAL( b2Vec2 ){ s * v.x, s * v.y };
215}
216
218B2_INLINE b2Vec2 b2MulAdd( b2Vec2 a, float s, b2Vec2 b )
219{
220 return B2_LITERAL( b2Vec2 ){ a.x + s * b.x, a.y + s * b.y };
221}
222
224B2_INLINE b2Vec2 b2MulSub( b2Vec2 a, float s, b2Vec2 b )
225{
226 return B2_LITERAL( b2Vec2 ){ a.x - s * b.x, a.y - s * b.y };
227}
228
230B2_INLINE b2Vec2 b2Abs( b2Vec2 a )
231{
232 b2Vec2 b;
233 b.x = b2AbsFloat( a.x );
234 b.y = b2AbsFloat( a.y );
235 return b;
236}
237
239B2_INLINE b2Vec2 b2Min( b2Vec2 a, b2Vec2 b )
240{
241 b2Vec2 c;
242 c.x = b2MinFloat( a.x, b.x );
243 c.y = b2MinFloat( a.y, b.y );
244 return c;
245}
246
248B2_INLINE b2Vec2 b2Max( b2Vec2 a, b2Vec2 b )
249{
250 b2Vec2 c;
251 c.x = b2MaxFloat( a.x, b.x );
252 c.y = b2MaxFloat( a.y, b.y );
253 return c;
254}
255
257B2_INLINE b2Vec2 b2Clamp( b2Vec2 v, b2Vec2 a, b2Vec2 b )
258{
259 b2Vec2 c;
260 c.x = b2ClampFloat( v.x, a.x, b.x );
261 c.y = b2ClampFloat( v.y, a.y, b.y );
262 return c;
263}
264
266B2_INLINE float b2Length( b2Vec2 v )
267{
268 return sqrtf( v.x * v.x + v.y * v.y );
269}
270
272B2_INLINE float b2Distance( b2Vec2 a, b2Vec2 b )
273{
274 float dx = b.x - a.x;
275 float dy = b.y - a.y;
276 return sqrtf( dx * dx + dy * dy );
277}
278
282{
283 float length = sqrtf( v.x * v.x + v.y * v.y );
284 if ( length < FLT_EPSILON )
285 {
286 return B2_LITERAL( b2Vec2 ){ 0.0f, 0.0f };
287 }
288
289 float invLength = 1.0f / length;
290 b2Vec2 n = { invLength * v.x, invLength * v.y };
291 return n;
292}
293
295B2_INLINE bool b2IsNormalized( b2Vec2 a )
296{
297 float aa = b2Dot( a, a );
298 return b2AbsFloat( 1.0f - aa ) < 10.0f * FLT_EPSILON;
299}
300
303B2_INLINE b2Vec2 b2GetLengthAndNormalize( float* length, b2Vec2 v )
304{
305 *length = sqrtf( v.x * v.x + v.y * v.y );
306 if ( *length < FLT_EPSILON )
307 {
308 return B2_LITERAL( b2Vec2 ){ 0.0f, 0.0f };
309 }
310
311 float invLength = 1.0f / *length;
312 b2Vec2 n = { invLength * v.x, invLength * v.y };
313 return n;
314}
315
318{
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 };
322 return qn;
323}
324
328B2_INLINE b2Rot b2IntegrateRotation( b2Rot q1, float deltaAngle )
329{
330 // dc/dt = -omega * sin(t)
331 // ds/dt = omega * cos(t)
332 // c2 = c1 - omega * h * s1
333 // s2 = s1 + omega * h * c1
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 };
338 return qn;
339}
340
342B2_INLINE float b2LengthSquared( b2Vec2 v )
343{
344 return v.x * v.x + v.y * v.y;
345}
346
348B2_INLINE float b2DistanceSquared( b2Vec2 a, b2Vec2 b )
349{
350 b2Vec2 c = { b.x - a.x, b.y - a.y };
351 return c.x * c.x + c.y * c.y;
352}
353
355B2_INLINE b2Rot b2MakeRot( float radians )
356{
357 b2CosSin cs = b2ComputeCosSin( radians );
358 return B2_LITERAL( b2Rot ){ cs.cosine, cs.sine };
359}
360
363
365B2_INLINE bool b2IsNormalizedRot( b2Rot q )
366{
367 // larger tolerance due to failure on mingw 32-bit
368 float qq = q.s * q.s + q.c * q.c;
369 return 1.0f - 0.0006f < qq && qq < 1.0f + 0.0006f;
370}
371
375B2_INLINE b2Rot b2NLerp( b2Rot q1, b2Rot q2, float t )
376{
377 float omt = 1.0f - t;
378 b2Rot q = {
379 omt * q1.c + t * q2.c,
380 omt * q1.s + t * q2.s,
381 };
382
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 };
386 return qn;
387}
388
393B2_INLINE float b2ComputeAngularVelocity( b2Rot q1, b2Rot q2, float inv_h )
394{
395 // ds/dt = omega * cos(t)
396 // dc/dt = -omega * sin(t)
397 // s2 = s1 + omega * h * c1
398 // c2 = c1 - omega * h * s1
399
400 // omega * h * s1 = c1 - c2
401 // omega * h * c1 = s2 - s1
402 // omega * h = (c1 - c2) * s1 + (s2 - s1) * c1;
403 // omega * h = s1 * c1 - c2 * s1 + s2 * c1 - s1 * c1
404 // omega * h = s2 * c1 - c2 * s1 = sin(a2 - a1) ~= a2 - a1 for small delta
405 float omega = inv_h * ( q2.s * q1.c - q2.c * q1.s );
406 return omega;
407}
408
410B2_INLINE float b2Rot_GetAngle( b2Rot q )
411{
412 return b2Atan2( q.s, q.c );
413}
414
417{
418 b2Vec2 v = { q.c, q.s };
419 return v;
420}
421
424{
425 b2Vec2 v = { -q.s, q.c };
426 return v;
427}
428
430B2_INLINE b2Rot b2MulRot( b2Rot q, b2Rot r )
431{
432 // [qc -qs] * [rc -rs] = [qc*rc-qs*rs -qc*rs-qs*rc]
433 // [qs qc] [rs rc] [qs*rc+qc*rs -qs*rs+qc*rc]
434 // s(q + r) = qs * rc + qc * rs
435 // c(q + r) = qc * rc - qs * rs
436 b2Rot qr;
437 qr.s = q.s * r.c + q.c * r.s;
438 qr.c = q.c * r.c - q.s * r.s;
439 return qr;
440}
441
443B2_INLINE b2Rot b2InvMulRot( b2Rot q, b2Rot r )
444{
445 // [ qc qs] * [rc -rs] = [qc*rc+qs*rs -qc*rs+qs*rc]
446 // [-qs qc] [rs rc] [-qs*rc+qc*rs qs*rs+qc*rc]
447 // s(q - r) = qc * rs - qs * rc
448 // c(q - r) = qc * rc + qs * rs
449 b2Rot qr;
450 qr.s = q.c * r.s - q.s * r.c;
451 qr.c = q.c * r.c + q.s * r.s;
452 return qr;
453}
454
456B2_INLINE float b2RelativeAngle( b2Rot b, b2Rot a )
457{
458 // sin(b - a) = bs * ac - bc * as
459 // cos(b - a) = bc * ac + bs * as
460 float s = b.s * a.c - b.c * a.s;
461 float c = b.c * a.c + b.s * a.s;
462 return b2Atan2( s, c );
463}
464
466B2_INLINE float b2UnwindAngle( float radians )
467{
468 if ( radians < -B2_PI )
469 {
470 return radians + 2.0f * B2_PI;
471 }
472 else if ( radians > B2_PI )
473 {
474 return radians - 2.0f * B2_PI;
475 }
476
477 return radians;
478}
479
481B2_INLINE float b2UnwindLargeAngle( float radians )
482{
483 while ( radians > B2_PI )
484 {
485 radians -= 2.0f * B2_PI;
486 }
487
488 while ( radians < -B2_PI )
489 {
490 radians += 2.0f * B2_PI;
491 }
492
493 return radians;
494}
495
498{
499 return B2_LITERAL( b2Vec2 ){ q.c * v.x - q.s * v.y, q.s * v.x + q.c * v.y };
500}
501
504{
505 return B2_LITERAL( b2Vec2 ){ q.c * v.x + q.s * v.y, -q.s * v.x + q.c * v.y };
506}
507
510{
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;
513
514 return B2_LITERAL( b2Vec2 ){ x, y };
515}
516
519{
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 };
523}
524
531{
532 b2Transform C;
533 C.q = b2MulRot( A.q, B.q );
534 C.p = b2Add( b2RotateVector( A.q, B.p ), A.p );
535 return C;
536}
537
542{
543 b2Transform C;
544 C.q = b2InvMulRot( A.q, B.q );
545 C.p = b2InvRotateVector( A.q, b2Sub( B.p, A.p ) );
546 return C;
547}
548
550B2_INLINE b2Vec2 b2MulMV( b2Mat22 A, b2Vec2 v )
551{
552 b2Vec2 u = {
553 A.cx.x * v.x + A.cy.x * v.y,
554 A.cx.y * v.x + A.cy.y * v.y,
555 };
556 return u;
557}
558
561{
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;
564 if ( det != 0.0f )
565 {
566 det = 1.0f / det;
567 }
568
569 b2Mat22 B = {
570 { det * d, -det * c },
571 { -det * b, det * a },
572 };
573 return B;
574}
575
579{
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;
582 if ( det != 0.0f )
583 {
584 det = 1.0f / det;
585 }
586 b2Vec2 x = { det * ( a22 * b.x - a12 * b.y ), det * ( a11 * b.y - a21 * b.x ) };
587 return x;
588}
589
591B2_INLINE bool b2AABB_Contains( b2AABB a, b2AABB b )
592{
593 bool s = true;
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;
598 return s;
599}
600
603{
604 b2Vec2 b = { 0.5f * ( a.lowerBound.x + a.upperBound.x ), 0.5f * ( a.lowerBound.y + a.upperBound.y ) };
605 return b;
606}
607
610{
611 b2Vec2 b = { 0.5f * ( a.upperBound.x - a.lowerBound.x ), 0.5f * ( a.upperBound.y - a.lowerBound.y ) };
612 return b;
613}
614
617{
618 b2AABB c;
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 );
623 return c;
624}
625
627B2_INLINE b2AABB b2MakeAABB( const b2Vec2* points, int count, float radius )
628{
629 B2_ASSERT( count > 0 );
630 b2AABB a = { points[0], points[0] };
631 for ( int i = 1; i < count; ++i )
632 {
633 a.lowerBound = b2Min( a.lowerBound, points[i] );
634 a.upperBound = b2Max( a.upperBound, points[i] );
635 }
636
637 b2Vec2 r = { radius, radius };
638 a.lowerBound = b2Sub( a.lowerBound, r );
639 a.upperBound = b2Add( a.upperBound, r );
640
641 return a;
642}
643
645B2_INLINE float b2PlaneSeparation( b2Plane plane, b2Vec2 point )
646{
647 return b2Dot( plane.normal, point ) - plane.offset;
648}
649
651B2_API bool b2IsValidFloat( float a );
652
654B2_API bool b2IsValidVec2( b2Vec2 v );
655
657B2_API bool b2IsValidRotation( b2Rot q );
658
660B2_API bool b2IsValidAABB( b2AABB aabb );
661
663B2_API bool b2IsValidPlane( b2Plane a );
664
679B2_API void b2SetLengthUnitsPerMeter( float lengthUnits );
680
682B2_API float b2GetLengthUnitsPerMeter( void );
683
685
693
694#ifdef __cplusplus
695
697inline void operator+=( b2Vec2& a, b2Vec2 b )
698{
699 a.x += b.x;
700 a.y += b.y;
701}
702
704inline void operator-=( b2Vec2& a, b2Vec2 b )
705{
706 a.x -= b.x;
707 a.y -= b.y;
708}
709
711inline void operator*=( b2Vec2& a, float b )
712{
713 a.x *= b;
714 a.y *= b;
715}
716
718inline b2Vec2 operator-( b2Vec2 a )
719{
720 return { -a.x, -a.y };
721}
722
724inline b2Vec2 operator+( b2Vec2 a, b2Vec2 b )
725{
726 return { a.x + b.x, a.y + b.y };
727}
728
730inline b2Vec2 operator-( b2Vec2 a, b2Vec2 b )
731{
732 return { a.x - b.x, a.y - b.y };
733}
734
736inline b2Vec2 operator*( float a, b2Vec2 b )
737{
738 return { a * b.x, a * b.y };
739}
740
742inline b2Vec2 operator*( b2Vec2 a, float b )
743{
744 return { a.x * b, a.y * b };
745}
746
748inline bool operator==( b2Vec2 a, b2Vec2 b )
749{
750 return a.x == b.x && a.y == b.y;
751}
752
754inline bool operator!=( b2Vec2 a, b2Vec2 b )
755{
756 return a.x != b.x || a.y != b.y;
757}
758
759#endif
760
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
A 2D rigid transform.
Definition math_functions.h:45
2D vector This can be used to represent a point or free vector
Definition math_functions.h:21