effect_core/transform/
mod.rs

1use crate::primitives::{matrix::Matrix4, vector::Vector3};
2
3pub struct Transform2D {
4    matrix: Matrix4,
5    rotation: f32,
6    scale: f32,
7    position: Vector3<f32>,
8}
9
10impl Transform2D {
11    pub fn new() -> Self {
12        let matrix = Matrix4::new();
13        let rotation = 0.0;
14        let scale = 1.0;
15        let position = Vector3 {
16            x: 0.0,
17            y: 0.0,
18            z: 0.0,
19        };
20        Self {
21            matrix,
22            rotation,
23            scale,
24            position,
25        }
26    }
27
28    pub fn to_raw(&self) -> Matrix4 {
29        self.matrix
30    }
31
32    pub fn position(&self) -> &Vector3<f32> {
33        &self.position
34    }
35
36    pub fn rotation(&self) -> f32 {
37        self.rotation.to_degrees()
38    }
39
40    pub fn scale(&self) -> f32 {
41        self.scale
42    }
43}
44
45pub struct Transform2DSystem;
46
47impl Transform2DSystem {
48    pub fn rotate(transform: &mut Transform2D, degrees: f32) {
49        let degrees = degrees % 360.0;
50        let radians = degrees.to_radians();
51        transform.rotation = radians;
52        transform.matrix.inner[0][0] = radians.cos() * transform.scale;
53        transform.matrix.inner[0][1] = radians.sin() * transform.scale;
54        transform.matrix.inner[1][0] = -(radians.sin()) * transform.scale;
55        transform.matrix.inner[1][1] = radians.cos() * transform.scale;
56    }
57
58    pub fn translate(transform: &mut Transform2D, position: Vector3<f32>) {
59        transform.position = position;
60        transform.matrix.inner[3][0] = position.x * transform.rotation.cos() * transform.scale
61            + position.y * (-(transform.rotation.sin())) * transform.scale;
62        transform.matrix.inner[3][1] = position.x * transform.rotation.sin() * transform.scale
63            + position.y * transform.rotation.cos() * transform.scale;
64        transform.matrix.inner[3][2] = position.z;
65    }
66
67    pub fn scale(transform: &mut Transform2D, scale: f32) {
68        transform.scale = scale;
69        transform.matrix.inner[0][0] = transform.rotation.cos() * scale;
70        transform.matrix.inner[0][1] = transform.rotation.sin() * scale;
71        transform.matrix.inner[1][0] = -(transform.rotation.sin()) * scale;
72        transform.matrix.inner[1][1] = transform.rotation.cos() * scale;
73    }
74}