From dcb6e6e5a99f6e3cfe7b8c6077e71c545a72d089 Mon Sep 17 00:00:00 2001 From: yuni Date: Sun, 15 Sep 2024 23:51:03 +0200 Subject: [PATCH] fix handle_wants_lookat() with ID_SPECIAL_PLAYERCAM --- src/actor.rs | 16 +++++++++++----- src/common.rs | 4 ++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/actor.rs b/src/actor.rs index 02f7b58..81fb8d2 100644 --- a/src/actor.rs +++ b/src/actor.rs @@ -599,8 +599,9 @@ fn handle_wants_maxvelocity( } fn handle_wants_lookat( - mut query: Query<(&Position, &mut Rotation, &Transform, &WantsToLookAt)>, + mut query: Query<(&Position, &mut Rotation, &Transform, &WantsToLookAt), Without>, q_playercam: Query<&Position, With>, + q_cam: Query<&Transform, With>, id2pos: Res, ) { let player_pos = if let Ok(player_pos) = q_playercam.get_single() { @@ -608,17 +609,22 @@ fn handle_wants_lookat( } else { return; }; + let cam_pos = if let Ok(cam_trans) = q_cam.get_single() { + cam_trans.translation.as_dvec3() + player_pos.0 + } else { + return; + }; // TODO: use ExternalTorque rather than hard-resetting the rotation for (pos, mut rot, trans, target_id) in &mut query { - let target_pos = if target_id.0 == cmd::ID_SPECIAL_PLAYERCAM { - player_pos + let target_pos: DVec3 = if target_id.0 == cmd::ID_SPECIAL_PLAYERCAM { + cam_pos } else if let Some(target_pos) = id2pos.0.get(&target_id.0) { - target_pos + *target_pos } else { continue; }; - rot.0 = look_at_quat(**pos, *target_pos, trans.up().as_dvec3()); + rot.0 = look_at_quat(**pos, target_pos, trans.up().as_dvec3()); } } diff --git a/src/common.rs b/src/common.rs index 85b3f12..2cc54ab 100644 --- a/src/common.rs +++ b/src/common.rs @@ -110,6 +110,10 @@ pub fn in_shadow( pub fn look_at_quat(from: DVec3, to: DVec3, up: DVec3) -> DQuat { let direction = (to - from).normalize(); + let direction_len = direction.length_squared(); + if direction_len < 1e-4 || direction_len.is_nan() { + return DQuat::IDENTITY; + } let right = up.cross(direction).normalize(); let corrected_up = direction.cross(right);