From cde43cfd5c285dc213895cebc305d9ba7f094d1a Mon Sep 17 00:00:00 2001 From: tcmal Date: Sun, 25 Aug 2024 17:44:22 +0100 Subject: feat(contrib): add mouse input to flycam controls --- stockton-contrib/src/flycam.rs | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'stockton-contrib') diff --git a/stockton-contrib/src/flycam.rs b/stockton-contrib/src/flycam.rs index 7b82c4f..093f75a 100644 --- a/stockton-contrib/src/flycam.rs +++ b/stockton-contrib/src/flycam.rs @@ -15,7 +15,9 @@ * with this program. If not, see . */ -use stockton_input::{Axis, InputManager}; +use std::f32::consts::PI; + +use stockton_input::{Axis, InputManager, Mouse}; use stockton_types::components::Transform; use stockton_types::Vector3; @@ -29,27 +31,38 @@ pub trait FlycamInput { pub struct FlycamControlled { pub speed: f32, + pub sensitivity: f32, +} + +impl FlycamControlled { + pub fn new(speed: f32, pixels_per_360: f32) -> Self { + FlycamControlled { + speed, + sensitivity: (2.0 * PI) / pixels_per_360, + } + } } #[system(for_each)] pub fn flycam_move( #[resource] manager: &T, #[resource] timing: &Timing, + #[resource] mouse: &Mouse, transform: &mut Transform, flycam: &FlycamControlled, ) where T: 'static + InputManager, T::Inputs: FlycamInput, { - // TODO: Deal with looking around - let inputs = manager.get_inputs(); - let speed = flycam.speed; - let impulse = Vector3::new( - **inputs.get_x_axis() as f32 * speed * timing.delta_time, - **inputs.get_y_axis() as f32 * speed * timing.delta_time, - **inputs.get_z_axis() as f32 * speed * timing.delta_time, + let delta = Vector3::new( + **inputs.get_x_axis() as f32 * flycam.speed * timing.delta_time, + **inputs.get_y_axis() as f32 * flycam.speed * timing.delta_time, + **inputs.get_z_axis() as f32 * flycam.speed * timing.delta_time, ); - transform.position += impulse; + transform.translate(delta); + + let rotation = mouse.delta * flycam.sensitivity; + transform.rotate(Vector3::new(-rotation.y, rotation.x, 0.0)); } -- cgit v1.2.3