Skip to content

Add Transform Helper methods #15200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,29 @@ impl Transform {
self.local_z()
}

/// Moves the ['Transform'] within its local space using the provided translation vector.
///
/// This method directly adjusts the ['Transform'] position using the `translation` vector,
/// without considering the ['Transform'] current rotation. The translation happens in the
/// ['Transform'] local coordinate system, meaning the movement occurs along its local
/// axes (`x`, `y`, and `z`), regardless of the ['Transform'] orientation.
#[inline]
pub fn translate(&mut self, translation: Vec3) {
self.translation += translation;
}

/// Moves the ['Transform'] within its local space, considering its rotation and orientation.
///
/// This method translates the ['Transform'] based on its current rotation, so the movement
/// happens relative to the direction the ['Transform'] is facing. The `translation` vector is
/// transformed by the ['Transform'] rotation, allowing for natural directional movement.
/// For instance, moving "forward" means moving along the ['Transform'] forward direction
/// based on its current orientation, rather than just along the local z-axis.
#[inline]
pub fn translate_with_local_rotation(&mut self, translation: Vec3) {
self.translation += self.rotation * translation;
}

/// Rotates this [`Transform`] by the given rotation.
///
/// If this [`Transform`] has a parent, the `rotation` is relative to the rotation of the parent.
Expand Down
3 changes: 1 addition & 2 deletions examples/transforms/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ fn move_cube(mut cubes: Query<(&mut Transform, &mut Movable)>, timer: Res<Time>)
if (cube.spawn - transform.translation).length() > cube.max_distance {
cube.speed *= -1.0;
}
let direction = transform.local_x();
transform.translation += direction * cube.speed * timer.delta_seconds();
transform.translate(Vec3::X * cube.speed * timer.delta_seconds());
}
}