diff options
-rw-r--r-- | examples/render-bsp/src/main.rs | 7 | ||||
-rw-r--r-- | stockton-contrib/src/flycam.rs | 31 | ||||
-rw-r--r-- | stockton-input/Cargo.toml | 3 | ||||
-rw-r--r-- | stockton-input/src/lib.rs | 2 | ||||
-rw-r--r-- | stockton-input/src/mouse.rs | 40 | ||||
-rw-r--r-- | stockton-render/src/window.rs | 15 | ||||
-rw-r--r-- | stockton-types/src/components/mod.rs | 39 |
7 files changed, 123 insertions, 14 deletions
diff --git a/examples/render-bsp/src/main.rs b/examples/render-bsp/src/main.rs index 5c03676..9332629 100644 --- a/examples/render-bsp/src/main.rs +++ b/examples/render-bsp/src/main.rs @@ -25,7 +25,7 @@ use winit::{event::Event, event_loop::EventLoop, window::WindowBuilder}; use stockton_contrib::delta_time::*; use stockton_contrib::flycam::*; -use stockton_input::{Axis, InputManager}; +use stockton_input::{Axis, InputManager, Mouse}; use stockton_levels::{prelude::*, q3::Q3BSPFile}; use stockton_render::{ do_render_system, draw::calc_vp_matrix_system, window::process_window_events_system, Renderer, @@ -110,7 +110,8 @@ fn main() { resources.insert(renderer); resources.insert(bsp); resources.insert(manager); - resources.insert(Timing::default()) + resources.insert(Timing::default()); + resources.insert(Mouse::default()); }, move |schedule| { schedule @@ -134,7 +135,7 @@ fn main() { fov: 90.0, near: 0.1, }, - FlycamControlled { speed: 512.0 }, + FlycamControlled::new(512.0, 400.0), )); // Done loading - This is our main loop. 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 <http://www.gnu.org/licenses/>. */ -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<T>( #[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)); } diff --git a/stockton-input/Cargo.toml b/stockton-input/Cargo.toml index 39c0666..fa6507f 100644 --- a/stockton-input/Cargo.toml +++ b/stockton-input/Cargo.toml @@ -4,4 +4,5 @@ version = "0.1.0" authors = ["Oscar Shrimpton <oscar.shrimpton.personal@gmail.com>"] edition = "2018" -[dependencies]
\ No newline at end of file +[dependencies] +stockton-types = { path = "../stockton-types" }
\ No newline at end of file diff --git a/stockton-input/src/lib.rs b/stockton-input/src/lib.rs index 7bb779f..f206f41 100644 --- a/stockton-input/src/lib.rs +++ b/stockton-input/src/lib.rs @@ -18,7 +18,9 @@ pub mod axis; pub mod button; pub mod manager; +pub mod mouse; pub use axis::Axis; pub use button::Button; pub use manager::*; +pub use mouse::Mouse; diff --git a/stockton-input/src/mouse.rs b/stockton-input/src/mouse.rs new file mode 100644 index 0000000..7ffad47 --- /dev/null +++ b/stockton-input/src/mouse.rs @@ -0,0 +1,40 @@ +/* + * Copyright (C) Oscar Shrimpton 2020 + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +use stockton_types::Vector2; + +#[derive(Debug, Clone)] +pub struct Mouse { + pub abs: Vector2, + pub delta: Vector2, +} + +impl Default for Mouse { + fn default() -> Self { + Mouse { + abs: Vector2::zeros(), + delta: Vector2::zeros(), + } + } +} + +impl Mouse { + pub fn handle_frame(&mut self, new: Vector2) { + self.delta = new - self.abs; + self.abs = new; + } +} diff --git a/stockton-render/src/window.rs b/stockton-render/src/window.rs index fa22e1d..efb8030 100644 --- a/stockton-render/src/window.rs +++ b/stockton-render/src/window.rs @@ -18,7 +18,7 @@ use crate::Renderer; use legion::systems::Runnable; -use stockton_input::{Action as KBAction, InputManager}; +use stockton_input::{Action as KBAction, InputManager, Mouse}; use winit::event::{ElementState, Event as WinitEvent, WindowEvent as WinitWindowEvent}; use winit::event_loop::ControlFlow; @@ -28,6 +28,7 @@ pub enum WindowEvent { SizeChanged, CloseRequested, KeyboardAction(KBAction), + MouseMoved(f32, f32), } impl WindowEvent { @@ -45,6 +46,10 @@ impl WindowEvent { KBAction::KeyRelease(input.scancode), )), }, + WinitWindowEvent::CursorMoved { position, .. } => Some(WindowEvent::MouseMoved( + position.x as f32, + position.y as f32, + )), _ => None, }, _ => None, @@ -57,9 +62,11 @@ impl WindowEvent { pub fn _process_window_events<T: 'static + InputManager>( #[resource] renderer: &mut Renderer<'static>, #[resource] manager: &mut T, + #[resource] mouse: &mut Mouse, #[state] actions_buf: &mut Vec<KBAction>, ) { let mut actions_buf_cursor = 0; + let mut mouse_delta = mouse.abs; while let Ok(event) = renderer.window_events.try_recv() { match event { @@ -77,9 +84,15 @@ pub fn _process_window_events<T: 'static + InputManager>( } actions_buf_cursor += 1; } + WindowEvent::MouseMoved(x, y) => { + mouse_delta.x = x; + mouse_delta.y = y; + } }; } + mouse.handle_frame(mouse_delta); + manager.handle_frame(&actions_buf[0..actions_buf_cursor]); } diff --git a/stockton-types/src/components/mod.rs b/stockton-types/src/components/mod.rs index 6f127c1..f5d12e7 100644 --- a/stockton-types/src/components/mod.rs +++ b/stockton-types/src/components/mod.rs @@ -15,8 +15,17 @@ * with this program. If not, see <http://www.gnu.org/licenses/>. */ +use na::{Mat4, Vec4}; +use std::f32::consts::PI; + use crate::Vector3; +/// 90 degrees in radians +const R89: f32 = (PI / 180.0) * 89.0; + +/// 180 degrees in radians +const R180: f32 = PI; + #[derive(Clone, Copy, Debug, PartialEq)] pub struct Transform { /// Position of the object @@ -26,6 +35,36 @@ pub struct Transform { pub rotation: Vector3, } +impl Transform { + pub fn rotate(&mut self, vec: Vector3) { + self.rotation += vec; + + // Clamp -pi/2 < pitch < pi/2 + if self.rotation.x > R89 { + self.rotation.x = R89; + } else if self.rotation.x <= -R89 { + self.rotation.x = -R89; + } + + // -pi < yaw <= pi + if self.rotation.y <= -R180 { + self.rotation.y = R180 - self.rotation.y % -R180; + } else if self.rotation.y > 180.0 { + self.rotation.y = -R180 + self.rotation.y % R180; + } + } + + pub fn translate(&mut self, delta: Vector3) { + let rot_matrix = + Mat4::from_euler_angles(-self.rotation.x, self.rotation.y, self.rotation.z); + + let new = rot_matrix * Vec4::new(delta.x, delta.y, delta.z, 1.0); + self.position.x += new.x; + self.position.y += new.y; + self.position.z += new.z; + } +} + #[derive(Clone, Copy, Debug, PartialEq)] pub struct CameraSettings { /// FOV (radians) |