-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMathFunctions.cpp
More file actions
160 lines (130 loc) · 3.39 KB
/
Copy pathMathFunctions.cpp
File metadata and controls
160 lines (130 loc) · 3.39 KB
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
#include "MathFunctions.h"
#include "CommonIncludes.h"
#include "Entities.h"
#include "Interfaces.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 inline SinCos(float radians, float *sine, float *cosine)
{
*sine = sin(radians);
*cosine = cos(radians);
}
inline Vector ExtrapolateTick(Vector p0, Vector v0)
{
return p0 + (v0 * I::Globals->interval_per_tick);
}
Vector IClientEntity::GetPredicted(Vector p0)
{
return ExtrapolateTick(p0, GetVelocity());
}
void VectorAngles(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 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 SEAngleFix(float &val)
{
while (val > 180) val -= 360;
while (val < -180) val += 360;
}
void CalcAngle(const Vector& vecSource, const Vector& vecDestination, Vector& qAngles)
{
Vector delta = Vector((vecSource[0] - vecDestination[0]), (vecSource[1] - vecDestination[1]), (vecSource[2] - vecDestination[2]));
double hyp = sqrtf(delta[0] * delta[0] + delta[1] * delta[1]);
qAngles[0] = (float)(atan(delta[2] / hyp) * (180.0 / M_PI));
qAngles[1] = (float)(atan(delta[1] / delta[0]) * (180.0 / M_PI));
qAngles[2] = 0.f;
if (delta[0] >= 0.f)
qAngles[1] += 180.f;
}
float AngleNormalize(float angle)
{
angle = fmodf(angle, 360.0f);
if (angle > 180)
{
angle -= 360;
}
if (angle < -180)
{
angle += 360;
}
return angle;
}
float GetFov(const Vector& viewAngle, const Vector& aimAngle)
{
Vector ang, aim;
AngleVectors(viewAngle, &aim);
AngleVectors(aimAngle, &ang);
return RAD2DEG(acos(aim.Dot(ang) / aim.LengthSqr()));
}
float VectorDistance(Vector v1, Vector v2)
{
return FASTSQRT(pow(v1.x - v2.x, 2) + pow(v1.y - v2.y, 2) + pow(v1.z - v2.z, 2));
}