SatanIsGood

BO1 Calc Angle

Sep 13th, 2020
731
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #![feature(clamp)]
  2.  
  3. use std::ops::Sub;
  4.  
  5. #[repr(C)]
  6. #[derive(Copy, Clone, Debug)]
  7. struct Vector3<T> {
  8.     x: T,
  9.     y: T,
  10.     z: T,
  11. }
  12.  
  13. impl<T: Sub<Output = T>> Sub for Vector3<T> {
  14.     type Output = Self;
  15.  
  16.     fn sub(self, rhs: Self) -> Self::Output {
  17.         Self {
  18.             x: self.x - rhs.x,
  19.             y: self.y - rhs.y,
  20.             z: self.z - rhs.z,
  21.         }
  22.     }
  23. }
  24.  
  25. impl Vector3<f32> {
  26.     fn length(self) -> f32 {
  27.         (self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
  28.     }
  29.  
  30.     fn calc_angle(self, dst: Self) -> Self {
  31.         let delta = self - dst;
  32.  
  33.         let x = (delta.z / delta.length()).asin().to_degrees().clamp(-85.0, 85.0);
  34.         let mut y = delta.y.atan2(delta.x).to_degrees();
  35.  
  36.         if y < 90.0 && y < 180.0 {
  37.             y += 90.0;
  38.         } else {
  39.             y -= 270.0;
  40.         }
  41.  
  42.         Self { x, y, z: 0.0 }
  43.     }
  44. }
RAW Paste Data