vigilant_lamp/engine/
transform.rs1use crate::{math::{array_ext::NumArray, matrix::{Matrix4}, quaternion::Quaternion32}};
2
3pub struct Transform {
5 pub position: NumArray<f32,3>,
6 pub rotation: Quaternion32,
7 pub scale: NumArray<f32,3>
8}
9
10impl Transform {
12 pub fn default () -> Transform {
13 Transform { position: NumArray::zero(), rotation: Quaternion32::zero_rotation(), scale: NumArray::one() }
14 }
15
16 pub fn new (position: NumArray<f32, 3>, rotation: Quaternion32, scale: NumArray<f32, 3>) -> Transform {
17 Transform { position, rotation, scale }
18 }
19
20 pub fn of_position (position: NumArray<f32, 3>) -> Transform {
21 Transform { position, rotation: Quaternion32::zero_rotation(), scale: NumArray::one() }
22 }
23
24 pub fn of_rotation (rotation: Quaternion32) -> Transform {
25 Transform { position: NumArray::zero(), rotation, scale: NumArray::one() }
26 }
27
28 pub fn of_scale (scale: NumArray<f32, 3>) -> Transform {
29 Transform { position: NumArray::zero(), rotation: Quaternion32::zero_rotation(), scale }
30 }
31
32 pub fn of_angles (roll: f32, pitch: f32, yaw: f32) -> Transform {
33 Transform { position: NumArray::zero(), rotation: Quaternion32::from_angles(roll, pitch, yaw), scale: NumArray::one() }
34 }
35}
36
37impl Transform {
39 pub fn set_scale (&mut self, value: f32) {
40 self.scale = NumArray([value, value, value])
41 }
42
43 pub fn rotate (&mut self, roll: f32, pitch: f32, yaw: f32) {
44 self.rotation = self.rotation * Quaternion32::from_angles(roll, pitch, yaw);
45 self.rotation = self.rotation.unit();
46 }
47
48 pub fn position_matrix (&self) -> Matrix4<f32> {
49 Matrix4::new([
50 NumArray([1., 0., 0., self.position.x()]),
51 NumArray([0., 1., 0., self.position.y()]),
52 NumArray([0., 0., 1., self.position.z()]),
53 NumArray([0., 0., 0., 1.]),
54 ])
55 }
56
57 pub fn scale_matrix (&self) -> Matrix4<f32> {
58 Matrix4::new([
59 NumArray([self.scale.x(), 0., 0., 0.]),
60 NumArray([0., self.scale.y(), 0., 0.]),
61 NumArray([0., 0., self.scale.z(), 0.]),
62 NumArray([0., 0., 0., 1.]),
63 ])
64 }
65
66 pub fn matrix (&self) -> Matrix4<f32> {
67 self.position_matrix() * self.rotation.rot_matrix4() * self.scale_matrix()
68 }
69}