diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 459c8e26ee9ea..c29c696108d8d 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -71,6 +71,12 @@ impl TryFrom for Direction3d { } } +impl From for Vec3 { + fn from(value: Direction3d) -> Self { + value.0 + } +} + impl std::ops::Deref for Direction3d { type Target = Vec3; fn deref(&self) -> &Self::Target { @@ -85,6 +91,13 @@ impl std::ops::Neg for Direction3d { } } +impl std::ops::Mul for Direction3d { + type Output = Vec3; + fn mul(self, rhs: f32) -> Self::Output { + self.0 * rhs + } +} + impl std::ops::Mul for Quat { type Output = Direction3d; diff --git a/crates/bevy_transform/src/components/transform.rs b/crates/bevy_transform/src/components/transform.rs index 316ee97719d7e..46dcceb68e5cf 100644 --- a/crates/bevy_transform/src/components/transform.rs +++ b/crates/bevy_transform/src/components/transform.rs @@ -1,5 +1,6 @@ use super::GlobalTransform; use bevy_ecs::{component::Component, reflect::ReflectComponent}; +use bevy_math::primitives::Direction3d; use bevy_math::{Affine3A, Mat3, Mat4, Quat, Vec3}; use bevy_reflect::prelude::*; use bevy_reflect::Reflect; @@ -185,55 +186,58 @@ impl Transform { /// Get the unit vector in the local `X` direction. #[inline] - pub fn local_x(&self) -> Vec3 { - self.rotation * Vec3::X + pub fn local_x(&self) -> Direction3d { + // Direction3d::new(x) panics if x is of invalid length, but quat * unit vector is length 1 + Direction3d::new(self.rotation * Vec3::X).unwrap() } /// Equivalent to [`-local_x()`][Transform::local_x()] #[inline] - pub fn left(&self) -> Vec3 { + pub fn left(&self) -> Direction3d { -self.local_x() } /// Equivalent to [`local_x()`][Transform::local_x()] #[inline] - pub fn right(&self) -> Vec3 { + pub fn right(&self) -> Direction3d { self.local_x() } /// Get the unit vector in the local `Y` direction. #[inline] - pub fn local_y(&self) -> Vec3 { - self.rotation * Vec3::Y + pub fn local_y(&self) -> Direction3d { + // Direction3d::new(x) panics if x is of invalid length, but quat * unit vector is length 1 + Direction3d::new(self.rotation * Vec3::Y).unwrap() } /// Equivalent to [`local_y()`][Transform::local_y] #[inline] - pub fn up(&self) -> Vec3 { + pub fn up(&self) -> Direction3d { self.local_y() } /// Equivalent to [`-local_y()`][Transform::local_y] #[inline] - pub fn down(&self) -> Vec3 { + pub fn down(&self) -> Direction3d { -self.local_y() } /// Get the unit vector in the local `Z` direction. #[inline] - pub fn local_z(&self) -> Vec3 { - self.rotation * Vec3::Z + pub fn local_z(&self) -> Direction3d { + // Direction3d::new(x) panics if x is of invalid length, but quat * unit vector is length 1 + Direction3d::new(self.rotation * Vec3::Z).unwrap() } /// Equivalent to [`-local_z()`][Transform::local_z] #[inline] - pub fn forward(&self) -> Vec3 { + pub fn forward(&self) -> Direction3d { -self.local_z() } /// Equivalent to [`local_z()`][Transform::local_z] #[inline] - pub fn back(&self) -> Vec3 { + pub fn back(&self) -> Direction3d { self.local_z() } diff --git a/examples/3d/tonemapping.rs b/examples/3d/tonemapping.rs index edeb97e0332e0..db4d4a9e23067 100644 --- a/examples/3d/tonemapping.rs +++ b/examples/3d/tonemapping.rs @@ -224,7 +224,7 @@ fn setup_color_gradient_scene( camera_transform: Res, ) { let mut transform = camera_transform.0; - transform.translation += transform.forward(); + transform.translation += *transform.forward(); commands.spawn(( MaterialMeshBundle { @@ -248,7 +248,7 @@ fn setup_image_viewer_scene( camera_transform: Res, ) { let mut transform = camera_transform.0; - transform.translation += transform.forward(); + transform.translation += *transform.forward(); // exr/hdr viewer (exr requires enabling bevy feature) commands.spawn(( diff --git a/examples/helpers/camera_controller.rs b/examples/helpers/camera_controller.rs index 82e1ff5c3f1f6..43ea542474761 100644 --- a/examples/helpers/camera_controller.rs +++ b/examples/helpers/camera_controller.rs @@ -171,8 +171,8 @@ fn run_camera_controller( controller.velocity = Vec3::ZERO; } } - let forward = transform.forward(); - let right = transform.right(); + let forward = *transform.forward(); + let right = *transform.right(); transform.translation += controller.velocity.x * dt * right + controller.velocity.y * dt * Vec3::Y + controller.velocity.z * dt * forward; diff --git a/examples/transforms/transform.rs b/examples/transforms/transform.rs index 48152662820e5..6e99a1ec0bddc 100644 --- a/examples/transforms/transform.rs +++ b/examples/transforms/transform.rs @@ -127,7 +127,7 @@ fn rotate_cube( // Update the rotation of the cube(s). for (mut transform, cube) in &mut cubes { // Calculate the rotation of the cube if it would be looking at the sphere in the center. - let look_at_sphere = transform.looking_at(center, transform.local_y()); + let look_at_sphere = transform.looking_at(center, *transform.local_y()); // Interpolate between the current rotation and the fully turned rotation // when looking a the sphere, with a given turn speed to get a smooth motion. // With higher speed the curvature of the orbit would be smaller.