From 2c5439b25ec149307aeea87d23f0c91bf5100372 Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Tue, 2 Jan 2024 19:00:23 +0200 Subject: [PATCH] Add constants for `Direction2d` and `Direction3d` (#11180) # Objective I often need a direction along one of the cartesian XYZ axes, and it currently requires e.g. `Direction2d::from_normalized(Vec2::X)`, which isn't ideal. ## Solution Add direction constants that are the same as the ones on Glam types. I also copied the doc comment format "A unit vector pointing along the ... axis", but I can change it if there's a better wording for directions. --- crates/bevy_math/src/primitives/dim2.rs | 14 ++++++++++---- crates/bevy_math/src/primitives/dim3.rs | 18 ++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index 8b34a100eb..b2c664377c 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -7,6 +7,15 @@ use crate::Vec2; pub struct Direction2d(Vec2); impl Direction2d { + /// A unit vector pointing along the positive X axis. + pub const X: Self = Self(Vec2::X); + /// A unit vector pointing along the positive Y axis. + pub const Y: Self = Self(Vec2::Y); + /// A unit vector pointing along the negative X axis. + pub const NEG_X: Self = Self(Vec2::NEG_X); + /// A unit vector pointing along the negative Y axis. + pub const NEG_Y: Self = Self(Vec2::NEG_Y); + /// Create a direction from a finite, nonzero [`Vec2`]. /// /// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length @@ -378,10 +387,7 @@ mod tests { #[test] fn direction_creation() { - assert_eq!( - Direction2d::new(Vec2::X * 12.5), - Ok(Direction2d::from_normalized(Vec2::X)) - ); + assert_eq!(Direction2d::new(Vec2::X * 12.5), Ok(Direction2d::X)); assert_eq!( Direction2d::new(Vec2::new(0.0, 0.0)), Err(InvalidDirectionError::Zero) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 93b92fbc6c..2f490e77de 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -7,6 +7,19 @@ use crate::Vec3; pub struct Direction3d(Vec3); impl Direction3d { + /// A unit vector pointing along the positive X axis. + pub const X: Self = Self(Vec3::X); + /// A unit vector pointing along the positive Y axis. + pub const Y: Self = Self(Vec3::Y); + /// A unit vector pointing along the positive Z axis. + pub const Z: Self = Self(Vec3::Z); + /// A unit vector pointing along the negative X axis. + pub const NEG_X: Self = Self(Vec3::NEG_X); + /// A unit vector pointing along the negative Y axis. + pub const NEG_Y: Self = Self(Vec3::NEG_Y); + /// A unit vector pointing along the negative Z axis. + pub const NEG_Z: Self = Self(Vec3::NEG_Z); + /// Create a direction from a finite, nonzero [`Vec3`]. /// /// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length @@ -391,10 +404,7 @@ mod test { #[test] fn direction_creation() { - assert_eq!( - Direction3d::new(Vec3::X * 12.5), - Ok(Direction3d::from_normalized(Vec3::X)) - ); + assert_eq!(Direction3d::new(Vec3::X * 12.5), Ok(Direction3d::X)); assert_eq!( Direction3d::new(Vec3::new(0.0, 0.0, 0.0)), Err(InvalidDirectionError::Zero)