1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
use crate::timeman;
use super::*;
pub type BodyId = usize;
pub const BODY_TICK_DURATION: Second = timeman::DAY;
pub struct OrbitalBody
{
id: BodyId,
name: String,
mass: Kilograms,
radius: Kilometers,
sgp: f64,
orbit: Option<StaticOrbit>,
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)
-> 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 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,
time: Second)
-> cgmath::Vector3<Kilometers>
{
match &self.orbit {
Some(orbit) => {
let parent_pos = solar_system.bodies()[orbit.parent()].absolute_position(solar_system, time);
parent_pos + self.relative_position(time)
},
None => self.relative_position(time)
}
}
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)
}
}
}
|