-
Notifications
You must be signed in to change notification settings - Fork 0
/
maths.c
96 lines (79 loc) · 2.28 KB
/
maths.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdlib.h>
#include <math.h>
#include "maths.h"
float assRand()
{
return (float)rand() / RAND_MAX;
}
float assRandRange(float min, float max)
{
return assLerp(min, max, assRand());
}
void assCircularRand(VU_VECTOR *out, float radius)
{
float a = assRandRange(0.0f, M_PI * 2.0f);
out->x = cosf(a) * radius;
out->y = sinf(a) * radius;
}
float assLerp(float v0, float v1, float t)
{
return (1 - t) * v0 + t * v1;
}
float assLengthVec2(VU_VECTOR *vect)
{
return sqrtf(vect->x * vect->x + vect->y * vect->y);
}
void assNormalizeVec2(VU_VECTOR *vect)
{
float len = assLengthVec2(vect);
vect->x /= len;
vect->y /= len;
}
void assRotateVec2(VU_VECTOR *vec, float angle)
{
float x = cosf(angle) * vec->x - sinf(angle) * vec->y;
float y = sinf(angle) * vec->x + cosf(angle) * vec->y;
vec->x = x;
vec->y = y;
}
float assClamp(float x, float min, float max)
{
return fminf(fmaxf(x, min), max);
}
void assOrthoProjRH(VU_MATRIX *output, float left, float right, float top, float bottom, float near, float far)
{
Vu0ResetMatrix(output);
// scale
output->m[0][0] = 2.0f / (right - left);
output->m[1][1] = 2.0f / (top - bottom);
output->m[2][2] = -2.0f / (far - near);
// translation
output->m[3][0] = -(right + left) / (right - left);
output->m[3][1] = -(top + bottom) / (top - bottom);
output->m[3][2] = -(far + near) / (far - near);
}
void assToFixed(xyz_t *output, float center_x, float center_y, float origin_x, float origin_y, VU_VECTOR *input)
{
// assume that input vector is in range [-1, 1]
// so we move it to the [0, 2] (by adding +1) and multiply it by half-screen size so we get [0, screenSize] in fixed point
// last step is to move the coordinates to the origin of the screen (GS settings)
u32 max_z = -1;
output->x = ((input->x + 1.0f) * ftoi4(center_x)) + ftoi4(origin_x);
output->y = ((input->y + 1.0f) * ftoi4(center_y)) + ftoi4(origin_y);
output->z = input->z * max_z;
}
float assTestCircle(VU_VECTOR *a, VU_VECTOR *b)
{
VU_VECTOR diff = {
.x = b->x - a->x,
.y = b->y - a->y,
.z = b->z - a->z
};
diff.x *= diff.x;
diff.y *= diff.y;
diff.z *= diff.z;
float distSq = diff.x + diff.y;
float radSumSq = a->z + b->z;
radSumSq *= radSumSq;
return distSq - radSumSq;
}