@@ -4,21 +4,58 @@ use bevy_math::{Mat3, Mat4, Quat, Vec3};
44use bevy_reflect:: Reflect ;
55use std:: ops:: Mul ;
66
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.
737#[ derive( Debug , PartialEq , Clone , Copy , Reflect ) ]
838#[ reflect( Component , PartialEq ) ]
939pub struct Transform {
40+ /// Position of the entity. In 2d, the last value of the `Vec3` is used for z-ordering.
1041 pub translation : Vec3 ,
42+ /// Rotation of the entity.
1143 pub rotation : Quat ,
44+ /// Scale of the entity.
1245 pub scale : Vec3 ,
1346}
1447
1548impl 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.
1752 #[ inline]
1853 pub fn from_xyz ( x : f32 , y : f32 , z : f32 ) -> Self {
1954 Self :: from_translation ( Vec3 :: new ( x, y, z) )
2055 }
2156
57+ /// Creates a new identity [`Transform`], with no translation, rotation, and a scale of 1 on
58+ /// all axes.
2259 #[ inline]
2360 pub const fn identity ( ) -> Self {
2461 Transform {
@@ -28,6 +65,8 @@ impl Transform {
2865 }
2966 }
3067
68+ /// Extracts the translation, rotation, and scale from `matrix`. It must be a 3d affine
69+ /// transformation matrix.
3170 #[ inline]
3271 pub fn from_matrix ( matrix : Mat4 ) -> Self {
3372 let ( scale, rotation, translation) = matrix. to_scale_rotation_translation ( ) ;
@@ -39,6 +78,8 @@ impl Transform {
3978 }
4079 }
4180
81+ /// Creates a new [`Transform`], with `translation`. Rotation will be 0 and scale 1 on
82+ /// all axes.
4283 #[ inline]
4384 pub fn from_translation ( translation : Vec3 ) -> Self {
4485 Transform {
@@ -47,6 +88,8 @@ impl Transform {
4788 }
4889 }
4990
91+ /// Creates a new [`Transform`], with `rotation`. Translation will be 0 and scale 1 on
92+ /// all axes.
5093 #[ inline]
5194 pub fn from_rotation ( rotation : Quat ) -> Self {
5295 Transform {
@@ -55,6 +98,8 @@ impl Transform {
5598 }
5699 }
57100
101+ /// Creates a new [`Transform`], with `scale`. Translation will be 0 and rotation 0 on
102+ /// all axes.
58103 #[ inline]
59104 pub fn from_scale ( scale : Vec3 ) -> Self {
60105 Transform {
@@ -63,43 +108,48 @@ impl Transform {
63108 }
64109 }
65110
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`.
68114 #[ inline]
69115 pub fn looking_at ( mut self , target : Vec3 , up : Vec3 ) -> Self {
70116 self . look_at ( target, up) ;
71117 self
72118 }
73119
120+ /// Returns the 3d affine transformation matrix from this transforms translation,
121+ /// rotation, and scale.
74122 #[ inline]
75123 pub fn compute_matrix ( & self ) -> Mat4 {
76124 Mat4 :: from_scale_rotation_translation ( self . scale , self . rotation , self . translation )
77125 }
78126
127+ /// Get the unit vector in the local x direction.
79128 #[ inline]
80- /// Get the unit vector in the local x direction
81129 pub fn local_x ( & self ) -> Vec3 {
82130 self . rotation * Vec3 :: X
83131 }
84132
133+ /// Get the unit vector in the local y direction.
85134 #[ inline]
86- /// Get the unit vector in the local y direction
87135 pub fn local_y ( & self ) -> Vec3 {
88136 self . rotation * Vec3 :: Y
89137 }
90138
139+ /// Get the unit vector in the local z direction.
91140 #[ inline]
92- /// Get the unit vector in the local z direction
93141 pub fn local_z ( & self ) -> Vec3 {
94142 self . rotation * Vec3 :: Z
95143 }
96144
145+ /// Rotates the transform by the given rotation.
97146 #[ inline]
98- /// Rotate the transform by the given rotation
99147 pub fn rotate ( & mut self , rotation : Quat ) {
100148 self . rotation *= rotation;
101149 }
102150
151+ /// Multiplies `self` with `transform` component by component, returning the
152+ /// resulting [`Transform`]
103153 #[ inline]
104154 pub fn mul_transform ( & self , transform : Transform ) -> Self {
105155 let translation = self . mul_vec3 ( transform. translation ) ;
@@ -112,6 +162,7 @@ impl Transform {
112162 }
113163 }
114164
165+ /// Returns a [`Vec3`] of this [`Transform`] applied to `value`.
115166 #[ inline]
116167 pub fn mul_vec3 ( & self , mut value : Vec3 ) -> Vec3 {
117168 value = self . rotation * value;
@@ -120,11 +171,15 @@ impl Transform {
120171 value
121172 }
122173
174+ /// Changes the `scale` of this [`Transform`], multiplying the current `scale` by
175+ /// `scale_factor`.
123176 #[ 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 ;
126179 }
127180
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`.
128183 #[ inline]
129184 pub fn look_at ( & mut self , target : Vec3 , up : Vec3 ) {
130185 let forward = Vec3 :: normalize ( self . translation - target) ;
0 commit comments