pub trait Orientation: Sized + Debug + From<Rotation> + Into<Rotation> + Copy {
fn distance(&self, other: Self) -> Rotation;
fn assert_approx_eq(self, other: impl Orientation) { ... }
fn rotation_direction(&self, target: Self) -> RotationDirection { ... }
fn rotate_towards(
&mut self,
target_orientation: Self,
max_rotation: Option<Rotation>
) { ... }
}
Expand description
A type that can represent a orientation in 2D space
Required Methods
Returns the absolute distance between self
and other
as a Rotation
The shortest path will always be taken, and so this value ranges between 0 and 180 degrees. Simply subtract the two rotations if you want a signed value instead.
Example
use leafwing_input_manager::orientation::{Orientation, Direction, Rotation};
Direction::NORTH.distance(Direction::SOUTHWEST).assert_approx_eq(Rotation::from_degrees(135.));
Provided Methods
fn assert_approx_eq(self, other: impl Orientation)
fn assert_approx_eq(self, other: impl Orientation)
Asserts that self
is approximately equal to other
Panics
Panics if the distance between self
and other
is greater than 2 deci-degrees.
fn rotation_direction(&self, target: Self) -> RotationDirection
fn rotation_direction(&self, target: Self) -> RotationDirection
Which RotationDirection
is the shortest to rotate towards to reach target
?
In the case of ties, RotationDirection::Clockwise
will be returned.
Example
use leafwing_input_manager::orientation::{Direction, Orientation, RotationDirection};
assert_eq!(Direction::NORTH.rotation_direction(Direction::NORTH), RotationDirection::Clockwise);
assert_eq!(Direction::NORTH.rotation_direction(Direction::SOUTH), RotationDirection::Clockwise);
assert_eq!(Direction::NORTH.rotation_direction(Direction::EAST), RotationDirection::Clockwise);
assert_eq!(Direction::NORTH.rotation_direction(Direction::WEST), RotationDirection::CounterClockwise);
assert_eq!(Direction::WEST.rotation_direction(Direction::SOUTH), RotationDirection::CounterClockwise);
assert_eq!(Direction::SOUTH.rotation_direction(Direction::WEST), RotationDirection::Clockwise);
fn rotate_towards(
&mut self,
target_orientation: Self,
max_rotation: Option<Rotation>
)
fn rotate_towards(
&mut self,
target_orientation: Self,
max_rotation: Option<Rotation>
)
Rotates self
towards target_orientation
by up to max_rotation
Example
use leafwing_input_manager::orientation::{Rotation, Orientation};
let mut rotation = Rotation::SOUTH;
// Without a `max_rotation`, the orientation snaps
rotation.rotate_towards(Rotation::WEST, None);
assert_eq!(rotation, Rotation::WEST);
// With a `max_rotation`, we don't get all the way there
rotation.rotate_towards(Rotation::SOUTH, Some(Rotation::new(450)));
assert_eq!(rotation, Rotation::SOUTHWEST);