mouse sensitivty is now specified in dots per (360)deg, camera rot is now interpolated

This commit is contained in:
aiden 2023-05-15 20:21:01 +01:00
parent 517bb3d743
commit c2c645ee39
Signed by: aiden
GPG Key ID: EFA9C74AEBF806E0
2 changed files with 20 additions and 30 deletions

@ -42,7 +42,7 @@ impl Camera {
self.aspect = dimensions.width as f32 / dimensions.height as f32; self.aspect = dimensions.width as f32 / dimensions.height as f32;
} }
pub fn update_pos(&mut self, input: &mut Input, dt: f32) { pub fn update_pos(&mut self, input: &Input, dt: f32) {
let (yaw_sin, yaw_cos) = Rad::from(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 forward = Vector3::new(yaw_cos, 0.0, yaw_sin).normalize();
let right = Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize(); let right = Vector3::new(-yaw_sin, 0.0, yaw_cos).normalize();
@ -52,13 +52,13 @@ impl Camera {
self.position.y += (input.amount_up - input.amount_down) * (input.speed * dt); self.position.y += (input.amount_up - input.amount_down) * (input.speed * dt);
} }
pub fn update_rot(&mut self, input: &mut Input) { pub fn update_rot(&mut self, input: &Input, sf: f32) {
// doesn't need dt because the input is not continuous.
let (dx, dy) = input.mouse_moved; let (dx, dy) = input.mouse_moved;
input.mouse_moved = (0.0, 0.0);
self.rot_x += Deg(dx * input.sens); self.rot_x += Deg((dx / input.dots_per_deg) * sf);
let pitch_lim = 90.0 - 0.0001; let pitch_lim = 90.0 - 0.0001;
let pitch = self.rot_y.0 + (-dy * input.sens); let pitch = self.rot_y.0 + ((-dy / input.dots_per_deg) * sf);
self.rot_y = Deg(pitch.clamp(-pitch_lim, pitch_lim)); self.rot_y = Deg(pitch.clamp(-pitch_lim, pitch_lim));
} }
} }

@ -62,13 +62,11 @@ pub struct Input {
amount_down: f32, amount_down: f32,
mouse_moved: (f32, f32), mouse_moved: (f32, f32),
speed: f32, speed: f32,
sens: f32, dots_per_deg: f32,
} }
impl Input { impl Input {
pub fn new(speed: f32, dots_multiplier: f32, dots_per_degree: f32) -> Self { pub fn new(speed: f32, dots_per_360deg: f32) -> Self {
let dots_multiplier = 1.0; // 8.8 / 100.0;
let dots_per_degree = 20.46666666666667;
Self { Self {
amount_left: 0.0, amount_left: 0.0,
amount_right: 0.0, amount_right: 0.0,
@ -78,7 +76,7 @@ impl Input {
amount_down: 0.0, amount_down: 0.0,
mouse_moved: (0.0, 0.0), mouse_moved: (0.0, 0.0),
speed, speed,
sens: dots_multiplier / dots_per_degree, dots_per_deg: dots_per_360deg / 360.0,
} }
} }
@ -98,8 +96,7 @@ impl Input {
self.amount_right = amount; self.amount_right = amount;
} }
VirtualKeyCode::Space => { VirtualKeyCode::Space => {
//self.amount_up = amount; self.amount_up = amount;
self.mouse_moved.0 = 7368.0 * amount;
} }
VirtualKeyCode::Semicolon => { VirtualKeyCode::Semicolon => {
self.amount_down = amount; self.amount_down = amount;
@ -153,15 +150,9 @@ fn real_main() -> Result<(), &'static str> {
let mut fullscreen = set_fullscreen(false, &(window), &mut(cursor_locked)); let mut fullscreen = set_fullscreen(false, &(window), &mut(cursor_locked));
let mut state = State::new(window)?; let mut state = State::new(window)?;
let mut input = Input::new( let mut input = Input::new(1.0, 7368.0);
1.0,
8.8 / 100.0,
// i don't want the sensitivity to be tied to the resolution, though.
1.8
);
let mut total_elapsed = 0.0; let mut total_elapsed = 0.0;
let mut timer = Instant::now();
let mut frames = 0; let mut frames = 0;
let mut prev_render = Instant::now(); let mut prev_render = Instant::now();
@ -227,19 +218,20 @@ fn real_main() -> Result<(), &'static str> {
total_elapsed += elapsed; total_elapsed += elapsed;
prev_render = Instant::now(); prev_render = Instant::now();
// doesn't need dt because
// the input is not continuous.
// FIXME: should this be done _after_ updating the camera's position? (updating the position depends on the camera's x rotation.)
state.camera.update_rot(&mut(input));
const TIMESTEP: f32 = 1.0 / 60.0; const TIMESTEP: f32 = 1.0 / 60.0;
let mut interpolate = 1.0;
let sf = interpolate / (elapsed / TIMESTEP);
while elapsed >= TIMESTEP { while elapsed >= TIMESTEP {
state.camera.update_pos(&mut(input), TIMESTEP); state.camera.update_pos(&(input), TIMESTEP);
state.camera.update_rot(&(input), sf);
elapsed -= TIMESTEP; elapsed -= TIMESTEP;
interpolate -= sf;
} }
state.camera.update_pos(&mut(input), elapsed); state.camera.update_pos(&(input), elapsed);
state.camera.update_rot(&(input), interpolate);
input.mouse_moved = (0.0, 0.0);
state.window().request_redraw(); state.window().request_redraw();
} }
@ -254,10 +246,8 @@ fn real_main() -> Result<(), &'static str> {
}; };
frames += 1; frames += 1;
let time = timer.elapsed().as_millis(); if total_elapsed >= 1.0 {
if time > 1000 { println!("frames in the past {total_elapsed}: {frames:?}");
timer = Instant::now();
println!("frames in the past {time}ms: {:?}. {total_elapsed}", frames);
frames = 0; frames = 0;
total_elapsed = 0.0; total_elapsed = 0.0;
} }