From 51059a2856d83724374b51b7867cde3799bf241e Mon Sep 17 00:00:00 2001 From: yuni Date: Sun, 17 Nov 2024 00:02:00 +0100 Subject: [PATCH] WIP crisper camera controls (mouse rot relative instead of absolute) --- src/actor.rs | 29 ++++++++++++++++++++++++----- src/camera.rs | 25 +++++++++---------------- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 9ef3051..7b6cc83 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -39,7 +39,7 @@ impl Plugin for ActorPlugin { update_physics_lifeforms.run_if(game_running), update_power.run_if(game_running), handle_gravity.run_if(game_running), - handle_wants_rotation.run_if(game_running), + handle_wants_rotation_change.run_if(game_running), handle_wants_acceleration.run_if(game_running).run_if(alive), handle_wants_maxrotation.run_if(game_running), handle_wants_maxvelocity @@ -185,7 +185,7 @@ pub struct WantsMaxVelocity(pub f64); #[derive(Component)] pub struct WantsToLookAt(pub String); #[derive(Component)] -pub struct WantsRotation(pub DQuat); +pub struct WantsRotationChange(pub Vec3); // Vec3 = (pitch, yaw, rot) #[derive(Component)] pub struct WantsMatchVelocityWith(pub String); #[derive(Component)] @@ -586,6 +586,7 @@ pub fn handle_vehicle_enter_exit( *driver_vis = Visibility::Hidden; //seems to have no effect... ew_sfx.send(audio::PlaySfxEvent(audio::Sfx::EnterVehicle)); commands.entity(driver).remove::(); + commands.entity(driver).remove::(); commands.entity(driver).remove::(); commands.entity(driver).insert(JustNowEnteredVehicle); commands @@ -717,9 +718,27 @@ fn handle_wants_maxvelocity( } } -fn handle_wants_rotation(mut q_actor: Query<(&mut Rotation, &WantsRotation)>) { - for (mut rot, setrot) in &mut q_actor { - **rot = setrot.0; +fn handle_wants_rotation_change(mut q_actor: Query<(&mut Rotation, &mut WantsRotationChange)>) { + for (mut rot, mut change_rot) in &mut q_actor { + if change_rot.0 == Vec3::ZERO { + continue; + } + + let actual_change_rot = change_rot.0.clamp_length_max(0.10); + for axis in 0..3 { + let original = change_rot.0[axis]; + change_rot.0[axis] = 0.90 * change_rot.0[axis] - actual_change_rot[axis]; + if original.signum() != change_rot.0[axis].signum() { + change_rot.0[axis] = 0.0; + } + } + let change = DQuat::from_euler( + EulerRot::XYZ, + actual_change_rot[0] as f64, + actual_change_rot[1] as f64, + actual_change_rot[2] as f64, + ); + **rot = **rot * change; } } diff --git a/src/camera.rs b/src/camera.rs index 52bc69a..7a1b003 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -450,8 +450,7 @@ pub fn apply_input_to_player( ( Entity, &mut actor::WantsAcceleration, - &Rotation, - Option<&mut actor::WantsRotation>, + Option<&mut actor::WantsRotationChange>, ), With, >, @@ -463,7 +462,7 @@ pub fn apply_input_to_player( if player.is_err() { return; } - let (entity, mut accel, rot, setrot) = player.unwrap(); + let (entity, mut accel, rot_change) = player.unwrap(); let (win_res_x, win_res_y): (f32, f32); if let Ok(window) = &q_windows.get_single() { @@ -491,24 +490,18 @@ pub fn apply_input_to_player( // Apply rotation to player if pitch_yaw_rot != Vec3::ZERO { - let rot_offset = DQuat::from_euler( - EulerRot::XYZ, - pitch_yaw_rot[0] as f64, - pitch_yaw_rot[1] as f64, - pitch_yaw_rot[2] as f64, - ); - let rot_current = if let Some(setrot) = &setrot { - setrot.0 + let rot_change_current = if let Some(rot_change) = &rot_change { + rot_change.0 } else { - **rot + Vec3::ZERO }; - let rot_target = rot_current * rot_offset; - if let Some(mut setrot) = setrot { - setrot.0 = rot_target; + let rot_target = rot_change_current + pitch_yaw_rot; + if let Some(mut rot_change) = rot_change { + rot_change.0 = rot_target; } else { commands .entity(entity) - .try_insert(actor::WantsRotation(rot_target)); + .try_insert(actor::WantsRotationChange(rot_target)); } }