Better first person controls

This commit is contained in:
Thomas Forgione 2018-04-12 17:40:49 +02:00
parent 83065cedf2
commit c5b04aef9d
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
1 changed files with 74 additions and 3 deletions

View File

@ -11,6 +11,7 @@ use glium::glutin::{
MouseScrollDelta,
KeyboardInput,
VirtualKeyCode,
MouseCursor,
};
use math::vector::{Vector2, Vector3};
@ -181,6 +182,9 @@ pub struct FirstPersonControls {
/// Vector indicating the direction of the camera.
forward: Vector3<f32>,
/// Vector indicating the left of the camera.
left: Vector3<f32>,
/// Speed of the camera.
speed: f32,
@ -192,6 +196,15 @@ pub struct FirstPersonControls {
/// Wether the backward button is pressed or not.
backward_pressed: bool,
/// Wether the left button is pressed or not.
left_pressed: bool,
/// Wether the right button is pressed or not.
right_pressed: bool,
/// Wether the boost is active or not.
boost: bool,
}
impl FirstPersonControls {
@ -202,9 +215,13 @@ impl FirstPersonControls {
phi: 0.0,
position: Vector3::new(0.0, 0.0, 0.0),
forward: Vector3::new(0.0, 0.0, 1.0),
left: Vector3::new(1.0, 0.0, 0.0),
speed: 0.001,
forward_pressed: false,
backward_pressed: false,
left_pressed: false,
right_pressed: false,
boost: false,
sensitivity: 500.0,
}
}
@ -250,6 +267,39 @@ impl Controls for FirstPersonControls {
self.backward_pressed = state == ElementState::Pressed;
},
// On Q pressed
Event::WindowEvent {
event: WindowEvent::KeyboardInput {
input: KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Q), state, ..
}, ..
}, ..
} => {
self.left_pressed = state == ElementState::Pressed;
},
// On D pressed
Event::WindowEvent {
event: WindowEvent::KeyboardInput {
input: KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::D), state, ..
}, ..
}, ..
} => {
self.right_pressed = state == ElementState::Pressed;
},
// On Space pressed
Event::WindowEvent {
event: WindowEvent::KeyboardInput {
input: KeyboardInput {
virtual_keycode: Some(VirtualKeyCode::Space), state, ..
}, ..
}, ..
} => {
self.boost = state == ElementState::Pressed;
},
// On mouse move
Event::WindowEvent {
event: WindowEvent::CursorMoved {
@ -275,6 +325,8 @@ impl Controls for FirstPersonControls {
self.phi.cos() * self.theta.sin(),
);
self.left = Vector3::new(0.0, 1.0, 0.0).cross_product(self.forward);
// Move the cursor back to the center
renderer
.gl_window()
@ -292,15 +344,34 @@ impl Controls for FirstPersonControls {
self.update_camera(camera);
}
fn update<'a>(&mut self, camera: &mut Camera, _: &Renderer<'a, Display>) {
fn update<'a>(&mut self, camera: &mut Camera, renderer: &Renderer<'a, Display>) {
renderer.gl_window().window().set_cursor(MouseCursor::NoneCursor);
let mut speed = Vector3::new(0.0, 0.0, 0.0);
if self.forward_pressed {
self.position += self.forward * self.speed;
speed += self.forward * self.speed;
}
if self.backward_pressed {
self.position -= self.forward * self.speed;
speed -= self.forward * self.speed;
}
if self.left_pressed {
speed += self.left * self.speed;
}
if self.right_pressed {
speed -= self.left * self.speed;
}
if self.boost {
speed *= 10.0;
}
self.position += speed;
self.update_camera(camera);
}
}