summaryrefslogtreecommitdiffstats
path: root/src/solar_system.rs
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2026-05-06 12:01:42 -0400
committerJon Santmyer <jon@jonsantmyer.com>2026-05-06 12:01:42 -0400
commit9788d9037ad7199701b1710c28559cb96bce5aec (patch)
treec490b40b1f0057c98c2b4bb3c2593f915313d14f /src/solar_system.rs
parentd67fca88b17120566a93004c99dadeef0a61964b (diff)
downloadsystemic4x-9788d9037ad7199701b1710c28559cb96bce5aec.tar.gz
systemic4x-9788d9037ad7199701b1710c28559cb96bce5aec.tar.bz2
systemic4x-9788d9037ad7199701b1710c28559cb96bce5aec.zip
regenerate orbit lines based on system tick interval
Diffstat (limited to 'src/solar_system.rs')
-rw-r--r--src/solar_system.rs95
1 files changed, 10 insertions, 85 deletions
diff --git a/src/solar_system.rs b/src/solar_system.rs
index 81567d0..ad1f304 100644
--- a/src/solar_system.rs
+++ b/src/solar_system.rs
@@ -1,10 +1,13 @@
-pub mod orbit;
+pub mod body;
pub mod ship;
+pub mod orbit;
+
+use self::body::*;
+use self::ship::*;
+use self::orbit::*;
use serde::{Deserialize};
use crate::known_stars::*;
-use crate::solar_system::orbit::*;
-use crate::solar_system::ship::*;
use crate::timeman::Second;
use std::error::Error;
@@ -15,7 +18,6 @@ pub type Kilometers = f64;
pub type Percentage = f64;
pub type Angle = f64;
-pub type BodyId = usize;
pub type SystemId = usize;
#[derive(Debug, Deserialize)]
@@ -36,17 +38,6 @@ pub struct CSVOrbitalBody
semi_major_axis: Kilometers,
}
-pub struct OrbitalBody
-{
- name: String,
- mass: Kilograms,
- radius: Kilometers,
- sgp: f64,
-
- orbit: Option<StaticOrbit>,
- position: Option<cgmath::Vector3<Kilometers>>
-}
-
pub struct SolarSystem
{
id: SystemId,
@@ -73,7 +64,7 @@ impl SolarSystem
match record.orbits {
Some(orbits) => {
if record.sgp == 0.0 {
- record.sgp = (bodies[orbits].mass + record.mass) * GRAVITATIONAL_CONSTANT;
+ record.sgp = (bodies[orbits].mass() + record.mass) * GRAVITATIONAL_CONSTANT;
}
}
None => {}
@@ -81,22 +72,7 @@ impl SolarSystem
if record.radius == 0.0 { continue; }
println!("New body: {:?}", record);
- bodies.push(OrbitalBody {
- name: record.name,
- mass: record.mass,
- radius: record.radius,
- sgp: record.sgp,
- orbit: Some(StaticOrbit {
- parent: record.orbits,
- eccentricity: record.eccentricity,
- inclination: record.inclination,
- long_asc_node: record.long_asc_node,
- long_periapsis: record.long_periapsis,
- mean_long: record.mean_long,
- semi_major_axis: record.semi_major_axis
- }),
- position: None
- });
+ bodies.push(OrbitalBody::new_from_record(bodies.len(), record));
}
Ok(Self {
@@ -133,19 +109,7 @@ impl SolarSystem
body: &OrbitalBody)
-> cgmath::Vector3<Kilometers>
{
- match &body.orbit {
- Some(orbit) => {
- match orbit.parent {
- Some(parent) => {
- let this_pos = body.position();
- let parent_pos = self.bodies[parent].position();
- this_pos + parent_pos
- },
- None => body.position()
- }
- },
- None => body.position()
- }
+ body.absolute_position(self)
}
pub fn update(
@@ -153,46 +117,7 @@ impl SolarSystem
time: Second)
{
self.bodies.iter_mut().for_each(|body| {
- body.position = Some(body.calculate_orbit_at(time));
+ body.update(time);
});
}
}
-
-impl OrbitalBody
-{
- pub fn name(&self) -> &String { &self.name }
- pub fn radius(&self) -> f32 { self.radius as f32 }
- pub fn position(&self) -> cgmath::Vector3<f64> { self.position.unwrap() }
- pub fn does_orbit(&self) -> bool {
- match &self.orbit {
- Some(orbit) => orbit.parent.is_some(),
- None => false
- }
- }
- pub fn get_orbits(&self) -> Option<BodyId> {
- match &self.orbit {
- Some(orbit) => orbit.parent(),
- None => None
- }
- }
-
- pub fn orbital_period(&self) -> Second
- {
- match &self.orbit {
- Some(v) => v.period(self),
- None => return 0
- }
- }
-
- pub fn calculate_orbit_at(
- &self,
- time: Second)
- -> cgmath::Vector3<Kilometers>
- {
- match &self.orbit {
- Some(orbit) => orbit.calculate_position_at(self, time),
- None => cgmath::vec3(0.0, 0.0, 0.0)
- }
- }
-}
-