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

Add a few transform convenience methods #2055

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
34 changes: 34 additions & 0 deletions crates/bevy_transform/src/components/global_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ impl GlobalTransform {
Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation)
}

/// Returns the inverse of this transform.
///
/// The inverse consists of applying the inverse rotation, scale, and
/// translation in reverse order.
#[inline]
pub fn inverse(&self) -> Transform {
let rotation = self.rotation.inverse();
let scale = rotation * -self.scale;
let translation = scale * -self.translation;
Transform {
rotation,
scale,
translation,
}
}

/// Get the unit vector in the local x direction
#[inline]
pub fn local_x(&self) -> Vec3 {
Expand Down Expand Up @@ -195,6 +211,24 @@ impl GlobalTransform {
value
}

/// Returns the result of applying this [`GlobalTransform`] to a [`Vec3`] interpreted as a point.
///
/// This applies rotation, scale, and translation. It's the equivalent of using w=1 in
/// homogeneous coordinates.
Copy link
Member

Choose a reason for hiding this comment

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

Could you link an appropriate explanatory article on homogenous coordinates as part of this? It's pretty dense for beginners.

https://www.tomdalling.com/blog/modern-opengl/explaining-homogenous-coordinates-and-projective-geometry/ looks quite nice.

#[inline]
pub fn transform_point(&self, point: Vec3) -> Vec3 {
self.scale * (self.rotation * point)
Copy link
Member

Choose a reason for hiding this comment

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

How does translation get applied here?

}

/// Returns the result of applying this [`GlobalTransform`] to a [`Vec3`] interpreted as a vector.
///
/// This applies rotation and scale, but not translation. It's the equivalent of using w=0 in
Copy link
Member

Choose a reason for hiding this comment

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

Did we swap the comments / math here?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah... I sure did. lol. I'll fix that

/// homogeneous coordinates.
#[inline]
pub fn transform_vector(&self, vector: Vec3) -> Vec3 {
self.scale * (self.rotation * point) + self.translation
}

#[doc(hidden)]
#[inline]
pub fn apply_non_uniform_scale(&mut self, scale: Vec3) {
Expand Down
34 changes: 34 additions & 0 deletions crates/bevy_transform/src/components/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ impl Transform {
Mat4::from_scale_rotation_translation(self.scale, self.rotation, self.translation)
}

/// Returns the inverse of this transform.
///
/// The inverse consists of applying the inverse rotation, scale, and
/// translation in reverse order.
Copy link
Member

Choose a reason for hiding this comment

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

I think spelling out A, then B, then C is clearer than "reverse order".

#[inline]
pub fn inverse(&self) -> Transform {
let rotation = self.rotation.inverse();
let scale = rotation * -self.scale;
let translation = scale * -self.translation;
Transform {
rotation,
scale,
translation,
}
}

/// Get the unit vector in the local x direction.
#[inline]
pub fn local_x(&self) -> Vec3 {
Expand Down Expand Up @@ -207,6 +223,24 @@ impl Transform {
value
}

/// Returns the result of applying this [`Transform`] to a [`Vec3`] interpreted as a point.
///
/// This applies rotation, scale, and translation. It's the equivalent of using w=1 in
/// homogeneous coordinates.
#[inline]
pub fn transform_point(&self, point: Vec3) -> Vec3 {
Copy link
Member

Choose a reason for hiding this comment

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

Does having two functions of the same name but different function arguments work in Rust?

Copy link
Contributor

Choose a reason for hiding this comment

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

rust doesn't have function overloading, so it should not work.

Copy link
Contributor

Choose a reason for hiding this comment

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

One of the functions is on Transform, the other on GlobalTransform.

self.scale * (self.rotation * point)
}

/// Returns the result of applying this [`Transform`] to a [`Vec3`] interpreted as a vector.
///
/// This applies rotation and scale, but not translation. It's the equivalent of using w=0 in
/// homogeneous coordinates.
#[inline]
pub fn transform_vector(&self, vector: Vec3) -> Vec3 {
self.scale * (self.rotation * point) + self.translation
}

/// Changes the `scale` of this [`Transform`], multiplying the current `scale` by
/// `scale_factor`.
#[inline]
Expand Down