summaryrefslogtreecommitdiffstats
path: root/src/solar_system.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/solar_system.rs')
-rw-r--r--src/solar_system.rs41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/solar_system.rs b/src/solar_system.rs
index 6730719..c98a0e8 100644
--- a/src/solar_system.rs
+++ b/src/solar_system.rs
@@ -1,9 +1,8 @@
-use cgmath::Point3;
use serde::{Deserialize};
-use crate::{GameState, known_stars::{KNOWN_STARS, StarNotFoundError}, timeman::Second};
+use crate::{GameState, known_stars::{KNOWN_STARS, StarNotFoundError}, timeman::{Second, TimeMan}};
use std::{cell::RefCell, error::Error};
-const GRAVITATIONAL_CONSTANT: f64 = 6.67408e-11;
+const GRAVITATIONAL_CONSTANT: f64 = 6.67408e-20;
pub type Kilograms = f64;
pub type Kilometers = f64;
@@ -34,7 +33,7 @@ pub struct SerialOrbitalBody
pub struct OrbitalBody
{
body: SerialOrbitalBody,
- position: Option<cgmath::Point3<Kilometers>>
+ position: Option<cgmath::Vector3<Kilometers>>
}
pub struct SolarSystem
@@ -60,14 +59,17 @@ impl SolarSystem
match record.orbits {
Some(orbits) => {
if record.sgp == 0.0 {
- record.sgp = bodies[orbits].body.mass + record.mass * GRAVITATIONAL_CONSTANT;
+ record.sgp = (bodies[orbits].body.mass + record.mass) * GRAVITATIONAL_CONSTANT;
}
}
None => {}
}
println!("New body: {:?}", record);
- bodies.push(OrbitalBody { body: record, position: None });
+ bodies.push(OrbitalBody {
+ body: record,
+ position: None
+ });
}
Ok(Self {
@@ -105,6 +107,21 @@ impl SolarSystem
self.time = Some(time);
}
+ pub fn body_position(
+ &self,
+ body: &OrbitalBody)
+ -> cgmath::Vector3<Kilometers>
+ {
+ match body.body.orbits {
+ Some(parent) => {
+ let body_pos = body.position();
+ let parent_pos = self.bodies[parent].position();
+ body_pos + parent_pos
+ },
+ None => body.position()
+ }
+ }
+
pub fn update(
&mut self,
time: Second)
@@ -125,15 +142,15 @@ impl OrbitalBody
{
pub fn name(&self) -> &String { &self.body.name }
pub fn radius(&self) -> f32 { self.body.radius as f32 }
- pub fn position(&self) -> Point3<f64> { self.position.unwrap() }
+ pub fn position(&self) -> cgmath::Vector3<f64> { self.position.unwrap() }
fn calculate_orbit_at(
&self,
time: Second)
- -> Point3<f64>
+ -> cgmath::Vector3<f64>
{
if self.body.orbits.is_none() {
- return Point3::<f64>::new(0.0, 0.0, 0.0);
+ return cgmath::Vector3::<f64>::new(0.0, 0.0, 0.0);
}
let eccentricity = self.body.eccentricity;
@@ -141,8 +158,8 @@ impl OrbitalBody
let long_asc_node = self.body.long_asc_node;
let arg_periaps = self.body.long_periapsis - long_asc_node;
- let sma_cubed = self.body.semi_major_axis.powf(3.0);
- let mean_motion = (self.body.sgp / sma_cubed.abs()).sqrt();
+ let sma_cubed = self.body.semi_major_axis.powf(3.0);
+ let mean_motion = (self.body.sgp / sma_cubed).sqrt();
let mean_anomaly_epoch = self.body.mean_long - self.body.long_periapsis;
let mean_anomaly = mean_anomaly_epoch + (mean_motion * time as f64);
@@ -179,7 +196,7 @@ impl OrbitalBody
let y = r * i_sin * vw_sin;
let z = r * (omega_sin * vw_cos + omega_cos * vw_sin * i_cos);
- Point3::<f64>::new(x, y, z)
+ cgmath::Vector3::<f64>::new(x, y, z)
}
}