-
Notifications
You must be signed in to change notification settings - Fork 25
/
math.cpp
159 lines (122 loc) · 3.65 KB
/
math.cpp
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
/*
___ ___ ___ ___
/\ \ |\__\ /\ \ /\__\
/::\ \ |:| | /::\ \ /::| |
/:/\:\ \ |:| | /:/\:\ \ /:|:| |
/:/ \:\ \ |:|__|__ /::\~\:\ \ /:/|:| |__
/:/__/ \:\__\ /::::\__\ /:/\:\ \:\__\ /:/ |:| /\__\
\:\ \ \/__/ /:/~~/~~ \/__\:\/:/ / \/__|:|/:/ /
\:\ \ /:/ / \::/ / |:/:/ /
\:\ \ \/__/ /:/ / |::/ /
\:\__\ /:/ / /:/ /
\/__/ \/__/ \/__/
revolt (4/2017)
credit: AimTux (https://github.com/AimTuxOfficial/AimTux)
*/
#include "math.h"
void Math::AngleVectors(const QAngle &angles, Vector& forward)
{
Assert(s_bMathlibInitialized);
Assert(forward);
float sp, sy, cp, cy;
SinCosX(DEG2RAD(angles[1]), sy, cy); // YAW = 1 (y)
SinCosX(DEG2RAD(angles[0]), sp, cp); // PITCH = 0 (x)
forward.x = cp * cy;
forward.y = cp * sy;
forward.z = -sp;
}
void Math::AngleVectors(const QAngle& angles, Vector& forward, Vector& right, Vector& up)
{
float sr, sp, sy, cr, cp, cy;
SinCosX(DEG2RAD(angles[1]), sy, cy);
SinCosX(DEG2RAD(angles[0]), sp, cp);
SinCosX(DEG2RAD(angles[2]), sr, cr);
forward.x = cp * cy;
forward.y = cp * sy;
forward.z = -sp;
right.x = (-1 * sr * sp * cy + -1 * cr * -sy);
right.y = (-1 * sr * sp * sy + -1 * cr * cy);
right.z = -1 * sr * cp;
up.x = (cr * sp * cy + -sr * -sy);
up.y = (cr * sp * sy + -sr * cy);
up.z = cr * cp;
}
void Math::NormalizeAngles(QAngle& angle)
{
while (angle.x > 89.0f)
angle.x -= 180.f;
while (angle.x < -89.0f)
angle.x += 180.f;
while (angle.y > 180.f)
angle.y -= 360.f;
while (angle.y < -180.f)
angle.y += 360.f;
}
void Math::ClampAngles(QAngle& angle)
{
if (angle.y > 180.0f)
angle.y = 180.0f;
else if (angle.y < -180.0f)
angle.y = -180.0f;
if (angle.x > 89.0f)
angle.x = 89.0f;
else if (angle.x < -89.0f)
angle.x = -89.0f;
angle.z = 0;
}
void Math::CorrectMovement(QAngle vOldAngles, CUserCmd* pCmd, float fOldForward, float fOldSidemove)
{
// side/forward move correction
float deltaView;
float f1;
float f2;
if (vOldAngles.y < 0.f)
f1 = 360.0f + vOldAngles.y;
else
f1 = vOldAngles.y;
if (pCmd->viewangles.y < 0.0f)
f2 = 360.0f + pCmd->viewangles.y;
else
f2 = pCmd->viewangles.y;
if (f2 < f1)
deltaView = abs(f2 - f1);
else
deltaView = 360.0f - abs(f1 - f2);
deltaView = 360.0f - deltaView;
pCmd->forwardmove = cos(DEG2RAD(deltaView)) * fOldForward + cos(DEG2RAD(deltaView + 90.f)) * fOldSidemove;
pCmd->sidemove = sin(DEG2RAD(deltaView)) * fOldForward + sin(DEG2RAD(deltaView + 90.f)) * fOldSidemove;
}
float Math::GetFov(const QAngle& viewAngle, const QAngle& aimAngle)
{
QAngle delta = aimAngle - viewAngle;
NormalizeAngles(delta);
return sqrtf(powf(delta.x, 2.0f) + powf(delta.y, 2.0f));
}
void Math::VectorAngles(const Vector& forward, QAngle &angles)
{
if (forward[1] == 0.0f && forward[0] == 0.0f)
{
angles[0] = (forward[2] > 0.0f) ? 270.0f : 90.0f; // Pitch (up/down)
angles[1] = 0.0f; //yaw left/right
}
else
{
angles[0] = atan2(-forward[2], forward.Length2D()) * -180 / M_PI;
angles[1] = atan2(forward[1], forward[0]) * 180 / M_PI;
if (angles[1] > 90)
angles[1] -= 180;
else if (angles[1] < 90)
angles[1] += 180;
else if (angles[1] == 90)
angles[1] = 0;
}
angles[2] = 0.0f;
}
QAngle Math::CalcAngle(Vector src, Vector dst)
{
QAngle angles;
Vector delta = src - dst;
Math::VectorAngles(delta, angles);
delta.Normalize();
return angles;
}