summaryrefslogtreecommitdiffstats
path: root/src/solar_system
diff options
context:
space:
mode:
Diffstat (limited to 'src/solar_system')
-rw-r--r--src/solar_system/body.rs46
-rw-r--r--src/solar_system/fleet.rs60
-rw-r--r--src/solar_system/orbit.rs18
3 files changed, 44 insertions, 80 deletions
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<StaticOrbit>,
- rel_pos: Option<cgmath::Vector3<Kilometers>>
+ rel_pos: Option<(i64, cgmath::Vector3<Kilometers>)>
+}
+
+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<f64> { 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<f64>
+ {
+ 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<Kilometers>
{
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<Kilometers>
{
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));
- }
}
diff --git a/src/solar_system/fleet.rs b/src/solar_system/fleet.rs
deleted file mode 100644
index c331cd7..0000000
--- a/src/solar_system/fleet.rs
+++ /dev/null
@@ -1,60 +0,0 @@
-use crate::solar_system::{GRAVITATIONAL_CONSTANT, Kilometers, SolarSystem, body::OrbitalBody, orbit::StaticOrbit};
-use crate::timeman::Second;
-
-pub type FleetId = usize;
-
-pub struct Fleet
-{
- id: FleetId,
- name: String,
-
- position: cgmath::Vector3<Kilometers>,
- velocity: cgmath::Vector3<f32>,
- acceleration: cgmath::Vector3<f32>,
-
- baked_orbit: Option<(f64, StaticOrbit)>,
-}
-
-impl Fleet
-{
- pub fn new(
- id: FleetId,
- name: String)
- -> Self
- {
- Self {
- id,
- name,
- position: cgmath::vec3(0.0, 0.0, 0.0),
- velocity: cgmath::vec3(0.0, 0.0, 0.0),
- acceleration: cgmath::vec3(0.0, 0.0, 0.0),
- baked_orbit: None,
- }
- }
-
- pub fn id(&self) -> FleetId { self.id }
- pub fn name(&self) -> &String { &self.name }
-
- pub fn make_orbit(
- &mut self,
- body: &OrbitalBody,
- radius: Kilometers)
- {
- let sgp = body.mass() * GRAVITATIONAL_CONSTANT;
- self.baked_orbit = Some((sgp, StaticOrbit::new_circular(body, radius)));
- }
-
- pub fn tick(
- &mut self,
- time: Second)
- {
- match &self.baked_orbit {
- Some(orbit_info) => {
- let sgp = orbit_info.0;
- let orbit = &orbit_info.1;
- self.position = orbit.calculate_position_at(sgp, time);
- },
- None => {}
- }
- }
-}
diff --git a/src/solar_system/orbit.rs b/src/solar_system/orbit.rs
index 3a1d129..f5a564c 100644
--- a/src/solar_system/orbit.rs
+++ b/src/solar_system/orbit.rs
@@ -1,4 +1,4 @@
-use crate::solar_system::{Angle, BodyId, Kilometers, OrbitalBody, Percentage, SolarSystem};
+use crate::solar_system::{Angle, BodyId, Kilometers, OrbitalBody, Percentage};
use crate::timeman::{Second};
pub struct StaticOrbit
@@ -13,6 +13,12 @@ pub struct StaticOrbit
semi_major_axis: Kilometers,
}
+pub trait StaticOrbiter
+{
+ fn orbit(&self) -> Option<&StaticOrbit>;
+ fn sgp(&self) -> f64;
+}
+
impl StaticOrbit
{
pub fn new(
@@ -51,9 +57,9 @@ impl StaticOrbit
}
}
- pub fn period(
+ pub fn period<T: StaticOrbiter>(
&self,
- this_body: &OrbitalBody)
+ this_body: &T)
-> Second
{
((self.semi_major_axis.powf(3.0) / this_body.sgp()).sqrt() * std::f64::consts::TAU) as Second
@@ -65,9 +71,9 @@ impl StaticOrbit
pub fn sma(&self) -> Kilometers { self.semi_major_axis }
- pub fn calculate_position_at(
+ pub fn calculate_position_at<T: StaticOrbiter>(
&self,
- sgp: f64,
+ this_body: &T,
time: Second)
-> cgmath::Vector3<f64>
{
@@ -77,7 +83,7 @@ impl StaticOrbit
let arg_periaps = self.long_periapsis - long_asc_node;
let sma_cubed = self.semi_major_axis.powf(3.0);
- let mean_motion = (sgp / sma_cubed).sqrt();
+ let mean_motion = (this_body.sgp() / sma_cubed).sqrt();
let mean_anomaly_epoch = self.mean_long - self.long_periapsis;
let mean_anomaly = mean_anomaly_epoch + (mean_motion * time as f64);