From d67fca88b17120566a93004c99dadeef0a61964b Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Tue, 5 May 2026 09:55:30 -0400 Subject: fix label rendering over large bodies --- src/solar_system.rs | 76 +++++++++-------------------------------------------- 1 file changed, 12 insertions(+), 64 deletions(-) (limited to 'src/solar_system.rs') diff --git a/src/solar_system.rs b/src/solar_system.rs index c53f045..81567d0 100644 --- a/src/solar_system.rs +++ b/src/solar_system.rs @@ -2,7 +2,10 @@ pub mod orbit; pub mod ship; use serde::{Deserialize}; -use crate::{known_stars::*, solar_system::orbit::StaticOrbit, timeman::Second}; +use crate::known_stars::*; +use crate::solar_system::orbit::*; +use crate::solar_system::ship::*; +use crate::timeman::Second; use std::error::Error; const GRAVITATIONAL_CONSTANT: f64 = 6.67408e-20; @@ -48,16 +51,9 @@ pub struct SolarSystem { id: SystemId, name: String, + bodies: Vec, -} - -pub fn update_systems( - solar_systems: std::slice::IterMut<'_, SolarSystem>, - time: Second) -{ - for system in solar_systems { - system.update(time); - } + ships: Vec } impl SolarSystem @@ -107,6 +103,7 @@ impl SolarSystem id: id, name: bodies[0].name().clone(), bodies: bodies, + ships: vec![] }) } @@ -186,65 +183,16 @@ impl OrbitalBody None => return 0 } } - + pub fn calculate_orbit_at( &self, time: Second) - -> cgmath::Vector3 + -> cgmath::Vector3 { - let orbit = match &self.orbit { - Some(v) => v, - None => return cgmath::vec3(0.0, 0.0, 0.0) - }; - - if orbit.parent.is_none() { - return cgmath::Vector3::::new(0.0, 0.0, 0.0); -} - - let eccentricity = orbit.eccentricity; - - let long_asc_node = orbit.long_asc_node; - let arg_periaps = orbit.long_periapsis - long_asc_node; - - let sma_cubed = orbit.semi_major_axis.powf(3.0); - let mean_motion = (self.sgp / sma_cubed).sqrt(); - - let mean_anomaly_epoch = orbit.mean_long - orbit.long_periapsis; - let mean_anomaly = mean_anomaly_epoch + (mean_motion * time as f64); - - //Find the eccentric anomaly via newton's method - let mut eccentric_anomaly = mean_anomaly; - for _ in 0..100 { - let (e_sin, e_cos) = eccentric_anomaly.sin_cos(); - let new_anomaly = eccentric_anomaly - - ( - (eccentric_anomaly - eccentricity * e_sin - mean_anomaly) / - (1.0 - eccentricity * e_cos) - ); - - let diff = eccentric_anomaly - new_anomaly; - eccentric_anomaly = new_anomaly; - if diff.abs() < 1e-6 { - break; - } + match &self.orbit { + Some(orbit) => orbit.calculate_position_at(self, time), + None => cgmath::vec3(0.0, 0.0, 0.0) } - - let true_anomaly = 2.0 * ( - ((1.0 + eccentricity) / (1.0 - eccentricity)).sqrt() - * (eccentric_anomaly / 2.0).tan() - ).atan(); - - let vw = true_anomaly + arg_periaps; - let (vw_sin, vw_cos) = vw.sin_cos(); - let (omega_sin, omega_cos) = long_asc_node.sin_cos(); - let (i_sin, i_cos) = orbit.inclination.sin_cos(); - - let r = orbit.semi_major_axis * (1.0 - eccentricity * eccentric_anomaly.cos()); - let x = r * (omega_cos * vw_cos - omega_sin * vw_sin * i_cos); - let y = r * i_sin * vw_sin; - let z = r * (omega_sin * vw_cos + omega_cos * vw_sin * i_cos); - - cgmath::Vector3::::new(x, y, z) } } -- cgit v1.2.3