use super::*;
const SENSITIVITY: f32 = 0.01;
#[derive(Clone, Debug)]
pub struct FirstPersonCamera {
pub position: Vec3f,
pub forward: Vec3f,
pub up: Vec3f,
}
impl FirstPersonCamera {
pub fn new(position: Vec3f, forward: Vec3f, up: Vec3f) -> FirstPersonCamera {
FirstPersonCamera { position, forward, up }
}
pub fn r#move(&mut self, dir: Vec3f) {
let right = self.forward.cross(self.up).norm();
self.position += self.forward * dir.x + right * dir.y + self.up * dir.z;
}
pub fn look_at(&mut self, target: Vec3f) {
self.forward = (target - self.position).norm();
}
pub fn mouse(&mut self, dx: f32, dy: f32) {
let yaw = Angle(dx * SENSITIVITY);
let pitch = Angle(dy * SENSITIVITY);
let forward = self.forward;
let right = forward.cross(self.up).norm();
let rotation = Mat3f::rotation(self.up, yaw) * Mat3f::rotation(right, pitch);
self.forward = (rotation * forward).norm();
}
#[inline]
pub fn position(&self) -> Vec3f {
self.position
}
#[inline]
pub fn view_dir(&self) -> Vec3f {
self.forward
}
pub fn view_matrix(&self, hand: Hand) -> Transform3f {
Transform3f::look_at(self.position, self.position + self.forward, self.up, hand)
}
}