diff --git a/src/main.rs b/src/main.rs index 0710d42..638ee6f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -272,24 +272,36 @@ fn main() { // Handle keyboard input for camera movement and rotation if let Ok(keys) = pressed_keys.lock() { + // movement speed + let speed = 2.5 * delta_time; + // camera direction vectors based on yaw & pitch + let front = glm::vec3( + cam_yaw.sin() * cam_pitch.cos(), + cam_pitch.sin(), + -cam_yaw.cos() * cam_pitch.cos(), + ); + let right = glm::normalize(&glm::cross(&front, &glm::vec3(0.0, 1.0, 0.0))); + let up_vec = glm::normalize(&glm::cross(&right, &front)); for key in keys.iter() { match key { - // Movement: WASD + Space (up) + LShift (down) - VirtualKeyCode::W => { cam_pos.z -= delta_time; } - VirtualKeyCode::S => { cam_pos.z += delta_time; } - VirtualKeyCode::A => { cam_pos.x -= delta_time; } - VirtualKeyCode::D => { cam_pos.x += delta_time; } - VirtualKeyCode::Space => { cam_pos.y += delta_time; } - VirtualKeyCode::LShift => { cam_pos.y -= delta_time; } - // Rotation: arrow keys for yaw and pitch - VirtualKeyCode::Left => { cam_yaw -= delta_time; } - VirtualKeyCode::Right => { cam_yaw += delta_time; } - VirtualKeyCode::Up => { cam_pitch += delta_time; } - VirtualKeyCode::Down => { cam_pitch -= delta_time; } - // default handler: + // Move relative to camera orientation + VirtualKeyCode::W => { cam_pos += front * speed; } + VirtualKeyCode::S => { cam_pos -= front * speed; } + VirtualKeyCode::A => { cam_pos -= right * speed; } + VirtualKeyCode::D => { cam_pos += right * speed; } + VirtualKeyCode::Space => { cam_pos += up_vec * speed; } + VirtualKeyCode::LShift => { cam_pos -= up_vec * speed; } + // Rotation: adjust yaw and pitch + VirtualKeyCode::Left => { cam_yaw -= delta_time; } + VirtualKeyCode::Right => { cam_yaw += delta_time; } + VirtualKeyCode::Up => { cam_pitch += delta_time; } + VirtualKeyCode::Down => { cam_pitch -= delta_time; } _ => {} } } + // clamp pitch to avoid flipping + let pitch_limit = std::f32::consts::FRAC_PI_2 - 0.01; + cam_pitch = cam_pitch.clamp(-pitch_limit, pitch_limit); } // Handle mouse movement. delta contains the x and y movement of the mouse since last frame in pixels if let Ok(mut delta) = mouse_delta.lock() {