Skip to content
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

[Merged by Bors] - Add Transform::look_to #6692

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
27 changes: 21 additions & 6 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,24 @@ impl Transform {
}
}

/// Updates and returns this [`Transform`] by rotating it so that its unit
/// vector in the local negative `Z` direction is toward `target` and its
/// unit vector in the local `Y` direction is toward `up`.
/// Returns this [`Transform`] with a new rotation so that [`Transform::forward`]
/// points towards the `target` position and [`Transform::up`] points towards `up`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should also keep the local z etc explanations, probably in parentheticals.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is already documented on the linked methods. I'd rather avoid repeating the documentation.
Although, since rust-analyzer doesn't support these links it does kinda suck. hmmmm

#[inline]
#[must_use]
pub fn looking_at(mut self, target: Vec3, up: Vec3) -> Self {
self.look_at(target, up);
self
}

/// Returns this [`Transform`] with a new rotation so that [`Transform::forward`]
/// points towards the given `direction` and [`Transform::up`] points towards `up`.
tim-blackbird marked this conversation as resolved.
Show resolved Hide resolved
#[inline]
#[must_use]
pub fn looking_to(mut self, direction: Vec3, up: Vec3) -> Self {
self.look_to(direction, up);
self
}

/// Returns this [`Transform`] with a new translation.
#[inline]
#[must_use]
Expand Down Expand Up @@ -314,11 +322,18 @@ impl Transform {
self.rotate(rotation);
}

/// Rotates this [`Transform`] so that its local negative `Z` direction is toward
/// `target` and its local `Y` direction is toward `up`.
/// Rotates this [`Transform`] so that [`Transform::forward`] points towards the `target` position,
/// and [`Transform::up`] points towards `up`.
#[inline]
pub fn look_at(&mut self, target: Vec3, up: Vec3) {
let forward = Vec3::normalize(self.translation - target);
self.look_to(target - self.translation, up);
}

/// Rotates this [`Transform`] so that [`Transform::forward`] points towards the given `direction`
tim-blackbird marked this conversation as resolved.
Show resolved Hide resolved
/// and [`Transform::up`] points towards `up`.
#[inline]
pub fn look_to(&mut self, direction: Vec3, up: Vec3) {
let forward = -direction.normalize();
let right = up.cross(forward).normalize();
let up = forward.cross(right);
self.rotation = Quat::from_mat3(&Mat3::from_cols(right, up, forward));
Expand Down