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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use super::Matrix4;
use super::Quaternion;
use super::Vector3;
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Transform {
pub position: Vector3,
pub scale: Vector3,
pub rotation: Quaternion,
}
impl Transform {
pub fn up(self) -> Vector3 {
self.rotation.inverse() * Vector3::UP
}
pub fn forward(self) -> Vector3 {
self.rotation.inverse() * Vector3::FORWARD
}
pub fn right(self) -> Vector3 {
self.rotation.inverse() * Vector3::RIGHT
}
pub fn move_by(&mut self, amount: impl Into<Vector3>) {
self.position += amount.into();
}
pub fn move_up(&mut self, amount: f32) {
self.move_by(self.up() * amount);
}
pub fn move_down(&mut self, amount: f32) {
self.move_by(-self.up() * amount);
}
pub fn move_right(&mut self, amount: f32) {
self.move_by(self.right() * amount);
}
pub fn move_left(&mut self, amount: f32) {
self.move_by(-self.right() * amount);
}
pub fn move_forward(&mut self, amount: f32) {
self.move_by(self.forward() * amount);
}
pub fn move_back(&mut self, amount: f32) {
self.move_by(-self.forward() * amount);
}
pub fn move_around_point(
&mut self,
point: impl Into<Vector3>,
angle: f32,
axis: impl Into<Vector3>,
) {
let point = point.into();
let rotation = Quaternion::axis_rotation(axis, angle);
self.position -= point;
self.position = rotation * self.position;
self.position += point;
}
pub fn look_dir(&mut self, dir: impl Into<Vector3>) {
let dir = dir.into().unit();
let up = if dir == Vector3::UP {
Vector3::FORWARD
} else {
Vector3::UP
};
self.rotation = Quaternion::look_rotation(dir, up);
}
pub fn look_at(&mut self, pos: impl Into<Vector3>) {
self.look_dir(pos.into() - self.position);
}
}
impl Default for Transform {
fn default() -> Self {
Self {
position: Vector3::default(),
scale: Vector3::new(1.0, 1.0, 1.0),
rotation: Quaternion::default(),
}
}
}
impl From<Matrix4> for Transform {
fn from(mut m: Matrix4) -> Self {
let position = Vector3::new(m.w.x, m.w.y, m.w.z);
let determinant = m.x.x * (m.y.y * m.z.z - m.z.y * m.y.z)
- m.y.x * (m.x.y * m.z.z - m.z.y * m.x.z)
+ m.z.x * (m.x.y * m.y.z - m.y.y * m.x.z);
let sx = m.x.xyz().length();
let sy = m.y.xyz().length();
let sz = m.z.xyz().length() * determinant.signum();
let scale = Vector3::new(sx, sy, sz);
m.x *= 1.0 / sx;
m.y *= 1.0 / sy;
m.z *= 1.0 / sz;
let rotation = Quaternion::from(m);
Self {
position,
scale,
rotation,
}
}
}
#[cfg(test)]
mod test {
use super::Quaternion;
use super::Transform;
use super::Vector3;
#[test]
fn default() {
let t = Transform::default();
assert_eq!(t.position, Vector3::new(0.0, 0.0, 0.0));
assert_eq!(t.scale, Vector3::new(1.0, 1.0, 1.0));
assert_eq!(t.rotation, Quaternion::new(0.0, 0.0, 0.0, 1.0));
}
}