diff --git a/src/game/camera.rs b/src/game/camera.rs index bf36fc9..e6c32e4 100644 --- a/src/game/camera.rs +++ b/src/game/camera.rs @@ -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] { diff --git a/src/game/controller.rs b/src/game/controller.rs index e899ce3..39f345c 100644 --- a/src/game/controller.rs +++ b/src/game/controller.rs @@ -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]; } }