-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.h
441 lines (362 loc) · 8.59 KB
/
math.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/// Math.h
// This header defines the Vector3 class and the CMath class
/*
The Vector3 class is a typical Vector3 definition with nasty and tasty operator overloads,
so operating with Vector3s should be better and faster.
The CMath class allows you to do maths calculations that come handy when coding things that involve
a lot of math, like an aimbot, or an external radar...
Credits to TeamGamerFood and giovanni-orciuolo (on github)
*/
#pragma once
#include <math.h>
#define M_RADPI 57.295779513082f
#define M_PI 3.14159265358979323846
#define M_PI_F ((float)(M_PI))
#define SQUARE( a ) a*a
#define DEG2RAD( x ) ( (float)(x) * (float)( M_PI_F / 180.f ) )
#define RAD2DEG( x ) ( (float)(x) * (float)( 180.f / M_PI_F ) )
#pragma warning(disable : 4244) //check: https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4244?redirectedfrom=MSDN&view=msvc-160
/// THE FASTEST SQRT ALIVE
double inline __declspec (naked) __stdcall FastSQRT(double n)
{
__asm {
fld qword ptr[esp + 4]
fsqrt
ret 8
}
}
class Vector3
{
public:
float x, y, z;
Vector3()
: x(0.f), y(0.f), z(0.f)
{
}
Vector3(float X, float Y, float Z)
: x(X), y(Y), z(Z)
{
}
Vector3(float XYZ)
: x(XYZ), y(XYZ), z(XYZ)
{
}
//Vector3( float* v )
//{
// x = v[ 0 ]; y = v[ 1 ]; z = v[ 2 ];
//}
//Vector3( const float* v )
//{
// x = v[ 0 ]; y = v[ 1 ]; z = v[ 2 ];
//}
inline Vector3& operator=(const Vector3& v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
//inline Vector3& operator=( const float* v )
//{
// x = v[ 0 ]; y = v[ 1 ]; z = v[ 2 ]; return *this;
//}
inline float& operator[](int i)
{
return ((float*)this)[i];
}
inline float operator[](int i) const
{
return ((float*)this)[i];
}
inline Vector3& operator+=(const Vector3& v)
{
x += v.x;
y += v.y;
z += v.z;
return *this;
}
inline Vector3& operator-=(const Vector3& v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
inline Vector3& operator*=(const Vector3& v)
{
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
inline Vector3& operator/=(const Vector3& v)
{
x /= v.x;
y /= v.y;
z /= v.z;
return *this;
}
inline Vector3& operator+=(float v)
{
x += v;
y += v;
z += v;
return *this;
}
inline Vector3& operator-=(float v)
{
x -= v;
y -= v;
z -= v;
return *this;
}
inline Vector3& operator*=(float v)
{
x *= v;
y *= v;
z *= v;
return *this;
}
inline Vector3& operator/=(float v)
{
x /= v;
y /= v;
z /= v;
return *this;
}
inline Vector3 operator-() const
{
return Vector3(-x, -y, -z);
}
inline Vector3 operator+(const Vector3& v) const
{
return Vector3(x + v.x, y + v.y, z + v.z);
}
inline Vector3 operator-(const Vector3& v) const
{
return Vector3(x - v.x, y - v.y, z - v.z);
}
inline Vector3 operator*(const Vector3& v) const
{
return Vector3(x * v.x, y * v.y, z * v.z);
}
inline Vector3 operator/(const Vector3& v) const
{
return Vector3(x / v.x, y / v.y, z / v.z);
}
inline Vector3 operator+(float v) const
{
return Vector3(x + v, y + v, z + v);
}
inline Vector3 operator-(float v) const
{
return Vector3(x - v, y - v, z - v);
}
inline Vector3 operator*(float v) const
{
return Vector3(x * v, y * v, z * v);
}
inline Vector3 operator/(float v) const
{
return Vector3(x / v, y / v, z / v);
}
inline float Length() const
{
return sqrtf(x * x + y * y + z * z);
}
inline float LengthSqr() const
{
return (x * x + y * y + z * z);
}
inline float LengthXY() const
{
return sqrtf(x * x + y * y);
}
inline float LengthXZ() const
{
return sqrtf(x * x + z * z);
}
inline float DistTo(const Vector3& v) const
{
return (*this - v).Length();
}
inline float Dot(const Vector3& v) const
{
return (x * v.x + y * v.y + z * v.z);
}
inline Vector3 Cross(const Vector3& v) const
{
return Vector3(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
}
inline bool IsZero() const
{
return (x > -0.01f && x < 0.01f
&& y > -0.01f && y < 0.01f
&& z > -0.01f && z < 0.01f);
}
inline void Reset()
{
x = 0.f; y = 0.f; z = 0.f;
}
};
/// TODO: Maybe update this class, for now I need it only in WorldToScreen
class Vector2
{
public:
float x, y;
};
class CMath
{
public:
void inline SinCos(float radians, float* sine, float* cosine)
{
__asm
{
fld DWORD PTR[radians]
fsincos
mov edx, DWORD PTR[cosine]
mov eax, DWORD PTR[sine]
fstp DWORD PTR[edx]
fstp DWORD PTR[eax]
}
*sine = sin(radians);
*cosine = cos(radians);
}
void AngleVectors(const Vector3& angles, Vector3* forward)
{
float sr, sp, sy, cr, cp, cy;
SinCos(DEG2RAD(angles[0]), &sy, &cy);
SinCos(DEG2RAD(angles[1]), &sp, &cp);
SinCos(DEG2RAD(angles[2]), &sr, &cr);
if (forward)
{
forward->x = cp * cy;
forward->y = cp * sy;
forward->z = -sp;
}
}
void AngleVectors(const Vector3& angles, Vector3* forward, Vector3* right, Vector3* up)
{
float sr, sp, sy, cr, cp, cy;
SinCos(DEG2RAD(angles[0]), &sy, &cy);
SinCos(DEG2RAD(angles[1]), &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;
}
}
float DotProduct(const Vector3& v1, const float* v2)
{
return v1.x * v2[0] + v1.y * v2[1] + v1.z * v2[2];
}
float Dot(const Vector3& v1, const Vector3& v2)
{
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
}
float VecLength(const Vector3& vec)
{
return FastSQRT(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
}
float VecDist(const Vector3& fVec1, const Vector3& fVec2)
{
return FastSQRT(pow(fVec1.x - fVec2.x, 2) + pow(fVec1.y - fVec2.y, 2) + pow(fVec1.z - fVec2.z, 2));
}
float GetFov(const Vector3& angle, const Vector3& src, const Vector3& dst)
{
Vector3 ang, aim;
ang = CalcAngle(src, dst);
MakeVector(angle, aim);
MakeVector(ang, ang);
float mag = FastSQRT(pow(aim.x, 2) + pow(aim.y, 2) + pow(aim.z, 2));
float u_dot_v = Dot(aim, ang);
return RAD2DEG(acos(u_dot_v / (pow(mag, 2))));
}
Vector3 CalcAngle(const Vector3& Source, const Vector3& Destination)
{
Vector3 angles;
Vector3 delta;
delta.x = (Source.x - Destination.x);
delta.y = (Source.y - Destination.y);
delta.z = (Source.z - Destination.z);
double hyp = FastSQRT(delta.x * delta.x + delta.y * delta.y);
angles.x = (float)(atanf(delta.z / hyp) * M_RADPI);
angles.y = (float)(atanf(delta.y / delta.x) * M_RADPI);
angles.z = 0.0f;
if (delta.x >= 0.0) { angles.y += 180.0f; }
return angles;
}
void VectorAngles(const Vector3& dir, Vector3& angles)
{
float hyp = FastSQRT((dir.x * dir.x) + (dir.y * dir.y));
angles.x = atanf(dir.z / hyp) * M_RADPI;
angles.y = atanf(dir.y / dir.x) * M_RADPI;
angles.z = 0.0f;
if (dir.x >= 0.0)
angles.y += 180.0f;
}
// normalize vector
void ClampAngle(Vector3& v)
{
if (v.x > 89.0f && v.x <= 180.0f)
v.x = 89.0f;
if (v.x > 180.f)
v.x -= 360.f;
if (v.x < -89.0f)
v.x = -89.0f;
if (v.y > 180.f)
v.y -= 360.f;
if (v.y < -180.f)
v.y += 360.f;
v.z = 0;
}
void SmoothAngle(const Vector3& ViewAngle, Vector3& DestAngles, float smooth)
{
if (smooth <= 0)
{
DestAngles = ViewAngle;
return;
}
Vector3 vecDelta = DestAngles - ViewAngle;
ClampAngle(vecDelta);
DestAngles = ViewAngle + (vecDelta / smooth);
}
void MakeVector(const Vector3& angle, Vector3& vector)
{
float pitch = float(angle[0] * M_PI / 180);
float yaw = float(angle[1] * M_PI / 180);
float tmp = float(cos(pitch));
vector[0] = float(-tmp * -cos(yaw));
vector[1] = float(sin(yaw) * tmp);
vector[2] = float(-sin(pitch));
}
Vector3 AngleToDirection(Vector3 angle)
{
// Convert angle to radians
angle.x = (float)DEG2RAD(angle.x);
angle.y = (float)DEG2RAD(angle.y);
float sinYaw = sin(angle.y);
float cosYaw = cos(angle.y);
float sinPitch = sin(angle.x);
float cosPitch = cos(angle.x);
Vector3 direction;
direction.x = cosPitch * cosYaw;
direction.y = cosPitch * sinYaw;
direction.z = -sinPitch;
return direction;
}
};