-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMath.cpp
More file actions
80 lines (62 loc) · 1.37 KB
/
Math.cpp
File metadata and controls
80 lines (62 loc) · 1.37 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
#include "Math.h"
#include <cmath>
float Math::wrapAngleTo180(float angle)
{
angle = std::fmod(angle, 360.0f);
if (angle >= 180.0f) {
angle -= 360.0f;
}
if (angle < -180.0f) {
angle += 360.0f;
}
return angle;
}
Vector2 Math::vec_wrapAngleTo180(Vector2 angle)
{
return Vector2{
wrapAngleTo180(angle.X),
wrapAngleTo180(angle.Y),
};
}
float Math::coterminal(float angle) {
return std::fmod(angle, 180) < 0 ? angle + 170 : angle;
}
float Math::magnitude(Vector3 v)
{
return sqrt(v.X * v.X + v.Y * v.Y + v.Z * v.Z);
}
Vector3 Math::crossProduct(Vector3 v1, Vector3 v2)
{
return Vector3{
v1.Y * v2.Z - v1.Z * v2.Y,
v1.Z * v2.X - v1.X * v2.Z,
v1.X * v2.Y - v1.Y * v2.X
};
}
float Math::shortestDistance(Vector3 p, Vector3 a, Vector3 b)
{
Vector3 ab{ b.X - a.X, b.Y - a.Y, b.Z - a.Z };
Vector3 cp = crossProduct(
Vector3{ p.X - a.X, p.Y - a.Y, p.Z - a.Z },
ab
);
return magnitude(cp) / magnitude(ab);
}
Vector2 Math::getAngles(Vector3 pos, Vector3 pos1)
{
double d_x = pos1.X - pos.X;
double d_y = pos1.Y - pos.Y;
double d_Z = pos1.Z - pos.Z;
double hypothenuse = sqrt(d_x * d_x + d_Z * d_Z);
float yaw = radiantsToDeg(atan2(d_Z, d_x)) - 90.f;
float pitch = radiantsToDeg(-atan2(d_y, hypothenuse));
return Vector2(yaw, pitch);
}
float Math::radiantsToDeg(float x)
{
return x * 180.f / PI;
}
float Math::degToRadiants(float x)
{
return x * PI / 180.f;
}