@@ -4,21 +4,58 @@ use bevy_math::{Mat3, Mat4, Quat, Vec3};
4
4
use bevy_reflect:: Reflect ;
5
5
use std:: ops:: Mul ;
6
6
7
+ /// Describe the position of an entity. If the entity has a parent, the position is relative
8
+ /// to its parent position.
9
+ ///
10
+ /// * To place or move an entity, you should set its [`Transform`].
11
+ /// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
12
+ /// * To get the global position of an entity, you should get its [`GlobalTransform`].
13
+ ///
14
+ /// ## [`Transform`] and [`GlobalTransform`]
15
+ ///
16
+ /// [`Transform`] is the position of an entity relative to its parent position, or the reference
17
+ /// frame if it doesn't have a [`Parent`](super::Parent).
18
+ ///
19
+ /// [`GlobalTransform`] is the position of an entity relative to the reference frame.
20
+ ///
21
+ /// [`GlobalTransform`] is updated from [`Transform`] in the system
22
+ /// [`transform_propagate_system`](crate::transform_propagate_system::transform_propagate_system).
23
+ ///
24
+ /// In pseudo code:
25
+ /// ```ignore
26
+ /// for entity in entities_without_parent:
27
+ /// set entity.global_transform to entity.transform
28
+ /// recursively:
29
+ /// set parent to current entity
30
+ /// for child in parent.children:
31
+ /// set child.global_transform to parent.global_transform * child.transform
32
+ /// ```
33
+ ///
34
+ /// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you
35
+ /// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
36
+ /// before the [`GlobalTransform`] is updated.
7
37
#[ derive( Debug , PartialEq , Clone , Copy , Reflect ) ]
8
38
#[ reflect( Component , PartialEq ) ]
9
39
pub struct Transform {
40
+ /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
10
41
pub translation : Vec3 ,
42
+ /// Rotation of the entity.
11
43
pub rotation : Quat ,
44
+ /// Scale of the entity.
12
45
pub scale : Vec3 ,
13
46
}
14
47
15
48
impl Transform {
16
- /// Create a new [`Transform`] at the position `(x, y, z)`
49
+ /// Creates a new [`Transform`] at the position `(x, y, z)`. In 2d, the `z` component
50
+ /// is used for z-ordering elements: higher `z`-value will be in front of lower
51
+ /// `z`-value.
17
52
#[ inline]
18
53
pub fn from_xyz ( x : f32 , y : f32 , z : f32 ) -> Self {
19
54
Self :: from_translation ( Vec3 :: new ( x, y, z) )
20
55
}
21
56
57
+ /// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on
58
+ /// all axes.
22
59
#[ inline]
23
60
pub const fn identity ( ) -> Self {
24
61
Transform {
@@ -28,6 +65,8 @@ impl Transform {
28
65
}
29
66
}
30
67
68
+ /// Extracts the translation, rotation, and scale from `matrix`. It must be a 3d affine
69
+ /// transformation matrix.
31
70
#[ inline]
32
71
pub fn from_matrix ( matrix : Mat4 ) -> Self {
33
72
let ( scale, rotation, translation) = matrix. to_scale_rotation_translation ( ) ;
@@ -39,6 +78,8 @@ impl Transform {
39
78
}
40
79
}
41
80
81
+ /// Creates a new [`Transform`], with `translation`. Rotation will be 0 and scale 1 on
82
+ /// all axes.
42
83
#[ inline]
43
84
pub fn from_translation ( translation : Vec3 ) -> Self {
44
85
Transform {
@@ -47,6 +88,8 @@ impl Transform {
47
88
}
48
89
}
49
90
91
+ /// Creates a new [`Transform`], with `rotation`. Translation will be 0 and scale 1 on
92
+ /// all axes.
50
93
#[ inline]
51
94
pub fn from_rotation ( rotation : Quat ) -> Self {
52
95
Transform {
@@ -55,6 +98,8 @@ impl Transform {
55
98
}
56
99
}
57
100
101
+ /// Creates a new [`Transform`], with `scale`. Translation will be 0 and rotation 0 on
102
+ /// all axes.
58
103
#[ inline]
59
104
pub fn from_scale ( scale : Vec3 ) -> Self {
60
105
Transform {
@@ -63,43 +108,48 @@ impl Transform {
63
108
}
64
109
}
65
110
66
- /// Returns transform with the same translation and scale, but rotation so that
67
- /// transform.forward() points at target
111
+ /// Updates and returns this [`Transform`] by rotating it so that its unit vector in the
112
+ /// local z direction is toward `target` and its unit vector in the local y direction
113
+ /// is toward `up`.
68
114
#[ inline]
69
115
pub fn looking_at ( mut self , target : Vec3 , up : Vec3 ) -> Self {
70
116
self . look_at ( target, up) ;
71
117
self
72
118
}
73
119
120
+ /// Returns the 3d affine transformation matrix from this transforms translation,
121
+ /// rotation, and scale.
74
122
#[ inline]
75
123
pub fn compute_matrix ( & self ) -> Mat4 {
76
124
Mat4 :: from_scale_rotation_translation ( self . scale , self . rotation , self . translation )
77
125
}
78
126
127
+ /// Get the unit vector in the local x direction.
79
128
#[ inline]
80
- /// Get the unit vector in the local x direction
81
129
pub fn local_x ( & self ) -> Vec3 {
82
130
self . rotation * Vec3 :: X
83
131
}
84
132
133
+ /// Get the unit vector in the local y direction.
85
134
#[ inline]
86
- /// Get the unit vector in the local y direction
87
135
pub fn local_y ( & self ) -> Vec3 {
88
136
self . rotation * Vec3 :: Y
89
137
}
90
138
139
+ /// Get the unit vector in the local z direction.
91
140
#[ inline]
92
- /// Get the unit vector in the local z direction
93
141
pub fn local_z ( & self ) -> Vec3 {
94
142
self . rotation * Vec3 :: Z
95
143
}
96
144
145
+ /// Rotates the transform by the given rotation.
97
146
#[ inline]
98
- /// Rotate the transform by the given rotation
99
147
pub fn rotate ( & mut self , rotation : Quat ) {
100
148
self . rotation *= rotation;
101
149
}
102
150
151
+ /// Multiplies `self` with `transform` component by component, returning the
152
+ /// resulting [`Transform`]
103
153
#[ inline]
104
154
pub fn mul_transform ( & self , transform : Transform ) -> Self {
105
155
let translation = self . mul_vec3 ( transform. translation ) ;
@@ -112,6 +162,7 @@ impl Transform {
112
162
}
113
163
}
114
164
165
+ /// Returns a [`Vec3`] of this [`Transform`] applied to `value`.
115
166
#[ inline]
116
167
pub fn mul_vec3 ( & self , mut value : Vec3 ) -> Vec3 {
117
168
value = self . rotation * value;
@@ -120,11 +171,15 @@ impl Transform {
120
171
value
121
172
}
122
173
174
+ /// Changes the `scale` of this [`Transform`], multiplying the current `scale` by
175
+ /// `scale_factor`.
123
176
#[ inline]
124
- pub fn apply_non_uniform_scale ( & mut self , scale : Vec3 ) {
125
- self . scale *= scale ;
177
+ pub fn apply_non_uniform_scale ( & mut self , scale_factor : Vec3 ) {
178
+ self . scale *= scale_factor ;
126
179
}
127
180
181
+ /// Rotates this [`Transform`] so that its unit vector in the local z direction is toward
182
+ /// `target` and its unit vector in the local y direction is toward `up`.
128
183
#[ inline]
129
184
pub fn look_at ( & mut self , target : Vec3 , up : Vec3 ) {
130
185
let forward = Vec3 :: normalize ( self . translation - target) ;
0 commit comments