use glam::Vec3;
use crate::input::action_frame::ActionFrame;
use crate::{Camera, LookAngles};
use viewport_lib_types::camera::Pose;
pub struct FirstPersonCameraController {
pub yaw: f32,
pub pitch: f32,
pub sensitivity: f32,
pub pitch_clamp: f32,
}
impl Default for FirstPersonCameraController {
fn default() -> Self {
Self::new(Self::DEFAULT_SENSITIVITY, Self::DEFAULT_PITCH_CLAMP)
}
}
impl FirstPersonCameraController {
pub const DEFAULT_SENSITIVITY: f32 = 0.005;
pub const DEFAULT_PITCH_CLAMP: f32 = 1.4;
pub fn new(sensitivity: f32, pitch_clamp: f32) -> Self {
Self {
yaw: 0.0,
pitch: 0.0,
sensitivity,
pitch_clamp: pitch_clamp.abs(),
}
}
pub fn with_sensitivity(mut self, sensitivity: f32) -> Self {
self.sensitivity = sensitivity;
self
}
pub fn with_pitch_clamp(mut self, pitch_clamp: f32) -> Self {
self.pitch_clamp = pitch_clamp.abs();
self.pitch = self.pitch.clamp(-self.pitch_clamp, self.pitch_clamp);
self
}
pub fn apply(
&mut self,
camera: &mut Camera,
frame: &ActionFrame,
eye_position: impl Into<glam::DVec3>,
) {
let look = frame.navigation.orbit;
self.turn_by(look.x * self.sensitivity, -look.y * self.sensitivity);
camera.set_pose(Pose::new(
eye_position,
self.look().orientation().as_dquat(),
));
}
pub fn look(&self) -> LookAngles {
LookAngles::new(self.yaw, self.pitch)
}
pub fn turn_by(&mut self, yaw: f32, pitch: f32) {
self.set_look(self.yaw + yaw, self.pitch + pitch);
}
pub fn sync_from_camera(&mut self, camera: &Camera) {
let look = LookAngles::from_orientation(camera.orientation());
self.set_look(look.yaw, look.pitch);
}
pub fn set_look(&mut self, yaw: f32, pitch: f32) {
self.yaw = yaw;
self.pitch = pitch.clamp(-self.pitch_clamp, self.pitch_clamp);
}
pub fn forward_dir(&self) -> Vec3 {
self.look().forward()
}
pub fn right_dir(&self) -> Vec3 {
self.look().right()
}
pub fn aim_dir(&self) -> Vec3 {
self.look().direction()
}
}
#[cfg(test)]
mod tests {
use super::*;
use glam::Vec2;
fn cam() -> FirstPersonCameraController {
FirstPersonCameraController::new(0.002, 1.4)
}
fn look(x: f32, y: f32) -> ActionFrame {
let mut f = ActionFrame::default();
f.navigation.orbit = Vec2::new(x, y);
f
}
fn approx_eq(a: Vec3, b: Vec3) -> bool {
(a - b).length() < 1e-4
}
#[test]
fn default_aim_is_forward_along_y() {
assert!(approx_eq(cam().aim_dir(), Vec3::Y));
}
#[test]
fn right_look_increases_yaw() {
let mut c = cam();
let mut camera = Camera::default();
c.apply(&mut camera, &look(100.0, 0.0), Vec3::ZERO);
assert!(c.yaw > 0.0, "look right should increase yaw");
}
#[test]
fn down_look_decreases_pitch() {
let mut c = cam();
let mut camera = Camera::default();
c.apply(&mut camera, &look(0.0, 100.0), Vec3::ZERO);
assert!(c.pitch < 0.0, "look down should decrease pitch");
}
#[test]
fn pitch_is_clamped() {
let mut c = FirstPersonCameraController::new(1.0, 1.0);
let mut camera = Camera::default();
c.apply(&mut camera, &look(0.0, -9999.0), Vec3::ZERO);
assert!(c.pitch <= 1.0 && c.pitch >= -1.0, "pitch={}", c.pitch);
}
#[test]
fn eye_attaches_to_supplied_position() {
let mut c = cam();
let mut camera = Camera::default();
let eye = Vec3::new(3.0, 4.0, 1.8);
c.apply(&mut camera, &look(0.0, 0.0), eye);
assert!((camera.eye().as_vec3() - eye).length() < 1e-4);
}
#[test]
fn forward_dir_is_horizontal_and_rotates() {
let mut c = cam();
c.set_look(std::f32::consts::FRAC_PI_2, 0.0);
let fwd = c.forward_dir();
assert!(fwd.z.abs() < 1e-6, "forward must be horizontal");
assert!(approx_eq(fwd, Vec3::X), "forward at yaw=PI/2 should be +X");
}
#[test]
fn sync_from_camera_round_trips() {
let mut c = cam();
let mut camera = Camera::default();
c.set_look(0.7, 0.3);
c.apply(&mut camera, &look(0.0, 0.0), Vec3::ZERO);
let mut c2 = cam();
c2.sync_from_camera(&camera);
assert!((c2.yaw - 0.7).abs() < 1e-4, "yaw={}", c2.yaw);
assert!((c2.pitch - 0.3).abs() < 1e-4, "pitch={}", c2.pitch);
}
#[test]
fn turn_by_clamps_pitch_and_accumulates_yaw() {
let mut c = FirstPersonCameraController::default();
c.turn_by(0.3, 0.0);
c.turn_by(0.2, 5.0);
assert!((c.yaw - 0.5).abs() < 1e-6);
assert!((c.pitch - FirstPersonCameraController::DEFAULT_PITCH_CLAMP).abs() < 1e-6);
}
#[test]
fn turn_by_matches_an_equal_drag() {
let mut dragged = cam();
let mut turned = cam();
let mut camera = Camera::default();
dragged.apply(&mut camera, &look(40.0, -25.0), Vec3::ZERO);
turned.turn_by(40.0 * turned.sensitivity, 25.0 * turned.sensitivity);
assert!((dragged.yaw - turned.yaw).abs() < 1e-6);
assert!((dragged.pitch - turned.pitch).abs() < 1e-6);
}
}