mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-10 01:28:41 -06:00
better movement
This commit is contained in:
parent
a10b59880a
commit
85cab9fb2c
|
@ -5,6 +5,14 @@
|
||||||
|
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
|
pub fn calculate_forward_direction(yaw: f32, pitch: f32) -> [f32; 3] {
|
||||||
|
[
|
||||||
|
yaw.cos() * pitch.cos(),
|
||||||
|
pitch.sin(),
|
||||||
|
yaw.sin() * pitch.cos(),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Camera {
|
pub struct Camera {
|
||||||
pub yaw: f32,
|
pub yaw: f32,
|
||||||
pub pitch: f32,
|
pub pitch: f32,
|
||||||
|
@ -18,11 +26,12 @@ pub struct Camera {
|
||||||
impl Camera {
|
impl Camera {
|
||||||
/// Update camera direction based on yaw/pitch
|
/// Update camera direction based on yaw/pitch
|
||||||
pub fn update_direction(&mut self) {
|
pub fn update_direction(&mut self) {
|
||||||
self.direction = [
|
self.direction = calculate_forward_direction(self.yaw, self.pitch);
|
||||||
self.yaw.cos() * self.pitch.cos(),
|
}
|
||||||
self.pitch.sin(),
|
pub fn forward(&mut self, amount: f32) {
|
||||||
self.yaw.sin() * self.pitch.cos(),
|
self.position[0] += self.direction[0] * amount;
|
||||||
];
|
self.position[1] += self.direction[1] * amount;
|
||||||
|
self.position[2] += self.direction[2] * amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn view_matrix(&self) -> [[f32; 4]; 4] {
|
pub fn view_matrix(&self) -> [[f32; 4]; 4] {
|
||||||
|
|
|
@ -16,14 +16,20 @@ pub struct Actions {
|
||||||
}
|
}
|
||||||
impl Actions {
|
impl Actions {
|
||||||
pub fn apply_to_camera(&self, camera: &mut Camera) {
|
pub fn apply_to_camera(&self, camera: &mut Camera) {
|
||||||
//Apply movement
|
|
||||||
camera.position[0] += self.movement[0];
|
|
||||||
camera.position[1] += self.movement[1];
|
|
||||||
camera.position[2] += self.movement[2];
|
|
||||||
//Apply rotation
|
//Apply rotation
|
||||||
camera.yaw -= self.rotation[0];
|
camera.yaw -= self.rotation[0];
|
||||||
camera.pitch -= self.rotation[1];
|
camera.pitch -= self.rotation[1];
|
||||||
camera.update_direction();
|
camera.update_direction();
|
||||||
|
//Apply movement
|
||||||
|
let (yaw_sin, yaw_cos) = camera.yaw.sin_cos();
|
||||||
|
//forward movement
|
||||||
|
camera.position[0] += yaw_cos * self.movement[2];
|
||||||
|
camera.position[2] += yaw_sin * self.movement[2];
|
||||||
|
//sideways movement
|
||||||
|
camera.position[0] -= -yaw_sin * self.movement[0];
|
||||||
|
camera.position[2] -= yaw_cos * self.movement[0];
|
||||||
|
//up/down movement
|
||||||
|
camera.position[1] += self.movement[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue