-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVector3D.cpp
157 lines (129 loc) · 4.46 KB
/
Vector3D.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
#include "Vector3D.h"
#include <sol/sol.hpp>
#include "IrrManagers.h"
// Constructors
Vector3D::Vector3D() : x(0), y(0), z(0) {}
Vector3D::Vector3D(float x, float y, float z) : x(x), y(y), z(z) {}
Vector3D::Vector3D(float x) : x(x), y(x), z(x) {}
// Operator overloads
Vector3D Vector3D::operator+(const Vector3D& other) const {
return Vector3D(x + other.x, y + other.y, z + other.z);
}
Vector3D Vector3D::operator-(const Vector3D& other) const {
return Vector3D(x - other.x, y - other.y, z - other.z);
}
Vector3D Vector3D::operator*(float scalar) const {
return Vector3D(x * scalar, y * scalar, z * scalar);
}
Vector3D Vector3D::operator/(float scalar) const {
return Vector3D(x / scalar, y / scalar, z / scalar);
}
// Length
float Vector3D::length() const {
return std::sqrt(x * x + y * y + z * z);
}
// Normalize
Vector3D Vector3D::normalize() const {
return normalizeRange(0, 1);
}
// Normalize to range
Vector3D Vector3D::normalizeRange(float min, float max) const {
float len = length();
if (len > 0) {
float normX = x / len;
float normY = y / len;
float normZ = z / len;
float scale = max - min;
normX = normX * scale + min;
normY = normY * scale + min;
normZ = normZ * scale + min;
return Vector3D(normX, normY, normZ);
}
return Vector3D(0.0f, 0.0f, 0.0f);
}
// Dot product
float Vector3D::dot(const Vector3D& other) const {
return (x * other.x) + (y * other.y) + (z * other.z);
}
// Distance
float Vector3D::distance(const Vector3D& other) const {
float dx = x - other.x;
float dy = y - other.y;
float dz = z - other.z;
return std::sqrt(dx * dx + dy * dy + dz * dz);
}
// Rotate
Vector3D Vector3D::rotate(const Vector3D& rot) const {
// X-axis rotation
float cosX = std::cos(rot.x * 180.0 / PI);
float sinX = std::sin(rot.x * 180.0 / PI);
float newY = y * cosX - z * sinX;
float newZ = y * sinX + z * cosX;
// Y-axis rotation
float cosY = std::cos(rot.y * 180.0 / PI);
float sinY = std::sin(rot.y * 180.0 / PI);
float newX = x * cosY + newZ * sinY;
newZ = -x * sinY + newZ * cosY;
// Z-axis rotation
float cosZ = std::cos(rot.z * 180.0 / PI);
float sinZ = std::sin(rot.z * 180.0 / PI);
float finalX = newX * cosZ - newY * sinZ;
float finalY = newX * sinZ + newY * cosZ;
finalX = finalX * PI / 180.0;
finalY = finalY * PI / 180.0;
newZ = newZ * PI / 180.0;
return Vector3D(finalX, finalY, newZ);
}
// Convert to degrees
Vector3D Vector3D::deg() const {
return Vector3D(x * 180 / PI, y * 180 / PI, z * 180 / PI);
}
// Convert to radians
Vector3D Vector3D::rad() const {
return Vector3D(x * PI / 180, y * PI / 180, z * PI / 180);
}
// Angle between vectors
float Vector3D::angle(const Vector3D& other) const {
float thisDot = dot(other);
float lenProduct = length() * other.length();
if (lenProduct == 0) {
return 0.0f;
}
float cosTheta = thisDot / lenProduct;
cosTheta = std::fmax(-1.0f, std::fmin(1.0f, cosTheta));
return std::acos(cosTheta);
}
// Equality operators
bool Vector3D::operator==(const Vector3D& other) const {
return x == other.x && y == other.y && z == other.z;
}
bool Vector3D::operator!=(const Vector3D& other) const {
return !operator==(other);
}
// To string
std::string Vector3D::toString() const {
return "(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ")";
}
// Lua binding
void bindVector3D() {
sol::usertype<Vector3D> bindType = lua->new_usertype<Vector3D>("Vector3D",
sol::constructors<Vector3D(), Vector3D(float, float, float), Vector3D(float)>(),
sol::meta_function::addition, &Vector3D::operator+,
sol::meta_function::subtraction, &Vector3D::operator-,
sol::meta_function::multiplication, &Vector3D::operator*,
sol::meta_function::division, &Vector3D::operator/,
sol::meta_function::equal_to, &Vector3D::operator==,
"x", &Vector3D::x,
"y", &Vector3D::y,
"z", &Vector3D::z);
bindType["length"] = &Vector3D::length;
bindType["normalize"] = &Vector3D::normalize;
bindType["normalizeToRange"] = &Vector3D::normalizeRange;
bindType["dot"] = &Vector3D::dot;
bindType["distance"] = &Vector3D::distance;
bindType["toDegrees"] = &Vector3D::deg;
bindType["toRadians"] = &Vector3D::rad;
bindType["rotate"] = &Vector3D::rotate;
bindType["angle"] = &Vector3D::angle;
bindType["toStr"] = &Vector3D::toString;
}