-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgamemath.c
82 lines (62 loc) · 1.48 KB
/
gamemath.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "gamemath.h"
#include <nusys.h>
// Quake III Fast-inverse square root
// Copied from Wikipedia: https://en.wikipedia.org/wiki/Fast_inverse_square_root#Overview_of_the_code
float Q_rsqrt( float number ) {
long i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( long * ) &y;
i = 0x5f3759df - ( i >> 1 );
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) );
return y;
}
float lerp(float a, float b, float f) {
return a + f * (b - a);
}
// Very crass wrapping function
float wrapMP(float f) {
while (f > M_PI) {
f -= (M_PI * 2.f);
}
while (f < -M_PI) {
f += (M_PI * 2.f);
}
return f;
}
float lerpAngle(float u, float v, float p) {
return u + p*wrapMP(v - u);
}
float clamp(float x, float min, float max) {
return MIN(max, MAX(min, x));
}
float distanceSq(const Vec2* a, const Vec2* b) {
const float deltaX = (a->x - b->x);
const float deltaY = (a->y - b->y);
return (deltaX * deltaX) + (deltaY * deltaY);
}
float lengthSq(const Vec2* a) {
const Vec2 ZERO = { 0.f, 0.f };
return distanceSq(a, &ZERO);
}
void normalize(Vec2* a) {
const float lengthSquared = lengthSq(a);
const float invLengthA = Q_rsqrt(lengthSquared);
a->x *= invLengthA;
a->y *= invLengthA;
}
float dotProduct(const Vec2* a, const Vec2* b) {
return (a->x * b->x) + (a->y * b->y);
}
float cubic(float t) {
return t * t * t;
}
int absInteger(int x) {
if (x < 0) {
return -x;
}
return x;
}