Skip to content

Commit 348e2a3

Browse files
committed
documentation on Transform and GlobalTransform (#1687)
fixes #1599 * Added doc on `Transform` and `GlobalTransform` to describe usage and how `GlobalTransform` is updated * Documented all methods on `Transform` * `#[doc(hidden)]` most constructors and methods mutating `GlobalTransform`, documented the other * Mentioned z-ordering for `Transform` in 2d
1 parent 8d1e52b commit 348e2a3

File tree

3 files changed

+114
-16
lines changed

3 files changed

+114
-16
lines changed

crates/bevy_transform/src/components/global_transform.rs

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,35 @@ use bevy_math::{Mat3, Mat4, Quat, Vec3};
44
use bevy_reflect::Reflect;
55
use std::ops::Mul;
66

7+
/// Describe the position of an entity relative to the reference frame.
8+
///
9+
/// * To place or move an entity, you should set its [`Transform`].
10+
/// * To be displayed, an entity must have both a [`Transform`] and a [`GlobalTransform`].
11+
/// * To get the global position of an entity, you should get its [`GlobalTransform`].
12+
///
13+
/// ## [`Transform`] and [`GlobalTransform`]
14+
///
15+
/// [`Transform`] is the position of an entity relative to its parent position, or the reference
16+
/// frame if it doesn't have a [`Parent`](super::Parent).
17+
///
18+
/// [`GlobalTransform`] is the position of an entity relative to the reference frame.
19+
///
20+
/// [`GlobalTransform`] is updated from [`Transform`] in the system
21+
/// [`transform_propagate_system`](crate::transform_propagate_system::transform_propagate_system).
22+
///
23+
/// In pseudo code:
24+
/// ```ignore
25+
/// for entity in entities_without_parent:
26+
/// set entity.global_transform to entity.transform
27+
/// recursively:
28+
/// set parent to current entity
29+
/// for child in parent.children:
30+
/// set child.global_transform to parent.global_transform * child.transform
31+
/// ```
32+
///
33+
/// This system runs in stage [`CoreStage::PostUpdate`](crate::CoreStage::PostUpdate). If you
34+
/// update the[`Transform`] of an entity in this stage or after, you will notice a 1 frame lag
35+
/// before the [`GlobalTransform`] is updated.
736
#[derive(Debug, PartialEq, Clone, Copy, Reflect)]
837
#[reflect(Component, PartialEq)]
938
pub struct GlobalTransform {
@@ -13,12 +42,14 @@ pub struct GlobalTransform {
1342
}
1443

1544
impl GlobalTransform {
16-
/// Create a new [`GlobalTransform`] at the position `(x, y, z)`
45+
#[doc(hidden)]
1746
#[inline]
1847
pub fn from_xyz(x: f32, y: f32, z: f32) -> Self {
1948
Self::from_translation(Vec3::new(x, y, z))
2049
}
2150

51+
/// Creates a new identity [`GlobalTransform`], with no translation, rotation, and a scale of 1
52+
/// on all axes.
2253
#[inline]
2354
pub const fn identity() -> Self {
2455
GlobalTransform {
@@ -28,6 +59,7 @@ impl GlobalTransform {
2859
}
2960
}
3061

62+
#[doc(hidden)]
3163
#[inline]
3264
pub fn from_matrix(matrix: Mat4) -> Self {
3365
let (scale, rotation, translation) = matrix.to_scale_rotation_translation();
@@ -39,6 +71,7 @@ impl GlobalTransform {
3971
}
4072
}
4173

74+
#[doc(hidden)]
4275
#[inline]
4376
pub fn from_translation(translation: Vec3) -> Self {
4477
GlobalTransform {
@@ -47,6 +80,7 @@ impl GlobalTransform {
4780
}
4881
}
4982

83+
#[doc(hidden)]
5084
#[inline]
5185
pub fn from_rotation(rotation: Quat) -> Self {
5286
GlobalTransform {
@@ -55,6 +89,7 @@ impl GlobalTransform {
5589
}
5690
}
5791

92+
#[doc(hidden)]
5893
#[inline]
5994
pub fn from_scale(scale: Vec3) -> Self {
6095
GlobalTransform {
@@ -63,43 +98,46 @@ impl GlobalTransform {
6398
}
6499
}
65100

66-
/// Returns transform with the same translation and scale, but rotation so that
67-
/// transform.forward() points at target
101+
#[doc(hidden)]
68102
#[inline]
69103
pub fn looking_at(mut self, target: Vec3, up: Vec3) -> Self {
70104
self.look_at(target, up);
71105
self
72106
}
73107

108+
/// Returns the 3d affine transformation matrix from this transforms translation,
109+
/// rotation, and scale.
74110
#[inline]
75111
pub fn compute_matrix(&self) -> Mat4 {
76112
Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation)
77113
}
78114

79-
#[inline]
80115
/// Get the unit vector in the local x direction
116+
#[inline]
81117
pub fn local_x(&self) -> Vec3 {
82118
self.rotation * Vec3::X
83119
}
84120

85-
#[inline]
86121
/// Get the unit vector in the local y direction
122+
#[inline]
87123
pub fn local_y(&self) -> Vec3 {
88124
self.rotation * Vec3::Y
89125
}
90126

91-
#[inline]
92127
/// Get the unit vector in the local z direction
128+
#[inline]
93129
pub fn local_z(&self) -> Vec3 {
94130
self.rotation * Vec3::Z
95131
}
96132

133+
#[doc(hidden)]
97134
#[inline]
98-
/// Rotate the transform by the given rotation
99135
pub fn rotate(&mut self, rotation: Quat) {
100136
self.rotation *= rotation;
101137
}
102138

139+
/// Multiplies `self` with `transform` component by component, returning the
140+
/// resulting [`GlobalTransform`]
103141
#[inline]
104142
pub fn mul_transform(&self, transform: Transform) -> GlobalTransform {
105143
let translation = self.mul_vec3(transform.translation);
@@ -112,6 +150,7 @@ impl GlobalTransform {
112150
}
113151
}
114152

153+
/// Returns a [`Vec3`] of this [`Transform`] applied to `value`.
115154
#[inline]
116155
pub fn mul_vec3(&self, mut value: Vec3) -> Vec3 {
117156
value = self.rotation * value;
@@ -120,11 +159,13 @@ impl GlobalTransform {
120159
value
121160
}
122161

162+
#[doc(hidden)]
123163
#[inline]
124164
pub fn apply_non_uniform_scale(&mut self, scale: Vec3) {
125165
self.scale *= scale;
126166
}
127167

168+
#[doc(hidden)]
128169
#[inline]
129170
pub fn look_at(&mut self, target: Vec3, up: Vec3) {
130171
let forward = Vec3::normalize(self.translation - target);

crates/bevy_transform/src/components/transform.rs

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,58 @@ use bevy_math::{Mat3, Mat4, Quat, Vec3};
44
use bevy_reflect::Reflect;
55
use 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)]
939
pub 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

1548
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.
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);

crates/bevy_transform/src/transform_propagate_system.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use bevy_ecs::{
55
system::Query,
66
};
77

8+
/// Update [`GlobalTransform`] component of entities based on entity hierarchy and
9+
/// [`Transform`] component.
810
pub fn transform_propagate_system(
911
mut root_query: Query<
1012
(Entity, Option<&Children>, &Transform, &mut GlobalTransform),

0 commit comments

Comments
 (0)