diff --git a/pumpkin/src/entity/mod.rs b/pumpkin/src/entity/mod.rs index 1fad60799..1d61b2b40 100644 --- a/pumpkin/src/entity/mod.rs +++ b/pumpkin/src/entity/mod.rs @@ -29,14 +29,11 @@ use pumpkin_util::math::{ wrap_degrees, }; use serde::Serialize; -use std::{ - f32::consts::PI, - sync::{ - Arc, - atomic::{ - AtomicBool, AtomicI32, - Ordering::{Relaxed, SeqCst}, - }, +use std::sync::{ + Arc, + atomic::{ + AtomicBool, AtomicI32, + Ordering::{Relaxed, SeqCst}, }, }; use tokio::sync::RwLock; @@ -375,33 +372,25 @@ impl Entity { } pub fn get_facing(&self) -> Facing { - let pitch = self.pitch.load() * (PI / 180.0); - let yaw = -self.yaw.load() * (PI / 180.0); + let pitch = self.pitch.load().to_radians(); + let yaw = -self.yaw.load().to_radians(); - let sin_pitch = pitch.sin(); - let cos_pitch = pitch.cos(); - let sin_yaw = yaw.sin(); - let cos_yaw = yaw.cos(); + let (sin_p, cos_p) = pitch.sin_cos(); + let (sin_y, cos_y) = yaw.sin_cos(); - let abs_sin_yaw = sin_yaw.abs(); - let abs_sin_pitch = sin_pitch.abs(); - let abs_cos_yaw = cos_yaw.abs(); + let x = sin_y * cos_p; + let y = -sin_p; + let z = cos_y * cos_p; - let o = abs_sin_yaw * cos_pitch.abs(); + let ax = x.abs(); + let ay = y.abs(); + let az = z.abs(); - if abs_sin_yaw > abs_cos_yaw { - if abs_sin_pitch > o { - if sin_pitch < 0.0 { - Facing::Up - } else { - Facing::Down - } - } else if sin_yaw > 0.0 { - Facing::East - } else { - Facing::West - } - } else if cos_yaw > 0.0 { + if ax > ay && ax > az { + if x > 0.0 { Facing::East } else { Facing::West } + } else if ay > ax && ay > az { + if y > 0.0 { Facing::Up } else { Facing::Down } + } else if z > 0.0 { Facing::South } else { Facing::North