From b01b0747b49c9cf251a75ccd9d633473781d67f6 Mon Sep 17 00:00:00 2001 From: Benjamin Lee Date: Wed, 28 Apr 2021 21:19:05 -0700 Subject: [PATCH 1/2] added transform inverse method Previously it was possible to convert a transform to a 4x4 matrix and then invert it, but inverting the transform in-place is both faster and more convenient. --- .../src/components/global_transform.rs | 16 ++++++++++++++++ .../bevy_transform/src/components/transform.rs | 16 ++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index 8d220f21f28a3..39253b1c56762 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -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 { diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index ad3812b0a773c..7137a900ea4fa 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -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. + #[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 { From 980cf52aead74b491b62f7f412d683cb57c0cb9e Mon Sep 17 00:00:00 2001 From: Benjamin Lee Date: Wed, 28 Apr 2021 21:21:05 -0700 Subject: [PATCH 2/2] added transform_point and transform_vector methods --- .../src/components/global_transform.rs | 18 ++++++++++++++++++ .../bevy_transform/src/components/transform.rs | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index 39253b1c56762..2037ef0ff4110 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -211,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. + #[inline] + pub fn transform_point(&self, point: Vec3) -> Vec3 { + self.scale * (self.rotation * point) + } + + /// 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 + /// 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) { diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index 7137a900ea4fa..e2fe334b82be2 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -223,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 { + 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]