From e90f95bc756755f988b03c3427cd9feaccf76810 Mon Sep 17 00:00:00 2001 From: timokoesters Date: Sat, 24 Aug 2019 19:58:28 +0200 Subject: [PATCH] Look in the direction of the camera --- common/src/comp/controller.rs | 1 + common/src/sys/movement.rs | 11 +++++++++-- voxygen/src/session.rs | 23 +++++++++-------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/common/src/comp/controller.rs b/common/src/comp/controller.rs index f64c961977..45dc34df86 100644 --- a/common/src/comp/controller.rs +++ b/common/src/comp/controller.rs @@ -5,6 +5,7 @@ use vek::*; #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] pub struct Controller { pub move_dir: Vec2, + pub look_dir: Vec3, pub jump: bool, pub attack: bool, pub block: bool, diff --git a/common/src/sys/movement.rs b/common/src/sys/movement.rs index 9eb73c1c71..5414fe3645 100644 --- a/common/src/sys/movement.rs +++ b/common/src/sys/movement.rs @@ -94,11 +94,18 @@ impl<'a> System<'a> for Sys { }; // Set direction based on move direction when on the ground - let ori_dir = if character.movement == Glide || character.movement.is_roll() { + let ori_dir = if controller + .look_dir + .map(|n| !n.is_normal() || n.abs() < std::f32::EPSILON) + .reduce_or() + || character.movement == Glide + || character.movement.is_roll() + { Vec2::from(vel.0) } else { - controller.move_dir + Vec2::from(controller.look_dir).normalized() }; + if ori_dir.magnitude_squared() > 0.0001 && (ori.0.normalized() - Vec3::from(ori_dir).normalized()).magnitude_squared() > 0.001 diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 3b750fdbeb..1bbfad91e6 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -105,19 +105,19 @@ impl PlayState for SessionState { self.client.borrow_mut().send_chat(cmd.to_string()); } } - // Compute camera data - let get_cam_data = |camera: &Camera, client: &Client| { - let (view_mat, _, cam_pos) = camera.compute_dependents(client); - let cam_dir: Vec3 = Vec3::from(view_mat.inverted() * -Vec4::unit_z()); - - (cam_dir, cam_pos) - }; // Game loop let mut current_client_state = self.client.borrow().get_client_state(); while let ClientState::Pending | ClientState::Character | ClientState::Dead = current_client_state { + // Compute camera data + let (view_mat, _, cam_pos) = self + .scene + .camera() + .compute_dependents(&self.client.borrow()); + let cam_dir: Vec3 = Vec3::from(view_mat.inverted() * -Vec4::unit_z()); + // Handle window events. for event in global_state.window.fetch_events() { // Pass all events to the ui first. @@ -142,8 +142,6 @@ impl PlayState for SessionState { .get(client.entity()) .is_some() { - let (cam_dir, cam_pos) = get_cam_data(&self.scene.camera(), &client); - let (d, b) = { let terrain = client.state().terrain(); let ray = terrain.ray(cam_pos, cam_pos + cam_dir * 100.0).cast(); @@ -169,8 +167,6 @@ impl PlayState for SessionState { .get(client.entity()) .is_some() { - let (cam_dir, cam_pos) = get_cam_data(&self.scene.camera(), &client); - let (d, b) = { let terrain = client.state().terrain(); let ray = terrain.ray(cam_pos, cam_pos + cam_dir * 100.0).cast(); @@ -194,9 +190,6 @@ impl PlayState for SessionState { .is_some() { if state { - let (cam_dir, cam_pos) = - get_cam_data(&self.scene.camera(), &client); - if let Ok(Some(block)) = client .state() .terrain() @@ -268,6 +261,8 @@ impl PlayState for SessionState { let dir_vec = self.key_state.dir_vec(); self.controller.move_dir = unit_vecs.0 * dir_vec[0] + unit_vecs.1 * dir_vec[1]; + self.controller.look_dir = cam_dir; + // Perform an in-game tick. if let Err(err) = self.tick(clock.get_avg_delta()) { error!("Failed to tick the scene: {:?}", err);