use core::f32::consts::{FRAC_PI_2, PI};
use crcbl_core::input::KeyCode;
use glam::{Vec2, Vec3};
use crate::camera::{Camera, Projection};
pub const SPEED: f32 = 2.4;
pub const TURN: f32 = 1.6;
pub const LOOK: f32 = 0.1 * (PI / 180.0);
const PITCH_LIMIT: f32 = FRAC_PI_2 - 0.05;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
struct Held {
forward: bool,
back: bool,
left: bool,
right: bool,
up: bool,
down: bool,
turn_left: bool,
turn_right: bool,
look_up: bool,
look_down: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Flyer {
eye: Vec3,
yaw: f32,
pitch: f32,
speed: f32,
held: Held,
moved: bool,
}
impl Flyer {
#[must_use]
pub fn at(camera: &Camera) -> Self {
let forward = (camera.target - camera.eye).normalize_or_zero();
Self {
eye: camera.eye,
yaw: forward.x.atan2(-forward.z),
pitch: forward.y.clamp(-1.0, 1.0).asin(),
speed: SPEED,
held: Held::default(),
moved: false,
}
}
#[must_use]
pub const fn with_speed(mut self, speed: f32) -> Self {
self.speed = speed;
self
}
#[must_use]
pub const fn speed(&self) -> f32 {
self.speed
}
#[must_use]
pub const fn has_moved(&self) -> bool {
self.moved
}
#[must_use]
pub const fn eye(&self) -> Vec3 {
self.eye
}
pub const fn key(&mut self, key: KeyCode, pressed: bool) -> bool {
let held = &mut self.held;
let slot = match key {
KeyCode::KeyW => &mut held.forward,
KeyCode::KeyS => &mut held.back,
KeyCode::KeyA => &mut held.left,
KeyCode::KeyD => &mut held.right,
KeyCode::Space => &mut held.up,
KeyCode::ShiftLeft | KeyCode::ShiftRight => &mut held.down,
KeyCode::ArrowLeft => &mut held.turn_left,
KeyCode::ArrowRight => &mut held.turn_right,
KeyCode::ArrowUp => &mut held.look_up,
KeyCode::ArrowDown => &mut held.look_down,
_ => return false,
};
*slot = pressed;
true
}
pub fn release_all(&mut self) {
self.held = Held::default();
}
pub fn look(&mut self, motion: Vec2) {
if motion == Vec2::ZERO {
return;
}
self.yaw += motion.x * LOOK;
self.pitch = (self.pitch - motion.y * LOOK).clamp(-PITCH_LIMIT, PITCH_LIMIT);
self.moved = true;
}
pub fn advance(&mut self, dt: f32) {
let axis = |positive: bool, negative: bool| f32::from(positive) - f32::from(negative);
let turn = axis(self.held.turn_right, self.held.turn_left) * TURN * dt;
let tilt = axis(self.held.look_up, self.held.look_down) * TURN * dt;
self.yaw += turn;
self.pitch = (self.pitch + tilt).clamp(-PITCH_LIMIT, PITCH_LIMIT);
let (sin_yaw, cos_yaw) = self.yaw.sin_cos();
let ahead = Vec3::new(sin_yaw, 0.0, -cos_yaw);
let aside = Vec3::new(cos_yaw, 0.0, sin_yaw);
let step = ahead * axis(self.held.forward, self.held.back)
+ aside * axis(self.held.right, self.held.left)
+ Vec3::Y * axis(self.held.up, self.held.down);
let step = step.normalize_or_zero() * self.speed * dt;
if step != Vec3::ZERO || turn != 0.0 || tilt != 0.0 {
self.moved = true;
}
self.eye += step;
}
#[must_use]
pub fn camera(&self, projection: Projection) -> Camera {
let (sin_yaw, cos_yaw) = self.yaw.sin_cos();
let (sin_pitch, cos_pitch) = self.pitch.sin_cos();
let forward = Vec3::new(sin_yaw * cos_pitch, sin_pitch, -cos_yaw * cos_pitch);
Camera {
eye: self.eye,
target: self.eye + forward,
up: Vec3::Y,
projection,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn fixture() -> Camera {
Camera {
eye: Vec3::new(1.5, 1.6, 4.0),
target: Vec3::new(-0.7, 0.9, -2.0),
up: Vec3::Y,
projection: Projection::default(),
}
}
#[test]
fn a_flyer_starts_looking_exactly_where_the_fixed_camera_looks() {
let fixed = fixture();
let flyer = Flyer::at(&fixed);
let rebuilt = flyer.camera(fixed.projection);
assert_eq!(rebuilt.eye, fixed.eye);
let want = (fixed.target - fixed.eye).normalize();
let got = (rebuilt.target - rebuilt.eye).normalize();
assert!(
got.dot(want) > 0.9999,
"the flyer looks {got:?} where the fixed camera looks {want:?}"
);
assert!(!flyer.has_moved(), "building one is not flying it");
}
#[test]
fn holding_forward_walks_and_releasing_it_stops() {
let fixed = fixture();
let mut flyer = Flyer::at(&fixed);
assert!(flyer.key(KeyCode::KeyW, true), "W is the camera's");
flyer.advance(0.5);
let walked = flyer.eye();
assert_ne!(walked, fixed.eye, "a held key did not move the camera");
assert!(flyer.has_moved());
flyer.key(KeyCode::KeyW, false);
flyer.advance(0.5);
assert_eq!(flyer.eye(), walked, "a released key kept walking");
flyer.key(KeyCode::KeyW, true);
flyer.release_all();
flyer.advance(0.5);
assert_eq!(
flyer.eye(),
walked,
"a key held across a focus loss kept walking"
);
}
#[test]
fn a_second_of_forward_walks_the_flyers_own_speed() {
let mut standard = Flyer::at(&fixture());
assert_eq!(standard.speed(), SPEED, "the default is the constant");
standard.key(KeyCode::KeyW, true);
standard.advance(1.0);
let walked = standard.eye().distance(fixture().eye);
assert!(
(walked - SPEED).abs() < 1e-4,
"a second of forward walked {walked} m at the default speed of {SPEED} m/s"
);
let quick = 20.0;
let mut fast = Flyer::at(&fixture()).with_speed(quick);
assert_eq!(fast.speed(), quick, "with_speed did not take");
fast.key(KeyCode::KeyW, true);
fast.advance(1.0);
let flown = fast.eye().distance(fixture().eye);
assert!(
(flown - quick).abs() < 1e-3,
"a second of forward walked {flown} m at {quick} m/s"
);
}
#[test]
fn walking_forward_does_not_follow_the_pitch_into_the_floor() {
let fixed = fixture();
let mut flyer = Flyer::at(&fixed);
flyer.key(KeyCode::ArrowDown, true);
flyer.advance(1.0);
flyer.key(KeyCode::ArrowDown, false);
let looking_down = flyer.eye();
flyer.key(KeyCode::KeyW, true);
flyer.advance(1.0);
assert!(
(flyer.eye().y - looking_down.y).abs() < 1e-5,
"walking while looking down changed the height: {:?} -> {:?}",
looking_down,
flyer.eye(),
);
flyer.key(KeyCode::KeyW, false);
flyer.key(KeyCode::Space, true);
flyer.advance(1.0);
assert!(flyer.eye().y > looking_down.y, "space did not rise");
}
#[test]
fn the_pitch_stops_short_of_straight_up_and_straight_down() {
for (key, sign) in [(KeyCode::ArrowUp, 1.0f32), (KeyCode::ArrowDown, -1.0)] {
let mut flyer = Flyer::at(&fixture());
flyer.key(key, true);
for _ in 0..200 {
flyer.advance(0.1);
}
let camera = flyer.camera(fixture().projection);
let forward = (camera.target - camera.eye).normalize();
assert!(
forward.y * sign > 0.9 && forward.y.abs() < 1.0,
"{key:?} reached {forward:?}, which is a degenerate view"
);
let level = Vec3::new(forward.x, 0.0, forward.z).length();
assert!(level > 0.01, "{key:?} left no horizontal component at all");
}
}
#[test]
fn the_turn_arrows_swing_the_view_toward_the_side_they_name() {
for (key, sign) in [(KeyCode::ArrowRight, 1.0f32), (KeyCode::ArrowLeft, -1.0)] {
let mut flyer = Flyer::at(&fixture());
let before = flyer.camera(fixture().projection);
let start = (before.target - before.eye).normalize();
let right = start.cross(Vec3::Y).normalize();
flyer.key(key, true);
flyer.advance(0.05);
let after = flyer.camera(fixture().projection);
let turned = (after.target - after.eye).normalize();
let leaned = turned.dot(right);
assert!(
leaned * sign > 0.01,
"{key:?} moved the view {leaned} of the way toward the camera's own right \
vector {right:?}, and the sign says it turned the other way"
);
assert!(
turned.dot(start) > 0.99,
"{key:?} swung {turned:?} from {start:?} in one step, which is too far for the \
sign above to mean what it says"
);
}
}
#[test]
fn the_mouse_swings_the_view_the_way_it_moved() {
let sweep = 50.0;
for (motion, toward_right, toward_up) in [
(Vec2::new(sweep, 0.0), 1.0f32, 0.0f32),
(Vec2::new(-sweep, 0.0), -1.0, 0.0),
(Vec2::new(0.0, -sweep), 0.0, 1.0),
(Vec2::new(0.0, sweep), 0.0, -1.0),
] {
let mut flyer = Flyer::at(&fixture());
let before = flyer.camera(fixture().projection);
let start = (before.target - before.eye).normalize();
let right = start.cross(Vec3::Y).normalize();
let up = right.cross(start).normalize();
flyer.look(motion);
let after = flyer.camera(fixture().projection);
let turned = (after.target - after.eye).normalize();
assert!(
turned.dot(right) * toward_right > 0.01 || toward_right == 0.0,
"{motion:?} leaned {} toward the camera's own right {right:?}",
turned.dot(right),
);
assert!(
turned.dot(up) * toward_up > 0.01 || toward_up == 0.0,
"{motion:?} leaned {} toward the camera's own up {up:?}",
turned.dot(up),
);
assert!(
turned.dot(start) > 0.99,
"{motion:?} swung {turned:?} from {start:?} in one step, which is too far for \
the signs above to mean what they say"
);
assert!(flyer.has_moved(), "a look is a movement");
}
let mut still = Flyer::at(&fixture());
still.look(Vec2::ZERO);
assert!(!still.has_moved());
}
#[test]
fn the_mouse_pitch_stops_short_of_vertical_too() {
for sign in [1.0f32, -1.0] {
let mut flyer = Flyer::at(&fixture());
flyer.look(Vec2::new(0.0, -sign * 100_000.0));
for _ in 0..200 {
flyer.look(Vec2::new(0.0, -sign * 50.0));
}
let camera = flyer.camera(fixture().projection);
let forward = (camera.target - camera.eye).normalize();
assert!(
forward.y * sign > 0.9 && forward.y.abs() < 1.0,
"the mouse reached {forward:?}, which is a degenerate view"
);
let level = Vec3::new(forward.x, 0.0, forward.z).length();
assert!(
level > 0.01,
"the mouse left no horizontal component at all"
);
}
}
#[test]
fn an_unbound_key_is_not_claimed() {
let mut flyer = Flyer::at(&fixture());
assert!(!flyer.key(KeyCode::KeyQ, true));
flyer.advance(1.0);
assert!(!flyer.has_moved());
}
}