diff --git a/crates/bevy_math/src/primitives/dim2.rs b/crates/bevy_math/src/primitives/dim2.rs index bed4198538b82..c09aae1541f9e 100644 --- a/crates/bevy_math/src/primitives/dim2.rs +++ b/crates/bevy_math/src/primitives/dim2.rs @@ -27,7 +27,15 @@ impl Direction2d { ) } - /// Create a direction from a [`Vec2`] that is already normalized + /// Create a direction from its `x` and `y` components. + /// + /// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length + /// of the vector formed by the components is zero (or very close to zero), infinite, or `NaN`. + pub fn from_xy(x: f32, y: f32) -> Result { + Self::new(Vec2::new(x, y)) + } + + /// Create a direction from a [`Vec2`] that is already normalized. pub fn from_normalized(value: Vec2) -> Self { debug_assert!(value.is_normalized()); Self(value) diff --git a/crates/bevy_math/src/primitives/dim3.rs b/crates/bevy_math/src/primitives/dim3.rs index 66b3e38301259..5b3453a0d3f38 100644 --- a/crates/bevy_math/src/primitives/dim3.rs +++ b/crates/bevy_math/src/primitives/dim3.rs @@ -27,7 +27,15 @@ impl Direction3d { ) } - /// Create a direction from a [`Vec3`] that is already normalized + /// Create a direction from its `x`, `y`, and `z` components. + /// + /// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length + /// of the vector formed by the components is zero (or very close to zero), infinite, or `NaN`. + pub fn from_xyz(x: f32, y: f32, z: f32) -> Result { + Self::new(Vec3::new(x, y, z)) + } + + /// Create a direction from a [`Vec3`] that is already normalized. pub fn from_normalized(value: Vec3) -> Self { debug_assert!(value.is_normalized()); Self(value)