forked from faggotman69/Prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathFunctions.h
More file actions
109 lines (91 loc) · 3.1 KB
/
MathFunctions.h
File metadata and controls
109 lines (91 loc) · 3.1 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
#pragma once
#include "Vector.h"
#include <algorithm>
#include <stdint.h>
#define PI 3.14159265358979323846f
#define DEG2RAD( x ) ( ( float )( x ) * ( float )( ( float )( PI ) / 180.0f ) )
#define RAD2DEG( x ) ( ( float )( x ) * ( float )( 180.0f / ( float )( PI ) ) )
#define RADPI 57.295779513082f
void AngleVectors(const Vector &angles, Vector *forward);
void VectorTransform(const Vector in1, float in2[3][4], Vector &out);
void SinCos(float a, float* s, float*c);
void VectorAngles(Vector forward, Vector &angles);
void AngleVectors(const Vector &angles, Vector *forward, Vector *right, Vector *up);
void Normalize(Vector &vIn, Vector &vOut);
void CalcAngle(Vector src, Vector dst, Vector &angles);
void AngleVectors2(const Vector& qAngles, Vector& vecForward);
void ClampAngles(Vector& inputangle);
void AverageDifference(const Vector& a, const Vector& b, float& result);
Vector CalcAngle(Vector& src, Vector& dst);
#define M_RADPI 57.295779513082f
inline void compute_angle(const Vector &source, const Vector &destination, QAngle& angles)
{
Vector delta = source - destination;
angles.x = static_cast< float >(asin(delta.z / delta.Length()) * M_RADPI);
angles.y = static_cast< float >(atan(delta.y / delta.x) * M_RADPI);
angles.z = 0.0f;
if (delta.x >= 0.0f)
angles.y += 180.0f;
}
inline QAngle compute_angle(const Vector &source, const Vector &destination)
{
QAngle angles;
Vector delta = source - destination;
angles.x = static_cast< float >(asin(delta.z / delta.Length()) * M_RADPI);
angles.y = static_cast< float >(atan(delta.y / delta.x) * M_RADPI);
angles.z = 0.0f;
if (delta.x >= 0.0f)
angles.y += 180.0f;
return angles;
}
inline float get_distance(const Vector &start, const Vector &end)
{
float distance = sqrt((start - end).Length());
if (distance < 1.0f)
distance = 1.0f;
return distance;
}
inline void clamp_angles(QAngle& angles)
{
if (angles.x > 89.0f) angles.x = 89.0f;
else if (angles.x < -89.0f) angles.x = -89.0f;
if (angles.y > 180.0f) angles.y = 180.0f;
else if (angles.y < -180.0f) angles.y = -180.0f;
angles.z = 0;
}
inline void normalize_angles(QAngle& angles)
{
for (auto i = 0; i < 3; i++) {
while (angles[i] < -180.0f) angles[i] += 360.0f;
while (angles[i] > 180.0f) angles[i] -= 360.0f;
}
}
inline bool sanitize_angles(QAngle &angles)
{
QAngle temp = angles;
normalize_angles(temp);
clamp_angles(temp);
if (!isfinite(temp.x) ||
!isfinite(temp.y) ||
!isfinite(temp.z))
return false;
angles = temp;
return true;
}
inline Vector angle_vector(Vector meme)
{
auto sy = sin(meme.y / 180.f * static_cast<float>(PI));
auto cy = cos(meme.y / 180.f * static_cast<float>(PI));
auto sp = sin(meme.x / 180.f * static_cast<float>(PI));
auto cp = cos(meme.x / 180.f* static_cast<float>(PI));
return Vector(cp*cy, cp*sy, -sp);
}
inline float distance_point_to_line(Vector Point, Vector LineOrigin, Vector Dir)
{
auto PointDir = Point - LineOrigin;
auto TempOffset = PointDir.Dot(Dir) / (Dir.x*Dir.x + Dir.y*Dir.y + Dir.z*Dir.z);
if (TempOffset < 0.000001f)
return FLT_MAX;
auto PerpendicularPoint = LineOrigin + (Dir * TempOffset);
return (Point - PerpendicularPoint).Length();
}