From 961f8c6d405c9c6fcf9aaf4fb6f199b0e5c60d88 Mon Sep 17 00:00:00 2001 From: Jon Santmyer Date: Thu, 30 Apr 2026 10:06:32 -0400 Subject: add orbit rendering for bodies --- src/solar_system.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/solar_system.rs') diff --git a/src/solar_system.rs b/src/solar_system.rs index c98a0e8..cf6a36a 100644 --- a/src/solar_system.rs +++ b/src/solar_system.rs @@ -1,6 +1,6 @@ use serde::{Deserialize}; use crate::{GameState, known_stars::{KNOWN_STARS, StarNotFoundError}, timeman::{Second, TimeMan}}; -use std::{cell::RefCell, error::Error}; +use std::{cell::RefCell, error::Error, f64::consts::PI}; const GRAVITATIONAL_CONSTANT: f64 = 6.67408e-20; @@ -33,11 +33,13 @@ pub struct SerialOrbitalBody pub struct OrbitalBody { body: SerialOrbitalBody, + id: BodyId, position: Option> } pub struct SolarSystem { + id: SystemId, name: String, bodies: Vec, time: Option @@ -46,6 +48,7 @@ pub struct SolarSystem impl SolarSystem { pub fn new_from_csv( + id: SystemId, data: &'static str) -> Result> { let data_reader = stringreader::StringReader::new(data); @@ -68,11 +71,13 @@ impl SolarSystem bodies.push(OrbitalBody { body: record, + id: bodies.len(), position: None }); } Ok(Self { + id: id, name: bodies[0].name().clone(), bodies: bodies, time: None @@ -80,15 +85,18 @@ impl SolarSystem } pub fn new_from_known_star( + id: SystemId, star: &'static str) -> Result> { let star_csv = match KNOWN_STARS.get(star).copied() { Some(csv) => csv, None => return Err(Box::new(StarNotFoundError { star: star })) }; - SolarSystem::new_from_csv(star_csv) + SolarSystem::new_from_csv(id, star_csv) } + pub fn id(&self) -> SystemId { self.id } + pub fn name(&self) -> &String { &self.name } pub fn bodies(&self) @@ -140,18 +148,24 @@ impl SolarSystem impl OrbitalBody { + pub fn id(&self) -> BodyId { self.id } pub fn name(&self) -> &String { &self.body.name } pub fn radius(&self) -> f32 { self.body.radius as f32 } pub fn position(&self) -> cgmath::Vector3 { self.position.unwrap() } + pub fn does_orbit(&self) -> bool { self.body.orbits.is_some() } + pub fn get_orbits(&self) -> Option { self.body.orbits } - fn calculate_orbit_at( + pub fn orbital_period(&self) -> Second + { ((self.body.semi_major_axis.powf(3.0) / self.body.sgp).sqrt() * std::f64::consts::TAU) as u64 } + + pub fn calculate_orbit_at( &self, time: Second) -> cgmath::Vector3 { if self.body.orbits.is_none() { return cgmath::Vector3::::new(0.0, 0.0, 0.0); - } +} let eccentricity = self.body.eccentricity; -- cgit v1.2.3