forked from faggotman69/Prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathFunctions.cpp
More file actions
158 lines (124 loc) · 3.48 KB
/
MathFunctions.cpp
File metadata and controls
158 lines (124 loc) · 3.48 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
#include "Vector.h"
#include "MathFunctions.h"
#include "CommonIncludes.h"
#define M_PI 3.14159265358979323846
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 AngleVectors2(const Vector& qAngles, Vector& vecForward)
{
float sp, sy, cp, cy;
SinCos((float)(qAngles[1] * (M_PI / 180.f)), &sy, &cy);
SinCos((float)(qAngles[0] * (M_PI / 180.f)), &sp, &cp);
vecForward[0] = cp*cy;
vecForward[1] = cp*sy;
vecForward[2] = -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;
yaw = (atan2(forward[1], forward[0]) * 180 / PI);
tmp = sqrt(forward[0] * forward[0] + forward[1] * forward[1]);
pitch = (atan2(-forward[2], tmp) * 180 / PI);
while (yaw <= -180) yaw += 360;
while (yaw > 180) yaw -= 360;
while (pitch <= -180) pitch += 360;
while (pitch > 180) pitch -= 360;
if (pitch > 89.0f)
pitch = 89.0f;
else if (pitch < -89.0f)
pitch = -89.0f;
if (yaw > 179.99999f)
yaw = 179.99999f;
else if (yaw < -179.99999f)
yaw = -179.99999f;
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();
angles.y = (atan(delta.y / delta.x) * 57.295779513082f);
angles.x = (vec_t)(atan(delta.z / hyp) * 57.295779513082f);
angles[2] = 0.00;
if (delta.x >= 0.0)
angles.y += 180.0f;
}
void AverageDifference(const Vector& a, const Vector& b, float& result)
{
Vector calcvec;
calcvec.x = abs(a.x - b.x);
calcvec.y = abs(a.y - b.y);
calcvec.z = abs(a.y - b.y);
result = (calcvec.x + calcvec.y + calcvec.z) / 3.f;
}
Vector CalcAngle(Vector& src, Vector& dst)
{
Vector vAngle;
Vector delta((src.x - dst.x), (src.y - dst.y), (src.z - dst.z));
double hyp = sqrt(delta.x*delta.x + delta.y*delta.y);
vAngle.x = float(atanf(float(delta.z / hyp)) * 57.295779513082f);
vAngle.y = float(atanf(float(delta.y / delta.x)) * 57.295779513082f);
vAngle.z = 0.0f;
if (delta.x >= 0.0)
vAngle.y += 180.0f;
return vAngle;
}