Update camera with delta time

This commit is contained in:
2024-09-06 14:40:40 -03:00
parent dbe8a2ad33
commit d325078cd5
2 changed files with 23 additions and 8 deletions

View File

@ -1,3 +1,5 @@
use std::time::Duration;
use winit::event::{ElementState, KeyboardInput, VirtualKeyCode, WindowEvent};
pub struct Camera {
@ -108,19 +110,23 @@ impl CameraController {
}
}
pub fn update_camera(&self, camera: &mut Camera) {
pub fn update_camera(&self, camera: &mut Camera, delta_time: Duration) {
use cgmath::InnerSpace;
let forward = camera.target - camera.eye;
let forward_norm = forward.normalize();
let forward_mag = forward.magnitude();
// Calculate the speed taking into account the delta time
let speed = self.speed * delta_time.as_secs_f32();
println!("speed: {}, delta_time: {:?}", speed, delta_time);
// Prevents glitching when the camera gets too close to the
// center of the scene.
if self.is_forward_pressed && forward_mag > self.speed {
camera.eye += forward_norm * self.speed;
if self.is_forward_pressed && forward_mag > speed {
camera.eye += forward_norm * speed;
}
if self.is_backward_pressed {
camera.eye -= forward_norm * self.speed;
camera.eye -= forward_norm * speed;
}
let right = forward_norm.cross(camera.up);
@ -133,10 +139,10 @@ impl CameraController {
// Rescale the distance between the target and the eye so
// that it doesn't change. The eye, therefore, still
// lies on the circle made by the target and eye.
camera.eye = camera.target - (forward + right * self.speed).normalize() * forward_mag;
camera.eye = camera.target - (forward + right * speed).normalize() * forward_mag;
}
if self.is_left_pressed {
camera.eye = camera.target - (forward - right * self.speed).normalize() * forward_mag;
camera.eye = camera.target - (forward - right * speed).normalize() * forward_mag;
}
}
}

View File

@ -1,4 +1,5 @@
use cgmath::prelude::*;
use std::time::{Duration, Instant};
use wgpu::util::DeviceExt;
use winit::{
dpi::LogicalSize,
@ -53,6 +54,7 @@ pub async fn run() {
}
let mut state = State::new(window).await;
let mut last_update = Instant::now();
event_loop.run(move |event, _, control_flow| {
match event {
@ -98,6 +100,11 @@ pub async fn run() {
Event::MainEventsCleared => {
// RedrawRequested will only trigger once unless we manually
// request it.
let now = Instant::now();
state.delta_time = now - last_update;
last_update = now;
state.window().request_redraw();
}
_ => {}
@ -127,6 +134,7 @@ pub struct State {
instance_buffer: wgpu::Buffer,
depth_texture: Texture,
obj_model: Model,
delta_time: Duration,
}
impl State {
@ -295,7 +303,7 @@ impl State {
label: Some("diffuse_bind_group"),
});
let camera_controller = CameraController::new(0.2);
let camera_controller = CameraController::new(10.0);
let camera = Camera {
// position the camera 1 unit up and 2 units back
@ -423,6 +431,7 @@ impl State {
instance_buffer,
depth_texture,
obj_model,
delta_time: Duration::default(),
}
}
@ -446,7 +455,7 @@ impl State {
}
fn update(&mut self) {
self.camera_controller.update_camera(&mut self.camera);
self.camera_controller.update_camera(&mut self.camera, self.delta_time);
self.camera_uniform.update_view_proj(&self.camera);
self.queue.write_buffer(
&self.camera_buffer,