use crate::vec3::V3;
use std::f64::consts::PI;
pub const G: f64 = 6.67430e-11;
#[derive(Debug, Clone, Copy)]
pub struct Body {
pub mass: f64,
pub position: V3,
pub velocity: V3,
pub radius: f64,
}
impl Body {
pub fn momentum(&self) -> V3 {
self.velocity * self.mass
}
pub fn density(&self) -> f64 {
self.mass / (4. / 3. * PI * self.radius * self.radius * self.radius)
}
pub fn roche_limit(&self, other: &Self, variant: RocheLimitType) -> f64 {
self.radius
* (2. * self.density() / other.density()).powf(1. / 3.)
* match variant {
RocheLimitType::Rigid => 1.,
RocheLimitType::Fluid => 2.44,
}
}
}
pub enum RocheLimitType {
Rigid,
Fluid,
}
pub fn forces(state: Vec<Body>) -> Vec<V3> {
let mut result = vec![
V3 {
x: 0.,
y: 0.,
z: 0.
};
state.len()
];
for (ix, xb) in state.iter().enumerate() {
for (iy, yb) in state.iter().enumerate() {
if ix != iy {
let d = yb.position - xb.position;
let r2 = d.x * d.x + d.y * d.y + d.z * d.z;
let f = xb.mass * yb.mass / r2;
result[ix] += d.normalize() * f;
}
}
}
for b in result.iter_mut() {
*b *= G;
}
result
}