-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3.cpp
More file actions
96 lines (74 loc) · 1.52 KB
/
vector3.cpp
File metadata and controls
96 lines (74 loc) · 1.52 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
#include <vector3.h>
#include <quaternion.h>
Vector3& Vector3::set(const Vector3& v) {
x = v.x;
y = v.y;
z = v.z;
return *this;
}
Vector3& Vector3::set(float x, float y, float z) {
this -> x = x;
this -> y = y;
this -> z = z;
return *this;
}
Vector3& Vector3::scl(float alpha) {
x *= alpha;
y *= alpha;
z *= alpha;
return *this;
}
Vector3& Vector3::nor() {
float len = this -> len();
if(len != 0.0 && !(abs(len - 1.0) <= Q_FP_ERR)) {
len = sqrt(len);
x /= len;
y /= len;
z /= len;
}
return *this;
}
Vector3& Vector3::zero() {
x = y = z = 0.0;
}
float Vector3::dot(const Vector3& v) {
return x * v.x + y * v.y + z * v.z;
}
float Vector3::len() {
return sqrt(x*x + y*y + z*z);
}
Vector3& Vector3::transform(const Quaternion& q) {
tq0.set(x, y, z, 0.0);
tq1.set(q).conjugate().mulLeft(tq0).mulLeft(q);
x = tq1.x;
y = tq1.y;
z = tq1.z;
return *this;
}
Vector3& Vector3::lerp(Vector3& v, float alpha) {
x += alpha * (v.x - x);
y += alpha * (v.y - y);
z += alpha * (v.z - z);
return *this;
}
Vector3& Vector3::slerp(Vector3& v, float alpha) {
float dot = this -> dot(v);
if(dot > 0.9995 || dot < -0.9995) {
lerp(v, alpha);
} else {
float theta0 = acos(dot);
float theta = theta0 * alpha;
float st = sin(theta);
float tx = v.x - x * dot;
float ty = v.y - y * dot;
float tz = v.z - z * dot;
float l2 = tx * tx + ty * ty + tz * tz;
float dl = st * ((l2 < 0.0001) ? (1.0) : (1.0/sqrt(l2)));
scl(cos(theta));
x += tx * dl;
y += ty * dl;
z += tz * dl;
nor();
}
return *this;
}