use glam::{Quat, Vec3};
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LookAngles {
pub yaw: f32,
pub pitch: f32,
}
impl LookAngles {
pub const FORWARD: Self = Self::new(0.0, 0.0);
pub const fn new(yaw: f32, pitch: f32) -> Self {
Self { yaw, pitch }
}
pub fn from_orientation(orientation: Quat) -> Self {
Self::from_direction(-(orientation * Vec3::Z))
}
pub fn from_direction(direction: Vec3) -> Self {
let Some(d) = direction.try_normalize() else {
return Self::FORWARD;
};
Self {
yaw: d.x.atan2(d.y),
pitch: d.z.clamp(-1.0, 1.0).asin(),
}
}
pub fn orientation(self) -> Quat {
(Quat::from_rotation_z(-self.yaw)
* Quat::from_rotation_x(std::f32::consts::FRAC_PI_2 + self.pitch))
.normalize()
}
pub fn direction(self) -> Vec3 {
let (sy, cy) = self.yaw.sin_cos();
let (sp, cp) = self.pitch.sin_cos();
Vec3::new(sy * cp, cy * cp, sp)
}
pub fn forward(self) -> Vec3 {
let (s, c) = self.yaw.sin_cos();
Vec3::new(s, c, 0.0)
}
pub fn right(self) -> Vec3 {
let (s, c) = self.yaw.sin_cos();
Vec3::new(c, -s, 0.0)
}
pub fn turned(self, yaw: f32, pitch: f32) -> Self {
Self {
yaw: self.yaw + yaw,
pitch: self.pitch + pitch,
}
}
pub fn with_pitch_clamped(self, min: f32, max: f32) -> Self {
Self {
yaw: self.yaw,
pitch: self.pitch.clamp(min, max),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::f32::consts::FRAC_PI_2;
fn close(a: Vec3, b: Vec3) -> bool {
(a - b).length() < 1e-4
}
#[test]
fn forward_looks_along_plus_y() {
let look = LookAngles::FORWARD;
assert!(close(look.direction(), Vec3::Y));
assert!(close(-(look.orientation() * Vec3::Z), Vec3::Y));
}
#[test]
fn yaw_turns_toward_plus_x() {
let look = LookAngles::new(FRAC_PI_2, 0.0);
assert!(close(look.direction(), Vec3::X));
assert!(close(look.forward(), Vec3::X));
assert!(close(look.right(), -Vec3::Y));
}
#[test]
fn positive_pitch_looks_up() {
assert!(LookAngles::new(0.0, 0.5).direction().z > 0.0);
assert!(LookAngles::new(0.0, -0.5).direction().z < 0.0);
}
#[test]
fn direction_matches_orientation() {
for &(yaw, pitch) in &[(0.0, 0.0), (0.7, 0.3), (-2.1, -0.9), (3.0, 1.2)] {
let look = LookAngles::new(yaw, pitch);
let from_quat = -(look.orientation() * Vec3::Z);
assert!(close(look.direction(), from_quat), "{look:?}");
}
}
#[test]
fn orientation_round_trips() {
for &(yaw, pitch) in &[(0.0, 0.0), (0.7, 0.3), (-2.1, -0.9), (3.0, 1.2)] {
let look = LookAngles::new(yaw, pitch);
let back = LookAngles::from_orientation(look.orientation());
assert!(close(back.direction(), look.direction()), "{look:?}");
assert!((back.pitch - pitch).abs() < 1e-4, "{look:?}");
}
}
#[test]
fn right_is_forward_cross_up() {
let look = LookAngles::new(0.9, 0.4);
assert!(close(look.right(), look.forward().cross(Vec3::Z)));
}
#[test]
fn camera_right_matches_look_right_when_level() {
let look = LookAngles::new(1.3, 0.0);
assert!(close(look.orientation() * Vec3::X, look.right()));
}
#[test]
fn zero_direction_is_forward() {
assert_eq!(LookAngles::from_direction(Vec3::ZERO), LookAngles::FORWARD);
}
#[test]
fn clamp_holds_pitch_only() {
let look = LookAngles::new(1.0, 2.0).with_pitch_clamped(-1.0, 1.0);
assert_eq!(look, LookAngles::new(1.0, 1.0));
}
}