Add compass direction constants to Dir2 (#13636)

# Objective

When working on `leafwing-input-manager` and in my games, I've found
these compass directions to be both clear and useful when attempting to
describe angles in 2 dimensions.

This was directly used when mapping gamepad inputs into 4-way movement
as a virtual dpad, and I expect other uses are common in games.

## Solution

- Add constants corresponding to the 4 cardinal and 4 semi-cardinal
directions.

## Testing

- I've validated the quadrants of each of the directions through
self-review.

---------

Co-authored-by: Alice Cecile <alice.i.cecil@gmail.com>
This commit is contained in:
Alice Cecile 2024-06-03 11:14:13 -04:00 committed by GitHub
parent 5c74c17c24
commit cca4fc76de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 2 deletions

View file

@ -3,6 +3,8 @@ use crate::{
Quat, Rotation2d, Vec2, Vec3, Vec3A,
};
use core::f32::consts::FRAC_1_SQRT_2;
#[cfg(feature = "bevy_reflect")]
use bevy_reflect::Reflect;
#[cfg(all(feature = "serialize", feature = "bevy_reflect"))]
@ -105,6 +107,23 @@ impl Dir2 {
/// The directional axes.
pub const AXES: [Self; 2] = [Self::X, Self::Y];
/// The "north" direction, equivalent to [`Dir2::Y`].
pub const NORTH: Self = Self(Vec2::Y);
/// The "south" direction, equivalent to [`Dir2::NEG_Y`].
pub const SOUTH: Self = Self(Vec2::NEG_Y);
/// The "east" direction, equivalent to [`Dir2::X`].
pub const EAST: Self = Self(Vec2::X);
/// The "west" direction, equivalent to [`Dir2::NEG_X`].
pub const WEST: Self = Self(Vec2::NEG_X);
/// The "north-east" direction, between [`Dir2::NORTH`] and [`Dir2::EAST`].
pub const NORTH_EAST: Self = Self(Vec2::new(FRAC_1_SQRT_2, FRAC_1_SQRT_2));
/// The "north-west" direction, between [`Dir2::NORTH`] and [`Dir2::WEST`].
pub const NORTH_WEST: Self = Self(Vec2::new(-FRAC_1_SQRT_2, FRAC_1_SQRT_2));
/// The "south-east" direction, between [`Dir2::SOUTH`] and [`Dir2::EAST`].
pub const SOUTH_EAST: Self = Self(Vec2::new(FRAC_1_SQRT_2, -FRAC_1_SQRT_2));
/// 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));
/// Create a direction from a finite, nonzero [`Vec2`], normalizing it.
///
/// Returns [`Err(InvalidDirectionError)`](InvalidDirectionError) if the length

View file

@ -81,8 +81,8 @@ impl Rotation2d {
/// A counterclockwise rotation of π/4 radians.
pub const FRAC_PI_4: Self = Self {
cos: std::f32::consts::FRAC_1_SQRT_2,
sin: std::f32::consts::FRAC_1_SQRT_2,
cos: core::f32::consts::FRAC_1_SQRT_2,
sin: core::f32::consts::FRAC_1_SQRT_2,
};
/// A counterclockwise rotation of π/6 radians.