fix handle_wants_lookat() with ID_SPECIAL_PLAYERCAM

This commit is contained in:
yuni 2024-09-15 23:51:03 +02:00
parent 904f7927eb
commit dcb6e6e5a9
2 changed files with 15 additions and 5 deletions

View file

@ -599,8 +599,9 @@ fn handle_wants_maxvelocity(
} }
fn handle_wants_lookat( fn handle_wants_lookat(
mut query: Query<(&Position, &mut Rotation, &Transform, &WantsToLookAt)>, mut query: Query<(&Position, &mut Rotation, &Transform, &WantsToLookAt), Without<Camera>>,
q_playercam: Query<&Position, With<PlayerCamera>>, q_playercam: Query<&Position, With<PlayerCamera>>,
q_cam: Query<&Transform, With<Camera>>,
id2pos: Res<game::Id2Pos>, id2pos: Res<game::Id2Pos>,
) { ) {
let player_pos = if let Ok(player_pos) = q_playercam.get_single() { let player_pos = if let Ok(player_pos) = q_playercam.get_single() {
@ -608,17 +609,22 @@ fn handle_wants_lookat(
} else { } else {
return; 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 // TODO: use ExternalTorque rather than hard-resetting the rotation
for (pos, mut rot, trans, target_id) in &mut query { for (pos, mut rot, trans, target_id) in &mut query {
let target_pos = if target_id.0 == cmd::ID_SPECIAL_PLAYERCAM { let target_pos: DVec3 = if target_id.0 == cmd::ID_SPECIAL_PLAYERCAM {
player_pos cam_pos
} else if let Some(target_pos) = id2pos.0.get(&target_id.0) { } else if let Some(target_pos) = id2pos.0.get(&target_id.0) {
target_pos *target_pos
} else { } else {
continue; 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());
} }
} }

View file

@ -110,6 +110,10 @@ pub fn in_shadow(
pub fn look_at_quat(from: DVec3, to: DVec3, up: DVec3) -> DQuat { pub fn look_at_quat(from: DVec3, to: DVec3, up: DVec3) -> DQuat {
let direction = (to - from).normalize(); 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 right = up.cross(direction).normalize();
let corrected_up = direction.cross(right); let corrected_up = direction.cross(right);