From 7f63ec5c10eb7e8dd4edaabd1a6a437328911d39 Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Sun, 10 May 2026 13:29:56 -0400 Subject: fleets --- src/solar_system/body.rs | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) (limited to 'src/solar_system/body.rs') diff --git a/src/solar_system/body.rs b/src/solar_system/body.rs index 7618a95..3e61486 100644 --- a/src/solar_system/body.rs +++ b/src/solar_system/body.rs @@ -1,7 +1,11 @@ +use crate::timeman; + use super::*; pub type BodyId = usize; +pub const BODY_TICK_DURATION: Second = timeman::DAY; + pub struct OrbitalBody { id: BodyId, @@ -11,11 +15,23 @@ pub struct OrbitalBody sgp: f64, orbit: Option, - rel_pos: Option> + rel_pos: Option<(i64, cgmath::Vector3)> +} + +impl StaticOrbiter for OrbitalBody { + fn orbit(&self) -> Option<&StaticOrbit> { + self.orbit.as_ref() + } + + fn sgp(&self) -> f64 { + self.sgp + } } impl OrbitalBody { + pub const TICK_DURATION: Second = BODY_TICK_DURATION; + pub fn new_from_record( id: BodyId, record: CSVOrbitalBody) @@ -45,21 +61,30 @@ impl OrbitalBody pub fn id(&self) -> BodyId { self.id } pub fn name(&self) -> &String { &self.name } pub fn radius(&self) -> f32 { self.radius as f32 } - pub fn relative_position(&self) -> cgmath::Vector3 { self.rel_pos.unwrap() } pub fn mass(&self) -> Kilograms { self.mass } pub fn sgp(&self) -> f64 { self.sgp } + pub fn relative_position( + &self, + time: Second) + -> cgmath::Vector3 + { + let time = time - (time % BODY_TICK_DURATION); + self.calculate_orbit_at(time) + } + pub fn absolute_position( &self, - solar_system: &SolarSystem) + solar_system: &SolarSystem, + time: Second) -> cgmath::Vector3 { match &self.orbit { Some(orbit) => { - let parent_pos = solar_system.bodies()[orbit.parent()].absolute_position(solar_system); - parent_pos + self.relative_position() + let parent_pos = solar_system.bodies()[orbit.parent()].absolute_position(solar_system, time); + parent_pos + self.relative_position(time) }, - None => self.relative_position() + None => self.relative_position(time) } } @@ -81,16 +106,9 @@ impl OrbitalBody -> cgmath::Vector3 { match &self.orbit { - Some(orbit) => orbit.calculate_position_at(self.sgp(), time), + Some(orbit) => orbit.calculate_position_at(self, time), None => cgmath::vec3(0.0, 0.0, 0.0) } } - - pub fn tick( - &mut self, - time: Second) - { - self.rel_pos = Some(self.calculate_orbit_at(time)); - } } -- cgit v1.2.3