From 89fbe9c0d5931b13a45fdea76f791489c8828610 Mon Sep 17 00:00:00 2001 From: James Beilby Date: Thu, 11 Nov 2021 12:59:58 +0000 Subject: [PATCH 1/3] Added Transform::rotate_around method --- crates/bevy_transform/src/components/transform.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index cea0fc32cb032..cfab0766ea319 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -205,6 +205,14 @@ impl Transform { self.rotation *= rotation; } + /// Rotates this [`Transform`] around a point in local space. + /// If the point is a zero vector, this will rotate around the parent (if any) or the origin. + #[inline] + pub fn rotate_around(&mut self, point: Vec3, rotation: Quat) { + self.translation = point + rotation * (self.translation - point); + self.rotation *= rotation; + } + /// Multiplies `self` with `transform` component by component, returning the /// resulting [`Transform`] #[inline] @@ -235,8 +243,8 @@ impl Transform { self.scale *= scale_factor; } - /// Rotates this [`Transform`] so that its unit vector in the local z direction is toward - /// `target` and its unit vector in the local y direction is toward `up`. + /// Rotates this [`Transform`] so that its local z direction is toward + /// `target` and its local y direction is toward `up`. #[inline] pub fn look_at(&mut self, target: Vec3, up: Vec3) { let forward = Vec3::normalize(self.translation - target); @@ -244,6 +252,7 @@ impl Transform { let up = forward.cross(right); self.rotation = Quat::from_mat3(&Mat3::from_cols(right, up, forward)); } + } impl Default for Transform { From bc2c6033c295c462740b2711c17549e9fde0feac Mon Sep 17 00:00:00 2001 From: James Beilby Date: Thu, 11 Nov 2021 14:08:14 +0000 Subject: [PATCH 2/3] Added GlobalTransform::rotate_around for consistency --- crates/bevy_transform/src/components/global_transform.rs | 7 +++++++ crates/bevy_transform/src/components/transform.rs | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/crates/bevy_transform/src/components/global_transform.rs b/crates/bevy_transform/src/components/global_transform.rs index 930f23a48af99..d5451b1ec04de 100644 --- a/crates/bevy_transform/src/components/global_transform.rs +++ b/crates/bevy_transform/src/components/global_transform.rs @@ -193,6 +193,13 @@ impl GlobalTransform { self.rotation *= rotation; } + #[doc(hidden)] + #[inline] + pub fn rotate_around(&mut self, point: Vec3, rotation: Quat) { + self.translation = point + rotation * (self.translation - point); + self.rotation *= rotation; + } + /// Multiplies `self` with `transform` component by component, returning the /// resulting [`GlobalTransform`] #[inline] diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index cfab0766ea319..cf10c730a9fd7 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -205,7 +205,7 @@ impl Transform { self.rotation *= rotation; } - /// Rotates this [`Transform`] around a point in local space. + /// Rotates this [`Transform`] around a point in space. /// If the point is a zero vector, this will rotate around the parent (if any) or the origin. #[inline] pub fn rotate_around(&mut self, point: Vec3, rotation: Quat) { From 8ab523e87bd5850972fe2b5f8c6aa217a6661b7d Mon Sep 17 00:00:00 2001 From: James Beilby Date: Thu, 11 Nov 2021 14:28:25 +0000 Subject: [PATCH 3/3] Fixed whitespace for cargo fmt --- crates/bevy_transform/src/components/transform.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index cf10c730a9fd7..d456e4eba753e 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -252,7 +252,6 @@ impl Transform { let up = forward.cross(right); self.rotation = Quat::from_mat3(&Mat3::from_cols(right, up, forward)); } - } impl Default for Transform {