50 lines
1.6 KiB
Rust
Executable File
50 lines
1.6 KiB
Rust
Executable File
use bevy::ecs::component::Component;
|
|
|
|
#[derive(Debug, Component, Clone)]
|
|
pub struct Position {
|
|
pub lat: f64,
|
|
pub long: f64,
|
|
pub altitude: f64,
|
|
}
|
|
const COMPASS_DRIFT: i32 = -5;
|
|
|
|
impl Position {
|
|
pub fn distance_to_km(&self, other: &Position) -> f64 {
|
|
let radius_of_earth_in_km: f64 = 6371.0;
|
|
let delta_lat = (other.lat - self.lat).to_radians();
|
|
let delta_long = (other.long - self.long).to_radians();
|
|
|
|
let a = (delta_lat / 2.0).sin() * (delta_lat / 2.0).sin()
|
|
+ self.lat.to_radians().cos()
|
|
* other.lat.to_radians().cos()
|
|
* (delta_long / 2.0).sin()
|
|
* (delta_long / 2.0).sin();
|
|
|
|
let c = 2.0 * (a.sqrt().atan2((1.0 - a).sqrt()));
|
|
radius_of_earth_in_km * c
|
|
}
|
|
|
|
pub fn distance_to_nmi(&self, other: &Position) -> f64 {
|
|
self.distance_to_km(other) * 0.5399568035
|
|
}
|
|
|
|
pub fn get_bearing_to(&self, other: &Position) -> i32 {
|
|
let delta_long = (other.long - self.long).to_radians();
|
|
|
|
let y =
|
|
(other.long.to_radians() - self.long.to_radians()).sin() * other.lat.to_radians().cos();
|
|
let x = self.lat.to_radians().cos() * other.lat.to_radians().sin()
|
|
- self.lat.to_radians().sin() * other.lat.to_radians().cos() * delta_long.cos();
|
|
|
|
(((y.atan2(x).to_degrees() + 360.0) % 360.0) as i32) + COMPASS_DRIFT
|
|
}
|
|
|
|
pub fn feet(&self) -> f64 {
|
|
self.altitude * 3.28084
|
|
}
|
|
|
|
pub fn angels(&self) -> i32 {
|
|
(self.feet() / 1000.0).round() as i32
|
|
}
|
|
}
|