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
19#define b2_pi 3.14159265359f
20
23typedef struct b2Vec2
24{
26 float x, y;
27} b2Vec2;
28
31typedef struct b2Rot
32{
34 float c, s;
35} b2Rot;
36
38typedef struct b2Transform
39{
40 b2Vec2 p;
41 b2Rot q;
43
45typedef struct b2Mat22
46{
49} b2Mat22;
50
52typedef struct b2AABB
53{
54 b2Vec2 lowerBound;
55 b2Vec2 upperBound;
56} b2AABB;
57
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 } };
69
73B2_API float b2Atan2( float y, float x );
74
76B2_INLINE float b2MinFloat( float a, float b )
77{
78 return a < b ? a : b;
79}
80
82B2_INLINE float b2MaxFloat( float a, float b )
83{
84 return a > b ? a : b;
85}
86
88B2_INLINE float b2AbsFloat( float a )
89{
90 return a < 0 ? -a : a;
91}
92
94B2_INLINE float b2ClampFloat( float a, float lower, float upper )
95{
96 return a < lower ? lower : ( a > upper ? upper : a );
97}
98
100B2_INLINE int b2MinInt( int a, int b )
101{
102 return a < b ? a : b;
103}
104
106B2_INLINE int b2MaxInt( int a, int b )
107{
108 return a > b ? a : b;
109}
110
112B2_INLINE int b2AbsInt( int a )
113{
114 return a < 0 ? -a : a;
115}
116
118B2_INLINE int b2ClampInt( int a, int lower, int upper )
119{
120 return a < lower ? lower : ( a > upper ? upper : a );
121}
122
124B2_INLINE float b2Dot( b2Vec2 a, b2Vec2 b )
125{
126 return a.x * b.x + a.y * b.y;
127}
128
130B2_INLINE float b2Cross( b2Vec2 a, b2Vec2 b )
131{
132 return a.x * b.y - a.y * b.x;
133}
134
136B2_INLINE b2Vec2 b2CrossVS( b2Vec2 v, float s )
137{
138 return B2_LITERAL( b2Vec2 ){ s * v.y, -s * v.x };
139}
140
142B2_INLINE b2Vec2 b2CrossSV( float s, b2Vec2 v )
143{
144 return B2_LITERAL( b2Vec2 ){ -s * v.y, s * v.x };
145}
146
149{
150 return B2_LITERAL( b2Vec2 ){ -v.y, v.x };
151}
152
155{
156 return B2_LITERAL( b2Vec2 ){ v.y, -v.x };
157}
158
160B2_INLINE b2Vec2 b2Add( b2Vec2 a, b2Vec2 b )
161{
162 return B2_LITERAL( b2Vec2 ){ a.x + b.x, a.y + b.y };
163}
164
166B2_INLINE b2Vec2 b2Sub( b2Vec2 a, b2Vec2 b )
167{
168 return B2_LITERAL( b2Vec2 ){ a.x - b.x, a.y - b.y };
169}
170
172B2_INLINE b2Vec2 b2Neg( b2Vec2 a )
173{
174 return B2_LITERAL( b2Vec2 ){ -a.x, -a.y };
175}
176
179B2_INLINE b2Vec2 b2Lerp( b2Vec2 a, b2Vec2 b, float t )
180{
181 return B2_LITERAL( b2Vec2 ){ ( 1.0f - t ) * a.x + t * b.x, ( 1.0f - t ) * a.y + t * b.y };
182}
183
185B2_INLINE b2Vec2 b2Mul( b2Vec2 a, b2Vec2 b )
186{
187 return B2_LITERAL( b2Vec2 ){ a.x * b.x, a.y * b.y };
188}
189
191B2_INLINE b2Vec2 b2MulSV( float s, b2Vec2 v )
192{
193 return B2_LITERAL( b2Vec2 ){ s * v.x, s * v.y };
194}
195
197B2_INLINE b2Vec2 b2MulAdd( b2Vec2 a, float s, b2Vec2 b )
198{
199 return B2_LITERAL( b2Vec2 ){ a.x + s * b.x, a.y + s * b.y };
200}
201
203B2_INLINE b2Vec2 b2MulSub( b2Vec2 a, float s, b2Vec2 b )
204{
205 return B2_LITERAL( b2Vec2 ){ a.x - s * b.x, a.y - s * b.y };
206}
207
209B2_INLINE b2Vec2 b2Abs( b2Vec2 a )
210{
211 b2Vec2 b;
212 b.x = b2AbsFloat( a.x );
213 b.y = b2AbsFloat( a.y );
214 return b;
215}
216
218B2_INLINE b2Vec2 b2Min( b2Vec2 a, b2Vec2 b )
219{
220 b2Vec2 c;
221 c.x = b2MinFloat( a.x, b.x );
222 c.y = b2MinFloat( a.y, b.y );
223 return c;
224}
225
227B2_INLINE b2Vec2 b2Max( b2Vec2 a, b2Vec2 b )
228{
229 b2Vec2 c;
230 c.x = b2MaxFloat( a.x, b.x );
231 c.y = b2MaxFloat( a.y, b.y );
232 return c;
233}
234
236B2_INLINE b2Vec2 b2Clamp( b2Vec2 v, b2Vec2 a, b2Vec2 b )
237{
238 b2Vec2 c;
239 c.x = b2ClampFloat( v.x, a.x, b.x );
240 c.y = b2ClampFloat( v.y, a.y, b.y );
241 return c;
242}
243
245B2_INLINE float b2Length( b2Vec2 v )
246{
247 return sqrtf( v.x * v.x + v.y * v.y );
248}
249
251B2_INLINE float b2Distance( b2Vec2 a, b2Vec2 b )
252{
253 float dx = b.x - a.x;
254 float dy = b.y - a.y;
255 return sqrtf( dx * dx + dy * dy );
256}
257
260{
261 float length = sqrtf( v.x * v.x + v.y * v.y );
262 if ( length < FLT_EPSILON )
263 {
264 return b2Vec2_zero;
265 }
266
267 float invLength = 1.0f / length;
268 b2Vec2 n = { invLength * v.x, invLength * v.y };
269 return n;
270}
271
274B2_INLINE b2Vec2 b2GetLengthAndNormalize( float* length, b2Vec2 v )
275{
276 *length = b2Length( v );
277 if ( *length < FLT_EPSILON )
278 {
279 return b2Vec2_zero;
280 }
281
282 float invLength = 1.0f / *length;
283 b2Vec2 n = { invLength * v.x, invLength * v.y };
284 return n;
285}
286
289{
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 };
293 return qn;
294}
295
299B2_INLINE b2Rot b2IntegrateRotation( b2Rot q1, float deltaAngle )
300{
301 // dc/dt = -omega * sin(t)
302 // ds/dt = omega * cos(t)
303 // c2 = c1 - omega * h * s1
304 // s2 = s1 + omega * h * c1
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 };
309 return qn;
310}
311
313B2_INLINE float b2LengthSquared( b2Vec2 v )
314{
315 return v.x * v.x + v.y * v.y;
316}
317
319B2_INLINE float b2DistanceSquared( b2Vec2 a, b2Vec2 b )
320{
321 b2Vec2 c = { b.x - a.x, b.y - a.y };
322 return c.x * c.x + c.y * c.y;
323}
324
326B2_API b2Rot b2MakeRot( float angle );
327
329B2_INLINE bool b2IsNormalized( b2Rot q )
330{
331 // larger tolerance due to failure on mingw 32-bit
332 float qq = q.s * q.s + q.c * q.c;
333 return 1.0f - 0.0006f < qq && qq < 1.0f + 0.0006f;
334}
335
338B2_INLINE b2Rot b2NLerp( b2Rot q1, b2Rot q2, float t )
339{
340 float omt = 1.0f - t;
341 b2Rot q = {
342 omt * q1.c + t * q2.c,
343 omt * q1.s + t * q2.s,
344 };
345
346 return b2NormalizeRot( q );
347}
348
349
354B2_INLINE float b2ComputeAngularVelocity( b2Rot q1, b2Rot q2, float inv_h )
355{
356 // ds/dt = omega * cos(t)
357 // dc/dt = -omega * sin(t)
358 // s2 = s1 + omega * h * c1
359 // c2 = c1 - omega * h * s1
360
361 // omega * h * s1 = c1 - c2
362 // omega * h * c1 = s2 - s1
363 // omega * h = (c1 - c2) * s1 + (s2 - s1) * c1;
364 // omega * h = s1 * c1 - c2 * s1 + s2 * c1 - s1 * c1
365 // omega * h = s2 * c1 - c2 * s1 = sin(a2 - a1) ~= a2 - a1 for small delta
366 float omega = inv_h * ( q2.s * q1.c - q2.c * q1.s );
367 return omega;
368}
369
371B2_INLINE float b2Rot_GetAngle( b2Rot q )
372{
373 return b2Atan2( q.s, q.c );
374}
375
378{
379 b2Vec2 v = { q.c, q.s };
380 return v;
381}
382
385{
386 b2Vec2 v = { -q.s, q.c };
387 return v;
388}
389
391B2_INLINE b2Rot b2MulRot( b2Rot q, b2Rot r )
392{
393 // [qc -qs] * [rc -rs] = [qc*rc-qs*rs -qc*rs-qs*rc]
394 // [qs qc] [rs rc] [qs*rc+qc*rs -qs*rs+qc*rc]
395 // s(q + r) = qs * rc + qc * rs
396 // c(q + r) = qc * rc - qs * rs
397 b2Rot qr;
398 qr.s = q.s * r.c + q.c * r.s;
399 qr.c = q.c * r.c - q.s * r.s;
400 return qr;
401}
402
404B2_INLINE b2Rot b2InvMulRot( b2Rot q, b2Rot r )
405{
406 // [ qc qs] * [rc -rs] = [qc*rc+qs*rs -qc*rs+qs*rc]
407 // [-qs qc] [rs rc] [-qs*rc+qc*rs qs*rs+qc*rc]
408 // s(q - r) = qc * rs - qs * rc
409 // c(q - r) = qc * rc + qs * rs
410 b2Rot qr;
411 qr.s = q.c * r.s - q.s * r.c;
412 qr.c = q.c * r.c + q.s * r.s;
413 return qr;
414}
415
417B2_INLINE float b2RelativeAngle( b2Rot b, b2Rot a )
418{
419 // sin(b - a) = bs * ac - bc * as
420 // cos(b - a) = bc * ac + bs * as
421 float s = b.s * a.c - b.c * a.s;
422 float c = b.c * a.c + b.s * a.s;
423 return b2Atan2( s, c );
424}
425
427B2_INLINE float b2UnwindAngle( float angle )
428{
429 if ( angle < -b2_pi )
430 {
431 return angle + 2.0f * b2_pi;
432 }
433 else if ( angle > b2_pi )
434 {
435 return angle - 2.0f * b2_pi;
436 }
437
438 return angle;
439}
440
442B2_INLINE float b2UnwindLargeAngle( float angle )
443{
444 while ( angle > b2_pi )
445 {
446 angle -= 2.0f * b2_pi;
447 }
448
449 while ( angle < -b2_pi )
450 {
451 angle += 2.0f * b2_pi;
452 }
453
454 return angle;
455}
456
459{
460 return B2_LITERAL( b2Vec2 ){ q.c * v.x - q.s * v.y, q.s * v.x + q.c * v.y };
461}
462
465{
466 return B2_LITERAL( b2Vec2 ){ q.c * v.x + q.s * v.y, -q.s * v.x + q.c * v.y };
467}
468
471{
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;
474
475 return B2_LITERAL( b2Vec2 ){ x, y };
476}
477
480{
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 };
484}
485
489{
490 b2Transform C;
491 C.q = b2MulRot( A.q, B.q );
492 C.p = b2Add( b2RotateVector( A.q, B.p ), A.p );
493 return C;
494}
495
499{
500 b2Transform C;
501 C.q = b2InvMulRot( A.q, B.q );
502 C.p = b2InvRotateVector( A.q, b2Sub( B.p, A.p ) );
503 return C;
504}
505
507B2_INLINE b2Vec2 b2MulMV( b2Mat22 A, b2Vec2 v )
508{
509 b2Vec2 u = {
510 A.cx.x * v.x + A.cy.x * v.y,
511 A.cx.y * v.x + A.cy.y * v.y,
512 };
513 return u;
514}
515
518{
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;
521 if ( det != 0.0f )
522 {
523 det = 1.0f / det;
524 }
525
526 b2Mat22 B = {
527 { det * d, -det * c },
528 { -det * b, det * a },
529 };
530 return B;
531}
532
536{
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;
539 if ( det != 0.0f )
540 {
541 det = 1.0f / det;
542 }
543 b2Vec2 x = { det * ( a22 * b.x - a12 * b.y ), det * ( a11 * b.y - a21 * b.x ) };
544 return x;
545}
546
548B2_INLINE bool b2AABB_Contains( b2AABB a, b2AABB b )
549{
550 bool s = true;
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;
555 return s;
556}
557
560{
561 b2Vec2 b = { 0.5f * ( a.lowerBound.x + a.upperBound.x ), 0.5f * ( a.lowerBound.y + a.upperBound.y ) };
562 return b;
563}
564
567{
568 b2Vec2 b = { 0.5f * ( a.upperBound.x - a.lowerBound.x ), 0.5f * ( a.upperBound.y - a.lowerBound.y ) };
569 return b;
570}
571
574{
575 b2AABB c;
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 );
580 return c;
581}
582
584B2_API bool b2IsValid( float a );
585
587B2_API bool b2Vec2_IsValid( b2Vec2 v );
588
590B2_API bool b2Rot_IsValid( b2Rot q );
591
593B2_API bool b2AABB_IsValid( b2AABB aabb );
594
599B2_API void b2SetLengthUnitsPerMeter( float lengthUnits );
600
602B2_API float b2GetLengthUnitsPerMeter( void );
603
614#ifdef __cplusplus
615
617inline void operator+=( b2Vec2& a, b2Vec2 b )
618{
619 a.x += b.x;
620 a.y += b.y;
621}
622
624inline void operator-=( b2Vec2& a, b2Vec2 b )
625{
626 a.x -= b.x;
627 a.y -= b.y;
628}
629
631inline void operator*=( b2Vec2& a, float b )
632{
633 a.x *= b;
634 a.y *= b;
635}
636
638inline b2Vec2 operator-( b2Vec2 a )
639{
640 return { -a.x, -a.y };
641}
642
644inline b2Vec2 operator+( b2Vec2 a, b2Vec2 b )
645{
646 return { a.x + b.x, a.y + b.y };
647}
648
650inline b2Vec2 operator-( b2Vec2 a, b2Vec2 b )
651{
652 return { a.x - b.x, a.y - b.y };
653}
654
656inline b2Vec2 operator*( float a, b2Vec2 b )
657{
658 return { a * b.x, a * b.y };
659}
660
662inline b2Vec2 operator*( b2Vec2 a, float b )
663{
664 return { a.x * b, a.y * b };
665}
666
668inline bool operator==( b2Vec2 a, b2Vec2 b )
669{
670 return a.x == b.x && a.y == b.y;
671}
672
674inline bool operator!=( b2Vec2 a, b2Vec2 b )
675{
676 return a.x != b.x || a.y != b.y;
677}
678
679#endif
680
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
A 2D rigid transform.
Definition math_functions.h:39
2D vector This can be used to represent a point or free vector
Definition math_functions.h:24