From f076172fb8fbd6318a17ad23117e95cb9e7d6b74 Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 15 Jan 2023 23:05:45 +0100 Subject: [PATCH] camera controls! --- src/game.rs | 16 +++++++++++++--- src/game/controller.rs | 30 +++++++++++++++++------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/game.rs b/src/game.rs index 4469405..6415fe8 100644 --- a/src/game.rs +++ b/src/game.rs @@ -1,6 +1,6 @@ use glium::{Surface, uniform}; use glium::glutin::{ - event::{Event, WindowEvent, DeviceEvent, VirtualKeyCode}, + event::{Event, WindowEvent, DeviceEvent, KeyboardInput, VirtualKeyCode}, event_loop::{EventLoop, ControlFlow}, }; use std::time::Instant; @@ -41,6 +41,8 @@ pub fn run() { let assets = Assets::load_all_sync(&display); log::info!("init game state"); let mut state = State::init(); + state.camera.position = [0., 0., 1.]; + state.camera.direction = [0., 0., -1.]; log::info!("game loaded"); //======================= @@ -58,8 +60,7 @@ pub fn run() { match event { Event::MainEventsCleared => (), Event::DeviceEvent { - event: DeviceEvent::MouseMotion{ delta, }, - .. + event: DeviceEvent::MouseMotion{ delta, }, .. } => { state.controls.process_mouse_input(delta.0, delta.1); } @@ -70,6 +71,14 @@ pub fn run() { *control_flow = ControlFlow::Exit; return }, + WindowEvent::KeyboardInput { + input: KeyboardInput { + virtual_keycode: Some(key), + state: el_state, .. + }, .. + } => { + state.controls.process_keyboard_input(key, el_state); + }, _ => return } }, @@ -81,6 +90,7 @@ pub fn run() { last_render = now; let actions = state.controls.calculate(dt); + log::trace!("{}", actions.rotation[0]); actions.apply_to_camera(&mut state.camera); let mut target = display.draw(); diff --git a/src/game/controller.rs b/src/game/controller.rs index 299ce19..9c28915 100644 --- a/src/game/controller.rs +++ b/src/game/controller.rs @@ -11,18 +11,18 @@ pub struct InputAmounts { } pub struct Actions { - movement: [f32; 3], - rotation: [f32; 2], + pub movement: [f32; 3], + pub rotation: [f32; 2], } impl Actions { pub fn apply_to_camera(&self, camera: &mut Camera) { //Apply movement for v in camera.position.iter_mut().zip(self.movement) { - *v.0 += v.1; + *v.0 -= v.1; } //Apply rotation - camera.yaw += self.rotation[0]; - camera.pitch += self.rotation[1]; + camera.yaw -= self.rotation[0]; + camera.pitch -= self.rotation[1]; camera.update_direction(); } } @@ -62,13 +62,17 @@ impl Controls { } } pub fn calculate(&mut self, dt: f32) -> Actions { - let mut movement = { + let movement = { let magnitude = (self.inputs.move_x.powi(2) + self.inputs.move_y.powi(2) + self.inputs.move_z.powi(2)).sqrt(); - [ - dt * self.speed * (self.inputs.move_x / magnitude), - dt * self.speed * (self.inputs.move_y / magnitude), - dt * self.speed * (self.inputs.move_z / magnitude) - ] + if magnitude == 0. { + [0., 0., 0.] + } else { + [ + dt * self.speed * (self.inputs.move_x / magnitude), + dt * self.speed * (self.inputs.move_y / magnitude), + dt * self.speed * (self.inputs.move_z / magnitude) + ] + } }; let rotation = [ dt * self.inputs.look_h * self.sensitivity, @@ -82,8 +86,8 @@ impl Default for Controls { fn default() -> Self { Self { inputs: Default::default(), - speed: 1., - sensitivity: 1., + speed: 10., + sensitivity: 2., } } }