bevy_transform/bundles.rs
1use bevy_ecs::bundle::Bundle;
2
3use crate::prelude::{GlobalTransform, Transform};
4
5/// A [`Bundle`] of the [`Transform`] and [`GlobalTransform`]
6/// [`Component`]s, which describe the position of an entity.
7///
8/// * To place or move an entity, you should set its [`Transform`].
9/// * To get the global transform of an entity, you should get its [`GlobalTransform`].
10/// * For transform hierarchies to work correctly, you must have both a [`Transform`] and a [`GlobalTransform`].
11/// * You may use the [`TransformBundle`] to guarantee this.
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.
17///
18/// [`GlobalTransform`] is the position of an entity relative to the reference frame.
19///
20/// [`GlobalTransform`] is updated from [`Transform`] by systems in the system set
21/// [`TransformPropagate`](TransformSystem::TransformPropagate).
22///
23/// This system runs during [`PostUpdate`]. If you
24/// update the [`Transform`] of an entity in this schedule or after, you will notice a 1 frame lag
25/// before the [`GlobalTransform`] is updated.
26#[derive(Clone, Copy, Debug, Default, Bundle)]
27pub struct TransformBundle {
28 /// The transform of the entity.
29 pub local: Transform,
30 /// The global transform of the entity.
31 pub global: GlobalTransform,
32}
33
34impl TransformBundle {
35 /// An identity [`TransformBundle`] with no translation, rotation, and a scale of 1 on all axes.
36 pub const IDENTITY: Self = TransformBundle {
37 local: Transform::IDENTITY,
38 global: GlobalTransform::IDENTITY,
39 };
40
41 /// Creates a new [`TransformBundle`] from a [`Transform`].
42 ///
43 /// This initializes [`GlobalTransform`] as identity, to be updated later by the
44 /// [`PostUpdate`] schedule.
45 #[inline]
46 pub const fn from_transform(transform: Transform) -> Self {
47 TransformBundle {
48 local: transform,
49 ..Self::IDENTITY
50 }
51 }
52}
53
54impl From<Transform> for TransformBundle {
55 #[inline]
56 fn from(transform: Transform) -> Self {
57 Self::from_transform(transform)
58 }
59}