-
Notifications
You must be signed in to change notification settings - Fork 5
/
GameMath.h
280 lines (240 loc) · 6.98 KB
/
GameMath.h
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#pragma once
#include "Interfaces.h"
class CGameMath
{
public:
float random_float(float min, float max)
{
typedef float(*RandomFloat_t)(float, float);
static RandomFloat_t m_RandomFloat = (RandomFloat_t)GetProcAddress(GetModuleHandle(XorStr("vstdlib.dll")), XorStr("RandomFloat"));
return m_RandomFloat(min, max);
}
int random_seed(int seed) {
typedef int(__cdecl* RandomSeed_t)(int iSeed);
static RandomSeed_t m_RandomSeed = (RandomSeed_t)GetProcAddress(GetModuleHandle(XorStr("vstdlib.dll")), XorStr("RandomSeed"));
return m_RandomSeed(seed);
}
void normalize_float(float& input)
{
while (input < 180)
input += 180;
while (input > 180)
input -= 180;
}
void normalize_vector(Vector& input)
{
while (input.y <= -180) input.y += 360;
while (input.y > 180) input.y -= 360;
while (input.x <= -180) input.x += 360;
while (input.x > 180) input.x -= 360;
if (input.x > 89.0) input.x = 89;
if (input.x < -89.0) input.x = -89;
if (input.y < -180) input.y = -179.999;
if (input.y > 180) input.y = 179.999;
input.z = 0;
}
void normalize(Vector &vIn, Vector &vOut)
{
float flLen = vIn.Length();
if (flLen == 0) {
vOut.Init(0, 0, 1);
return;
}
flLen = 1 / flLen;
vOut.Init(vIn.x * flLen, vIn.y * flLen, vIn.z * flLen);
}
void angle_vectors(const Vector &angles, Vector& forward)
{
Assert(s_bMathlibInitialized);
Assert(forward);
float sp, sy, cp, cy;
sy = sin(DEG2RAD(angles[1]));
cy = cos(DEG2RAD(angles[1]));
sp = sin(DEG2RAD(angles[0]));
cp = cos(DEG2RAD(angles[0]));
forward.x = cp*cy;
forward.y = cp*sy;
forward.z = -sp;
}
void vector_transform(const Vector in1, float in2[3][4], Vector &out)
{
out[0] = DotProduct(in1, Vector(in2[0][0], in2[0][1], in2[0][2])) + in2[0][3];
out[1] = DotProduct(in1, Vector(in2[1][0], in2[1][1], in2[1][2])) + in2[1][3];
out[2] = DotProduct(in1, Vector(in2[2][0], in2[2][1], in2[2][2])) + in2[2][3];
}
void vector_angles(const Vector &vecForward, Vector &vecAngles)
{
Vector vecView;
if (vecForward[1] == 0.f && vecForward[0] == 0.f)
{
vecView[0] = 0.f;
vecView[1] = 0.f;
}
else
{
vecView[1] = atan2(vecForward[1], vecForward[0]) * 180.f / 3.14159265358979323846f;
if (vecView[1] < 0.f)
vecView[1] += 360.f;
vecView[2] = sqrt(vecForward[0] * vecForward[0] + vecForward[1] * vecForward[1]);
vecView[0] = atan2(vecForward[2], vecView[2]) * 180.f / 3.14159265358979323846f;
}
vecAngles[0] = -vecView[0];
vecAngles[1] = vecView[1];
vecAngles[2] = 0.f;
}
void vector_angles(Vector &forward, Vector &up, Vector &angle)
{
Vector left = CrossProduct(up, forward);
left.NormalizeInPlace();
float forwardDist = forward.Length2D();
if (forwardDist > 0.001f)
{
angle.x = atan2f(-forward.z, forwardDist) * 180 / ((float)(3.14159265358979323846264338327950288419));
angle.y = atan2f(forward.y, forward.x) * 180 / ((float)(3.14159265358979323846264338327950288419));
float upZ = (left.y * forward.x) - (left.x * forward.y);
angle.z = atan2f(left.z, upZ) * 180 / ((float)(3.14159265358979323846264338327950288419));
}
else
{
angle.x = atan2f(-forward.z, forwardDist) * 180 / ((float)(3.14159265358979323846264338327950288419));
angle.y = atan2f(-left.x, left.y) * 180 / ((float)(3.14159265358979323846264338327950288419));
angle.z = 0;
}
}
inline vec_t vector_normalize(Vector& v)
{
vec_t l = v.Length();
if (l != 0.0f)
{
v /= l;
}
else
{
v.x = v.y = 0.0f; v.z = 1.0f;
}
return l;
}
void inline sincos(float radians, float *sine, float *cosine)
{
*sine = sin(radians);
*cosine = cos(radians);
}
void angle_vectors(const Vector &angles, Vector *forward) {
float sp, sy, cp, cy;
sincos(DEG2RAD(angles[1]), &sy, &cy);
sincos(DEG2RAD(angles[0]), &sp, &cp);
forward->x = cp*cy;
forward->y = cp*sy;
forward->z = -sp;
}
void angle_vectors(const Vector &angles, Vector *forward, Vector *right, Vector *up)
{
float sr, sp, sy, cr, cp, cy;
sincos(DEG2RAD(angles[1]), &sy, &cy);
sincos(DEG2RAD(angles[0]), &sp, &cp);
sincos(DEG2RAD(angles[2]), &sr, &cr);
if (forward)
{
forward->x = cp*cy;
forward->y = cp*sy;
forward->z = -sp;
}
if (right)
{
right->x = (-1 * sr*sp*cy + -1 * cr*-sy);
right->y = (-1 * sr*sp*sy + -1 * cr*cy);
right->z = -1 * sr*cp;
}
if (up)
{
up->x = (cr*sp*cy + -sr*-sy);
up->y = (cr*sp*sy + -sr*cy);
up->z = cr*cp;
}
}
void angle_clamp(CUserCmd* pCmd, Vector oang)
{
normalize_vector(pCmd->viewangles);
if (pCmd->viewangles.z != 0.0f)
pCmd->viewangles.z = 0.00;
if (pCmd->viewangles.x < -89 || pCmd->viewangles.x > 89 || pCmd->viewangles.y < -180 || pCmd->viewangles.y > 180)
{
normalize_vector(pCmd->viewangles);
if (pCmd->viewangles.x < -89 || pCmd->viewangles.x > 89 || pCmd->viewangles.y < -180 || pCmd->viewangles.y > 180)
pCmd->viewangles = oang;
}
}
void calculate_angle(Vector src, Vector dst, Vector &angles)
{
Vector delta = src - dst;
double hyp = delta.Length2D();
angles.y = (atan(delta.y / delta.x) * 57.295779513082f);
angles.x = (atan(delta.z / hyp) * 57.295779513082f);
angles[2] = 0.0f;
if (delta.x >= 0.0) angles.y += 180.0f;
}
FORCEINLINE void vector_subtract(const Vector& a, const Vector& b, Vector& c)
{
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
}
bool is_close(float value1, float value2, float max_difference)
{
float buffered[2] = { value1, value2 };
if (buffered[0] < 180) buffered[0] += 360;
if (buffered[0] > 180) buffered[0] -= 360;
if (buffered[1] < 180) buffered[1] += 360;
if (buffered[1] > 180) buffered[1] -= 360;
if ((buffered[0] - buffered[1]) >= -max_difference && (buffered[0] - buffered[1]) <= max_difference)
return true;
return false;
}
void matrix_set_column(const Vector &in, int column, matrix3x4_t& out)
{
out.m_flMatVal[0][column] = in.x;
out.m_flMatVal[1][column] = in.y;
out.m_flMatVal[2][column] = in.z;
}
void angle_matrix(Vector& angles, const Vector& position, matrix3x4_t& matrix)
{
angle_matrix(angles, matrix);
matrix_set_column(position, 3, matrix);
}
template<class T, class U>
T clamp(T in, U low, U high)
{
if (in <= low)
return low;
if (in >= high)
return high;
return in;
}
private:
void angle_matrix(Vector& angles, matrix3x4_t& matrix)
{
float angle;
float sr, sp, sy, cr, cp, cy;
angle = angles.x * (6.283185f / 360);
sp = sin(angle);
cp = cos(angle);
angle = angles.y * (6.283185f / 360);
sy = sin(angle);
cy = cos(angle);
angle = angles.z * (6.283185f / 360);
sr = sin(angle);
cr = cos(angle);
matrix.m_flMatVal[0][0] = cp * cy;
matrix.m_flMatVal[1][0] = cp * sy;
matrix.m_flMatVal[2][0] = -sp;
matrix.m_flMatVal[0][1] = sr * sp * cy + cr * -sy;
matrix.m_flMatVal[1][1] = sr * sp * sy + cr * cy;
matrix.m_flMatVal[2][1] = sr * cp;
matrix.m_flMatVal[0][2] = (cr * sp * cy + -sr * -sy);
matrix.m_flMatVal[1][2] = (cr * sp * sy + -sr * cy);
matrix.m_flMatVal[2][2] = cr * cp;
matrix.m_flMatVal[0][3] = 0.0;
matrix.m_flMatVal[1][3] = 0.0;
matrix.m_flMatVal[2][3] = 0.0;
}
};