Skip to main content

ursus_ecs/components/
transform.rs

1use glam::{Mat4, Quat, Vec3};
2
3#[derive(Debug, Copy, Clone)]
4pub struct Transform {
5    pub position: Vec3,
6    pub rotation: Quat,
7    pub scale: Vec3,
8}
9
10impl Transform {
11    pub fn identity() -> Self {
12        Self { position: Vec3::ZERO, rotation: Quat::IDENTITY, scale: Vec3::ONE }
13    }
14
15    pub fn at(x: f32, y: f32, z: f32) -> Self {
16        Self { position: Vec3::new(x, y, z), ..Self::identity() }
17    }
18
19    pub fn with_scale(mut self, s: f32) -> Self {
20        self.scale = Vec3::splat(s);
21        self
22    }
23
24    pub fn with_rotation(mut self, rotation: Quat) -> Self {
25        self.rotation = rotation;
26        self
27    }
28
29    pub fn matrix(&self) -> Mat4 {
30        Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.position)
31    }
32}
33
34impl Default for Transform {
35    fn default() -> Self {
36        Self::identity()
37    }
38}