gizmo_physics/components/
transform.rs1use gizmo_math::{Mat4, Quat, Vec3};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
5pub struct TransformData {
6 pub position: Vec3,
7 pub rotation: Quat,
8 pub scale: Vec3,
9}
10
11#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
12#[serde(from = "TransformData")]
13pub struct Transform {
14 pub position: Vec3,
15 pub rotation: Quat,
16 pub scale: Vec3,
17 #[serde(skip)]
18 pub local_matrix: Mat4,
19}
20
21impl From<TransformData> for Transform {
22 fn from(data: TransformData) -> Self {
23 let mut t = Self {
24 position: data.position,
25 rotation: data.rotation,
26 scale: data.scale,
27 local_matrix: Mat4::IDENTITY,
28 };
29 t.update_local_matrix();
30 t
31 }
32}
33
34impl Default for Transform {
35 fn default() -> Self {
36 Self::new(Vec3::ZERO)
37 }
38}
39
40impl Transform {
41 pub fn new(position: Vec3) -> Self {
42 let mut t = Self {
43 position,
44 rotation: Quat::IDENTITY,
45 scale: Vec3::ONE,
46 local_matrix: Mat4::IDENTITY,
47 };
48 t.update_local_matrix();
49 t
50 }
51
52 pub fn with_position(mut self, position: Vec3) -> Self {
53 self.position = position;
54 self.update_local_matrix();
55 self
56 }
57
58 pub fn with_scale(mut self, scale: Vec3) -> Self {
59 self.scale = scale;
60 self.update_local_matrix();
61 self
62 }
63
64 pub fn with_rotation(mut self, rotation: Quat) -> Self {
65 self.rotation = rotation;
66 self.update_local_matrix();
67 self
68 }
69
70 pub fn set_position(&mut self, pos: Vec3) {
71 self.position = pos;
72 self.update_local_matrix();
73 }
74
75 pub fn set_rotation(&mut self, rot: Quat) {
76 self.rotation = rot;
77 self.update_local_matrix();
78 }
79
80 pub fn set_scale(&mut self, scale: Vec3) {
81 self.scale = scale;
82 self.update_local_matrix();
83 }
84
85 #[inline]
87 pub fn rotate_x(&mut self, angle: f32) {
88 self.rotation *= Quat::from_rotation_x(angle);
89 self.update_local_matrix();
90 }
91
92 #[inline]
94 pub fn rotate_y(&mut self, angle: f32) {
95 self.rotation *= Quat::from_rotation_y(angle);
96 self.update_local_matrix();
97 }
98
99 #[inline]
101 pub fn rotate_z(&mut self, angle: f32) {
102 self.rotation *= Quat::from_rotation_z(angle);
103 self.update_local_matrix();
104 }
105
106 #[inline]
108 pub fn translate(&mut self, delta: Vec3) {
109 self.position += delta;
110 self.update_local_matrix();
111 }
112
113 pub fn update_local_matrix(&mut self) {
114 self.local_matrix =
115 Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.position);
116 }
117
118 pub fn world_matrix(&self, parent: Option<&Transform>) -> Mat4 {
119 match parent {
120 Some(p) => p.world_matrix(None) * self.local_matrix,
121 None => self.local_matrix,
122 }
123 }
124}
125
126gizmo_core::impl_component!(Transform);
127
128#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
129pub struct GlobalTransform {
130 #[serde(skip)]
131 pub matrix: Mat4,
132}
133
134impl Default for GlobalTransform {
135 fn default() -> Self {
136 Self {
137 matrix: Mat4::IDENTITY,
138 }
139 }
140}
141
142impl GlobalTransform {
143 pub fn compute_matrix(&self) -> Mat4 {
144 self.matrix
145 }
146}
147
148gizmo_core::impl_component!(GlobalTransform);