better movement

This commit is contained in:
griffi-gh 2023-01-16 00:04:13 +01:00
parent a10b59880a
commit 85cab9fb2c
2 changed files with 24 additions and 9 deletions

View file

@ -5,6 +5,14 @@
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 yaw: f32,
pub pitch: f32,
@ -18,11 +26,12 @@ pub struct Camera {
impl Camera {
/// Update camera direction based on yaw/pitch
pub fn update_direction(&mut self) {
self.direction = [
self.yaw.cos() * self.pitch.cos(),
self.pitch.sin(),
self.yaw.sin() * self.pitch.cos(),
];
self.direction = calculate_forward_direction(self.yaw, self.pitch);
}
pub fn forward(&mut self, amount: f32) {
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] {

View file

@ -16,14 +16,20 @@ pub struct Actions {
}
impl Actions {
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
camera.yaw -= self.rotation[0];
camera.pitch -= self.rotation[1];
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];
}
}