diff --git a/crates/bevy_math/src/direction.rs b/crates/bevy_math/src/direction.rs index 03cb9f969f1e6..509e497b43040 100644 --- a/crates/bevy_math/src/direction.rs +++ b/crates/bevy_math/src/direction.rs @@ -107,6 +107,8 @@ impl Dir2 { pub const NEG_Y: Self = Self(Vec2::NEG_Y); /// The directional axes. pub const AXES: [Self; 2] = [Self::X, Self::Y]; + /// The cardinal directions. + pub const CARDINALS: [Self; 4] = [Self::X, Self::NEG_X, Self::Y, Self::NEG_Y]; /// The "north" direction, equivalent to [`Dir2::Y`]. pub const NORTH: Self = Self(Vec2::Y); @@ -125,6 +127,25 @@ impl Dir2 { /// The "south-west" direction, between [`Dir2::SOUTH`] and [`Dir2::WEST`]. pub const SOUTH_WEST: Self = Self(Vec2::new(-FRAC_1_SQRT_2, -FRAC_1_SQRT_2)); + /// The diagonals between the cardinal directions. + pub const DIAGONALS: [Self; 4] = [ + Self::NORTH_EAST, + Self::NORTH_WEST, + Self::SOUTH_EAST, + Self::SOUTH_WEST, + ]; + /// All neighbors of a tile on a square grid in a 3x3 neighborhood. A combination of [`Self::CARDINALS`] and [`Self::DIAGONALS`] + pub const ALL_NEIGHBORS: [Self; 8] = [ + Self::X, + Self::NEG_X, + Self::Y, + Self::NEG_Y, + Self::NORTH_EAST, + Self::NORTH_WEST, + Self::SOUTH_EAST, + Self::SOUTH_WEST, + ]; + /// Create a direction from a finite, nonzero [`Vec2`], normalizing it. /// /// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length @@ -395,6 +416,143 @@ impl Dir3 { pub const NEG_Z: Self = Self(Vec3::NEG_Z); /// The directional axes. pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z]; + /// The cardinal directions. + pub const CARDINALS: [Self; 6] = [ + Self::X, + Self::NEG_X, + Self::Y, + Self::NEG_Y, + Self::Z, + Self::NEG_Z, + ]; + + // Adding this allow here to make sure that the precision in FRAC_1_SQRT_2 + // and here is the same + #[expect( + clippy::excessive_precision, + reason = "compatibility with the unstable rust definition" + )] + /// Approximation of 1/sqrt(3) needed for the diagonals in 3D space + const FRAC_1_SQRT_3: f32 = 0.577350269189625764509148780501957456_f32; + /// The directions pointing towards the vertices of a cube centered at the origin. + pub const ALL_VERTICES: [Self; 8] = [ + Self(Vec3::new( + Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + -Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + -Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + -Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + -Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + )), + ]; + /// The directions towards centers of each edge of a cube + pub const ALL_EDGES: [Self; 12] = [ + Self(Vec3::new(FRAC_1_SQRT_2, FRAC_1_SQRT_2, 0.)), + Self(Vec3::new(-FRAC_1_SQRT_2, FRAC_1_SQRT_2, 0.)), + Self(Vec3::new(FRAC_1_SQRT_2, -FRAC_1_SQRT_2, 0.)), + Self(Vec3::new(-FRAC_1_SQRT_2, -FRAC_1_SQRT_2, 0.)), + Self(Vec3::new(FRAC_1_SQRT_2, 0., FRAC_1_SQRT_2)), + Self(Vec3::new(-FRAC_1_SQRT_2, 0., FRAC_1_SQRT_2)), + Self(Vec3::new(FRAC_1_SQRT_2, 0., -FRAC_1_SQRT_2)), + Self(Vec3::new(-FRAC_1_SQRT_2, 0., -FRAC_1_SQRT_2)), + Self(Vec3::new(0., FRAC_1_SQRT_2, FRAC_1_SQRT_2)), + Self(Vec3::new(0., -FRAC_1_SQRT_2, FRAC_1_SQRT_2)), + Self(Vec3::new(0., FRAC_1_SQRT_2, -FRAC_1_SQRT_2)), + Self(Vec3::new(0., -FRAC_1_SQRT_2, -FRAC_1_SQRT_2)), + ]; + /// All neighbors of a tile on a cube grid a 3x3x3 neighborhood. A combination of [`Self::CARDINALS`], [`Self::ALL_EDGES`] and [`Self::ALL_VERTICES`] + pub const ALL_NEIGHBORS: [Self; 26] = [ + Self::X, + Self::NEG_X, + Self::Y, + Self::NEG_Y, + Self::Z, + Self::NEG_Z, + Self(Vec3::new(FRAC_1_SQRT_2, FRAC_1_SQRT_2, 0.)), + Self(Vec3::new(-FRAC_1_SQRT_2, FRAC_1_SQRT_2, 0.)), + Self(Vec3::new(FRAC_1_SQRT_2, -FRAC_1_SQRT_2, 0.)), + Self(Vec3::new(-FRAC_1_SQRT_2, -FRAC_1_SQRT_2, 0.)), + Self(Vec3::new(FRAC_1_SQRT_2, 0., FRAC_1_SQRT_2)), + Self(Vec3::new(-FRAC_1_SQRT_2, 0., FRAC_1_SQRT_2)), + Self(Vec3::new(FRAC_1_SQRT_2, 0., -FRAC_1_SQRT_2)), + Self(Vec3::new(-FRAC_1_SQRT_2, 0., -FRAC_1_SQRT_2)), + Self(Vec3::new(0., FRAC_1_SQRT_2, FRAC_1_SQRT_2)), + Self(Vec3::new(0., -FRAC_1_SQRT_2, FRAC_1_SQRT_2)), + Self(Vec3::new(0., FRAC_1_SQRT_2, -FRAC_1_SQRT_2)), + Self(Vec3::new(0., -FRAC_1_SQRT_2, -FRAC_1_SQRT_2)), + Self(Vec3::new( + Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + -Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + -Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + -Self::FRAC_1_SQRT_3, + Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + )), + Self(Vec3::new( + -Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + -Self::FRAC_1_SQRT_3, + )), + ]; /// Create a direction from a finite, nonzero [`Vec3`], normalizing it. ///