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 2 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
37 changes: 37 additions & 0 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,43 @@ impl Transform {
self.local_z()
}

/// Translates the entity in the local space using a given direction, speed, and delta time.
///
/// The translation is applied directly to the entity's local position without considering
/// the entity's rotation. This means the entity moves along the provided direction in its
/// own local space.
///
/// # Parameters
/// - `direction`: A `Vec3` representing the direction of movement in the entity's local space.
/// - `speed`: A `f32` representing the speed at which the entity should move.
/// - `delta_time_seconds`: A `f32` representing the time elapsed in seconds, typically
/// the time since the last frame.
pub fn translate(&mut self, direction: Vec3, speed: f32, delta_time_seconds: f32) {
self.translation += direction * speed * delta_time_seconds;
}

/// Translates the entity in local space, taking into account the entity's rotation, speed,
/// and delta time.
///
/// The translation is relative to the entity's rotation. This means the movement will be in
/// the direction the entity is currently facing, which allows the entity to move "forward"
/// or in any rotated direction relative to its local orientation.
///
/// # Parameters
/// - `direction`: A `Vec3` representing the direction of movement, relative to the entity's
/// local orientation.
/// - `speed`: A `f32` representing the speed at which the entity should move.
/// - `delta_time_seconds`: A `f32` representing the time elapsed in seconds, typically
/// the time since the last frame.
pub fn translate_with_local_rotation(
&mut self,
direction: Vec3,
speed: f32,
delta_time_seconds: f32,
) {
self.translation += self.rotation * direction * speed * delta_time_seconds;
}

/// 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());
}
}