summaryrefslogtreecommitdiffstats
path: root/src/solar_system/body.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/solar_system/body.rs')
-rw-r--r--src/solar_system/body.rs96
1 files changed, 96 insertions, 0 deletions
diff --git a/src/solar_system/body.rs b/src/solar_system/body.rs
new file mode 100644
index 0000000..55d3d4a
--- /dev/null
+++ b/src/solar_system/body.rs
@@ -0,0 +1,96 @@
+use super::*;
+
+pub type BodyId = usize;
+
+pub struct OrbitalBody
+{
+ id: BodyId,
+ name: String,
+ mass: Kilograms,
+ radius: Kilometers,
+ sgp: f64,
+
+ orbit: Option<StaticOrbit>,
+ rel_pos: Option<cgmath::Vector3<Kilometers>>
+}
+
+impl OrbitalBody
+{
+ pub fn new_from_record(
+ id: BodyId,
+ record: CSVOrbitalBody)
+ -> Self {
+ Self {
+ id: id,
+ name: record.name,
+ mass: record.mass,
+ radius: record.radius,
+ sgp: record.sgp,
+ orbit: match record.orbits {
+ Some(parent) => Some(StaticOrbit::new(
+ parent,
+ record.eccentricity,
+ record.inclination,
+ record.long_asc_node,
+ record.long_periapsis,
+ record.mean_long,
+ record.semi_major_axis
+ )),
+ None => None
+ },
+ rel_pos: None
+ }
+ }
+
+ 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 absolute_position(
+ &self,
+ solar_system: &SolarSystem)
+ -> 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()
+ },
+ None => self.relative_position()
+ }
+ }
+
+ pub fn get_orbit(&self) -> &Option<StaticOrbit> { &self.orbit }
+
+ 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)
+ }
+ }
+
+ pub fn update(
+ &mut self,
+ time: Second)
+ {
+ self.rel_pos = Some(self.calculate_orbit_at(time));
+ }
+}
+