Repository navigation
Expand file tree
/
Copy pathMyQuaternion.java
More file actions
103 lines (71 loc) · 2.52 KB
/
Copy pathMyQuaternion.java
File metadata and controls
103 lines (71 loc) · 2.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
97
98
99
100
101
102
103
// Quaternions are essentially encodings for R^3 rotations
public class MyQuaternion {
private float x;
private float y;
private float z;
private float w;
public MyQuaternion(float x, float y, float z, float w) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public float length() {
return (float) Math.sqrt((x * x) + (y * y) + (z * z) + (w * w));
}
public MyQuaternion normalize() {
float len = this.length();
x /= len;
y /= len;
z /= len;
w /= len;
return this;
}
public MyQuaternion conjugate() {
return new MyQuaternion(-this.x, -this.y, -this.z, this.w);
}
public MyQuaternion mul(MyQuaternion other) {
float new_x = x * other.getW() + w * other.getX() + y * other.getZ() - z * other.getY();
float new_y = y * other.getW() + w * other.getY() + z * other.getX() - x * other.getZ();
float new_z = z * other.getW() + w * other.getZ() + x * other.getY() - y * other.getX();
float new_w = w * other.getW() - x * other.getX() - y * other.getY() - z * other.getZ();
return new MyQuaternion(new_x, new_y, new_z, new_w);
}
public MyQuaternion mul(Vector3F other) {
float w_ = -x * other.getX() - y * other.getY() - z * other.getZ();
float x_ = w * other.getX() + y * other.getZ() - z * other.getY();
float y_ = w * other.getY() + z * other.getX() - x * other.getZ();
float z_ = w * other.getZ() + x * other.getY() - y * other.getX();
return new MyQuaternion(x_, y_, z_, w_);
// float new_w = -x * other.getX() - y * other.getY() - z * other.getZ();
// float new_x = w * other.getX() + y * other.getZ() - z * other.getY();
// float new_y = w * other.getY() + z * other.getX() - x * other.getZ();
// float new_z = w * other.getZ() + x * other.getY() - y * other.getX();
//
// return new MyQuaternion(new_x, new_y, new_z, new_w);
}
public float getX() {
return x;
}
public void setX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void setY(float y) {
this.y = y;
}
public float getZ() {
return z;
}
public void setZ(float z) {
this.z = z;
}
public float getW() {
return w;
}
public void setW(float w) {
this.w = w;
}
}