This commit is contained in:
aiden 2023-05-15 09:28:40 +01:00
parent bfd5914b6b
commit 9e9752cc38
Signed by: aiden
GPG Key ID: EFA9C74AEBF806E0
2 changed files with 20 additions and 18 deletions

@ -1,15 +1,15 @@
use cgmath::{Vector3, Point3, Rad, InnerSpace, Deg};
use cgmath::{Vector3, Point3, InnerSpace, Deg, Rad};
use crate::Input;
#[derive(Debug)]
pub struct Camera {
pub position: Point3<f32>,
pub rot_x: Rad<f32>,
pub rot_y: Rad<f32>,
pub rot_x: Deg<f32>,
pub rot_y: Deg<f32>,
aspect: f32,
fovy: Rad<f32>,
fovy: Deg<f32>,
znear: f32,
zfar: f32,
}
@ -17,8 +17,8 @@ pub struct Camera {
impl Camera {
pub fn new<
V: Into<Point3<f32>>,
Y: Into<Rad<f32>>,
P: Into<Rad<f32>>,
Y: Into<Deg<f32>>,
P: Into<Deg<f32>>,
>(
dimensions: winit::dpi::PhysicalSize<u32>,
@ -32,7 +32,7 @@ impl Camera {
rot_y: rot_y.into(),
aspect: dimensions.width as f32 / dimensions.height as f32,
fovy: Deg(45.0).into(),
fovy: Deg(45.0),
znear: 0.1,
zfar: 100.0,
}
@ -45,7 +45,7 @@ impl Camera {
}
pub fn update(&mut self, input: &mut Input, dt: f32) {
let (yaw_sin, yaw_cos) = self.rot_x.0.sin_cos();
let (yaw_sin, yaw_cos) = Rad::from(self.rot_x).0.sin_cos();
let forward = Vector3::new(yaw_cos, 0.0, yaw_sin).normalize();
let right = Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize();
self.position += forward * (input.amount_forward - input.amount_backward) * (input.speed * dt);
@ -56,12 +56,10 @@ impl Camera {
let (dx, dy) = input.mouse_moved;
input.mouse_moved = (0.0, 0.0);
let dt = 1.0 / (1920.0 / 360.0);
self.rot_x += Deg(dx * input.sens * dt).into();
let pitch = Deg::from(self.rot_y) + Deg(-dy * input.sens * dt);
let frac = std::f32::consts::FRAC_PI_2 - 0.0001;
self.rot_y = Rad(Rad::from(pitch).0.clamp(-Rad(frac).0, Rad(frac).0));
self.rot_x += Deg(dx * input.dots_multiplier / input.dots_per_degree);
let pitch_lim = 90.0 - 0.0001;
let pitch = self.rot_y.0 + (-dy * input.dots_multiplier / input.dots_per_degree);
self.rot_y = Deg(pitch.clamp(-pitch_lim, pitch_lim));
}
}
@ -90,8 +88,8 @@ impl<'a> CameraUniform {
}
}
pub fn set_view_projection_matrix(&self, queue: &wgpu::Queue, camera: &Camera) {
let (sin_yaw, cos_yaw) = camera.rot_x.0.sin_cos();
let (sin_pitch, cos_pitch) = camera.rot_y.0.sin_cos();
let (sin_yaw, cos_yaw) = Rad::from(camera.rot_x).0.sin_cos();
let (sin_pitch, cos_pitch) = Rad::from(camera.rot_y).0.sin_cos();
let target = Vector3::new(
cos_pitch * cos_yaw,

@ -21,7 +21,8 @@ pub struct Input {
amount_down: f32,
mouse_moved: (f32, f32),
speed: f32,
sens: f32,
dots_multiplier: f32,
dots_per_degree: f32,
}
impl Input {
@ -35,7 +36,10 @@ impl Input {
amount_down: 0.0,
mouse_moved: (0.0, 0.0),
speed,
sens,
dots_multiplier: sens,
// i don't want the sensitivity to be tied to the resolution, though.
dots_per_degree: 1920.0 / 360.0,
}
}