-
Notifications
You must be signed in to change notification settings - Fork 2
/
MathFunctions.cpp
189 lines (151 loc) · 3.49 KB
/
MathFunctions.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
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
/*
Syn's AyyWare Framework 2015
*/
#include "MathFunctions.h"
#include "CommonIncludes.h"
void AngleVectors(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 VectorTransform(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 SinCos(float a, float* s, float*c)
{
*s = sin(a);
*c = cos(a);
}
void VectorAngles(Vector forward, Vector &angles)
{
float tmp, yaw, pitch;
if (forward[2] == 0 && forward[0] == 0)
{
yaw = 0;
if (forward[2] > 0)
pitch = 90;
else
pitch = 270;
}
else
{
yaw = (atan2(forward[1], forward[0]) * 180 / PI);
if (yaw < 0)
yaw += 360;
tmp = sqrt(forward[0] * forward[0] + forward[1] * forward[1]);
pitch = (atan2(-forward[2], tmp) * 180 / PI);
if (pitch < 0)
pitch += 360;
}
if (pitch > 180)
pitch -= 360;
else if (pitch < -180)
pitch += 360;
if (yaw > 180)
yaw -= 360;
else if (yaw < -180)
yaw += 360;
if (pitch > 89)
pitch = 89;
else if (pitch < -89)
pitch = -89;
if (yaw > 180)
yaw = 180;
else if (yaw < -180)
yaw = -180;
angles[0] = pitch;
angles[1] = yaw;
angles[2] = 0;
}
void AngleVectors(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 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 CalcAngle(Vector src, Vector dst, Vector &angles)
{
Vector delta = src - dst;
double hyp = delta.Length2D(); //delta.Length
angles.y = (atan(delta.y / delta.x) * 57.295779513082f);
angles.x = (atan(delta.z / hyp) * 57.295779513082f);
angles[2] = 0.00;
if (delta.x >= 0.0)
angles.y += 180.0f;
}
bool IsVisible(IClientEntity* pLocal, IClientEntity* pEntity, int BoneID)
{
if (BoneID < 0) return false;
trace_t Trace;
Vector start = pLocal->GetOrigin() + pLocal->GetViewOffset();
Vector end = GetHitboxPosition(pEntity, BoneID);//pEntity->GetBonePos(BoneID);
UTIL_TraceLine(start, end, MASK_SOLID, pLocal, 0, &Trace);
if (Trace.m_pEnt == pEntity)
{
return true;
}
if (Trace.fraction == 1.0f)
{
return true;
}
return false;
}
void NormalizeVector(Vector& vec) {
for (int i = 0; i < 3; ++i) {
while (vec[i] > 180.f)
vec[i] -= 360.f;
while (vec[i] < -180.f)
vec[i] += 360.f;
}
vec[2] = 0.f;
}
void CalcAngleYawOnly(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.00;
if (delta.x >= 0.0)
angles.y += 180.0f;
}