Skip to content

Commit

Permalink
fix body rotation, rotation speed, improve spice file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Oct 7, 2024
1 parent b606ad3 commit 05238ad
Show file tree
Hide file tree
Showing 22 changed files with 220 additions and 275 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/.lapce
/.idea
/.fleet
/data/*
/wasm/assets
/wasm/Simulation.js
/wasm/Simulation_bg.wasm
Binary file modified assets/models/earth.glb
Binary file not shown.
2 changes: 1 addition & 1 deletion scenarios/earth_satellites.sim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"bodies":[{"children":[{"children":[{"children":[],"data":{"mass":450000.0,"starting_position":{"x":139697751.6768489,"y":-55457691.56188262,"z":26726.49883086234},"starting_velocity":{"x":8.512277308736511,"y":20.73621312430883,"z":-2.743211831320219},"name":"ISS","model_path":"iss.glb","diameter":0.1,"rotation_speed":0.0,"simulate":true,"naif_id":-1,"fixed_body_frame":{"target_id":-1,"orientation_id":-1},"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":null,"rotation_matrix":{"x":{"x":1.0,"y":0.0,"z":0.0},"y":{"x":0.0,"y":1.0,"z":0.0},"z":{"x":0.0,"y":0.0,"z":1.0}}}},{"children":[],"data":{"mass":10800.0,"starting_position":{"x":139708447.6237727,"y":-55461983.31379145,"z":33786.01008588448},"starting_velocity":{"x":13.95250317957649,"y":32.23801340370306,"z":-4.892445364940268},"name":"Hubble","model_path":"hubble.glb","diameter":0.01,"rotation_speed":0.0,"simulate":true,"naif_id":-1,"fixed_body_frame":{"target_id":-1,"orientation_id":-1},"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":null,"rotation_matrix":{"x":{"x":1.0,"y":0.0,"z":0.0},"y":{"x":0.0,"y":1.0,"z":0.0},"z":{"x":0.0,"y":0.0,"z":1.0}}}}],"data":{"mass":5.972e24,"starting_position":{"x":139703169.6031987,"y":-50894516.80238317,"z":-22031976.570581578},"starting_velocity":{"x":10.34755584668519,"y":25.363844384635943,"z":10.995351298282667},"name":"Earth","model_path":"earth.glb","diameter":12742.0,"rotation_speed":1436.0,"simulate":true,"naif_id":399,"fixed_body_frame":{"target_id":399,"orientation_id":399},"ellipsoid":{"semi_major_equatorial_radius_km":6378.1366,"semi_minor_equatorial_radius_km":6378.1366,"polar_radius_km":6356.7519},"light_source":null,"rotation_matrix":{"x":{"x":0.9411901831626892,"y":0.3378687798976898,"z":0.0023979549296200275},"y":{"x":-0.3378697335720062,"y":0.9411928653717041,"z":-6.617409326281631e-6},"z":{"x":-0.002259173896163702,"y":-0.000803968112450093,"z":0.9999971389770508}}}},{"children":[],"data":{"mass":0.0,"starting_position":{"x":0.0,"y":0.0,"z":0.0},"starting_velocity":{"x":0.0,"y":0.0,"z":0.0},"name":"Mars","model_path":"earth.glb","diameter":0.0,"rotation_speed":0.0,"simulate":true,"naif_id":499,"fixed_body_frame":{"target_id":499,"orientation_id":499},"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":null,"rotation_matrix":{"x":{"x":1.0,"y":0.0,"z":0.0},"y":{"x":0.0,"y":1.0,"z":0.0},"z":{"x":0.0,"y":0.0,"z":1.0}}}}],"data":{"mass":1.9885e30,"starting_position":{"x":-982171.6832391358,"y":-655394.8782055749,"z":28787.74341113935},"starting_velocity":{"x":0.01127558914848326,"y":-0.008532815916463004,"z":-0.0001656555243002107},"name":"Sun","model_path":"sun.glb","diameter":1392700.032,"rotation_speed":0.0,"simulate":true,"naif_id":-1,"fixed_body_frame":{"target_id":-1,"orientation_id":-1},"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":{"intensity":3.75e28,"range":9e30,"color":"#FFFFFF","enabled":true},"rotation_matrix":{"x":{"x":1.0,"y":0.0,"z":0.0},"y":{"x":0.0,"y":1.0,"z":0.0},"z":{"x":0.0,"y":0.0,"z":1.0}}}}],"data_sets":["pck11.pca","de440s.bsp"],"starting_time_millis":1725148800000,"title":"Earth Satellites","description":"A scenario with all major satellites orbiting the Earth.","scale":1e-7,"timestep":150}
{"bodies":[{"children":[{"children":[],"data":{"mass":0.0,"starting_position":{"x":149676047.62437057,"y":145468789.39744163,"z":62705286.14179747},"starting_velocity":{"x":-16.660751062419582,"y":16.85519821471999,"z":8.180891832173366},"name":"Mars","model_path":"mars.glb","diameter":0.0,"rotation_speed":0.0,"simulate":true,"naif_id":4,"fixed_body_frame":{"target_id":499,"orientation_id":499},"ellipsoid":{"semi_major_equatorial_radius_km":3396.19,"semi_minor_equatorial_radius_km":3396.19,"polar_radius_km":3376.2},"light_source":null,"rotation_matrix":{"x":{"x":0.4554736018180847,"y":0.869995653629303,"z":0.18881569802761078},"y":{"x":-0.7704004645347595,"y":0.2789040207862854,"z":0.5733198523521423},"z":{"x":0.4461243450641632,"y":-0.40659576654434204,"z":0.7972784638404846}}}},{"children":[{"children":[],"data":{"mass":450000.0,"starting_position":{"x":139697751.6768489,"y":-55457691.56188262,"z":26726.49883086234},"starting_velocity":{"x":8.512277308736511,"y":20.73621312430883,"z":-2.743211831320219},"name":"ISS","model_path":"iss.glb","diameter":0.1,"rotation_speed":0.0,"simulate":true,"naif_id":-1,"fixed_body_frame":{"target_id":-1,"orientation_id":-1},"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":null,"rotation_matrix":{"x":{"x":1.0,"y":0.0,"z":0.0},"y":{"x":0.0,"y":1.0,"z":0.0},"z":{"x":0.0,"y":0.0,"z":1.0}}}},{"children":[],"data":{"mass":10800.0,"starting_position":{"x":139708447.6237727,"y":-55461983.31379145,"z":33786.01008588448},"starting_velocity":{"x":13.95250317957649,"y":32.23801340370306,"z":-4.892445364940268},"name":"Hubble","model_path":"hubble.glb","diameter":0.01,"rotation_speed":0.0,"simulate":true,"naif_id":-1,"fixed_body_frame":{"target_id":-1,"orientation_id":-1},"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":null,"rotation_matrix":{"x":{"x":1.0,"y":0.0,"z":0.0},"y":{"x":0.0,"y":1.0,"z":0.0},"z":{"x":0.0,"y":0.0,"z":1.0}}}}],"data":{"mass":5.972e24,"starting_position":{"x":139703169.6031987,"y":-50894516.80238317,"z":-22031976.570581574},"starting_velocity":{"x":10.34755584668519,"y":25.363844384635943,"z":10.995351298282669},"name":"Earth","model_path":"earth.glb","diameter":12742.0,"rotation_speed":1436.0,"simulate":true,"naif_id":399,"fixed_body_frame":{"target_id":399,"orientation_id":399},"ellipsoid":{"semi_major_equatorial_radius_km":6378.1366,"semi_minor_equatorial_radius_km":6378.1366,"polar_radius_km":6356.7519},"light_source":null,"rotation_matrix":{"x":{"x":0.9411901831626892,"y":-0.3378697335720062,"z":-0.002259173896163702},"y":{"x":0.3378687798976898,"y":0.9411928653717041,"z":-0.000803968112450093},"z":{"x":0.0023979549296200275,"y":-6.617409326281631e-6,"z":0.9999971389770508}}}}],"data":{"mass":1.9885e30,"starting_position":{"x":-982171.6832391358,"y":-655394.8782055749,"z":28787.74341113935},"starting_velocity":{"x":0.01127558914848326,"y":-0.008532815916463004,"z":-0.0001656555243002107},"name":"Sun","model_path":"sun.glb","diameter":1392700.032,"rotation_speed":0.0,"simulate":true,"naif_id":-1,"fixed_body_frame":{"target_id":-1,"orientation_id":-1},"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":{"intensity":3.75e28,"range":9e30,"color":"#FFFFFF","enabled":true},"rotation_matrix":{"x":{"x":1.0,"y":0.0,"z":0.0},"y":{"x":0.0,"y":1.0,"z":0.0},"z":{"x":0.0,"y":0.0,"z":1.0}}}}],"data_sets":["pck11.pca","de440s.bsp"],"starting_time_millis":1725148800000,"title":"Earth Satellites","description":"A scenario with all major satellites orbiting the Earth.","scale":1e-7,"timestep":150}
2 changes: 1 addition & 1 deletion scenarios/solar_system.sim

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/simulation/asset/serialization.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::simulation::asset::default_values::*;
use anise::structure::planetocentric::ellipsoid::Ellipsoid;
use bevy::asset::io::file::FileAssetReader;
use bevy::asset::io::{AssetSource, AssetSourceBuilder, AssetSourceId, Reader};
Expand All @@ -9,7 +10,6 @@ use bevy::{
prelude::Plugin, reflect::TypePath, utils::BoxedFuture,
};
use serde::{Deserialize, Serialize};
use crate::simulation::asset::default_values::*;

#[derive(Debug, Deserialize, Serialize, TypePath, Asset, Clone)]
pub struct SimulationData {
Expand Down Expand Up @@ -99,7 +99,6 @@ pub struct SerializedBodyData {
pub starting_velocity: SerializedVec,
pub name: String,
pub model_path: String,
pub diameter: f64,
pub rotation_speed: f64,
pub simulate: bool,
#[serde(default = "default_id")]
Expand Down
37 changes: 21 additions & 16 deletions src/simulation/components/anise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ use crate::simulation::scenario::setup::ScenarioData;
use crate::simulation::ui::editor_body_panel::EditorPanelState;
use crate::simulation::ui::toast::{error_toast, success_toast, ToastContainer};
use crate::simulation::{SimState, SimStateType};
use anise::constants::frames::{IAU_EARTH_FRAME, JUPITER_BARYCENTER_J2000, SSB_J2000};
use anise::constants::orientations::{IAU_EARTH, J2000};
use anise::constants::frames::SSB_J2000;
use anise::constants::orientations::J2000;
use anise::errors::AlmanacError;
use anise::math::cartesian::CartesianState;
use anise::math::Vector3;
use anise::prelude::{Almanac, Epoch, Frame, SPK};
use anise::structure::planetocentric::ellipsoid::Ellipsoid;
use anise::prelude::{Almanac, Epoch, Frame};
use bevy::app::Plugin;
use bevy::math::DVec3;
use bevy::prelude::{in_state, IntoSystemConfigs, Local, OnEnter, Quat, Query, Res, ResMut, Resource, State, Update};
use bevy::prelude::{in_state, IntoSystemConfigs, Local, Name, Query, Res, ResMut, Resource, Update};
use bevy_async_task::{AsyncTaskRunner, AsyncTaskStatus};
use reqwest::get;

pub struct AnisePlugin;

Expand All @@ -39,15 +36,15 @@ impl Default for AlmanacHolder {

pub fn retrieve_starting_data(
selected_entity: Res<SelectedEntity>,
mut bodies: Query<&mut AniseMetadata>,
mut bodies: Query<(&mut AniseMetadata, &Name)>,
almanac: Res<AlmanacHolder>,
mut e_state: ResMut<EditorPanelState>,
scenario: Res<ScenarioData>,
mut toasts: ResMut<ToastContainer>
) {
// Define an Epoch in the dynamical barycentric time scale
let epoch = Epoch::from_unix_milliseconds(scenario.starting_time_millis as f64);
let mut metadata = selected_entity.entity.map(|e| bodies.get_mut(e).ok()).flatten().unwrap();
let (mut metadata, name) = selected_entity.entity.map(|e| bodies.get_mut(e).ok()).flatten().unwrap();
let state = almanac.0
.translate(
Frame::new(metadata.ephemeris_id, J2000), // Target
Expand All @@ -56,11 +53,11 @@ pub fn retrieve_starting_data(
None,
);
if let Ok(s) = state {
toasts.0.add(success_toast(&format!("Retrieved data for {}", metadata.ephemeris_id)));
toasts.0.add(success_toast(&format!("Retrieved data for {}", name)));
e_state.new_velocity = vector3_to_dvec3(s.velocity_km_s);
e_state.new_position = vector3_to_dvec3(s.radius_km);
} else {
toasts.0.add(error_toast(format!("Error: {:?}", state.unwrap_err()).as_str()));
toasts.0.add(error_toast(format!("Couldn't retrieve position and velocity: {:?}", state.unwrap_err()).as_str()));
}

let fixed_frame = Frame::new(metadata.target_id, metadata.orientation_id);
Expand All @@ -69,15 +66,18 @@ pub fn retrieve_starting_data(
if let Ok(f) = full_frame {
e_state.ellipsoid = f.shape.unwrap_or(e_state.ellipsoid);
} else {
toasts.0.add(error_toast(format!("Error: {:?}", full_frame.unwrap_err()).as_str()));
toasts.0.add(error_toast(format!("Couldn't retrieve shape: {:?}", full_frame.unwrap_err()).as_str()));
}

let dcm = almanac.0.rotation_to_parent(fixed_frame, epoch);

let dcm = almanac.0.rotate(
fixed_frame,
SSB_J2000,
epoch,
);
if let Ok(d) = dcm {
e_state.rotation_matrix = matrix3_to_mat3(d.rot_mat);
} else {
toasts.0.add(error_toast(format!("Error: {:?}", dcm.unwrap_err()).as_str()));
toasts.0.add(error_toast(format!("Couldn't retrieve rotation: {:?}", dcm.unwrap_err()).as_str()));
}
}

Expand All @@ -99,11 +99,16 @@ fn spk_file_loading(
mut scenario_data: ResMut<ScenarioData>,
mut loading_state: ResMut<LoadingState>,
mut task_executor: AsyncTaskRunner<Result<Almanac, AlmanacError>>,
mut to_load: Local<Option<Vec<String>>>
mut to_load: Local<Option<Vec<String>>>,
sim_type: Res<SimStateType>
) {
if loading_state.loaded_spice_files || !loading_state.spawned_bodies {
return;
}
if *sim_type != SimStateType::Editor {
loading_state.loaded_spice_files = true;
return;
}
if to_load.is_none() {
*to_load = Some(scenario_data.spice_files.clone());
loading_state.spice_total = to_load.as_ref().unwrap().len() as i32;
Expand Down
15 changes: 7 additions & 8 deletions src/simulation/components/billboard.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use bevy::app::{App, Plugin};
use bevy::math::Vec3;
use bevy::prelude::{Children, in_state, IntoSystemConfigs, Query, Res, Resource, Transform, Update, Visibility, With, Without, Has, Name, Window, Camera, GlobalTransform, Vec2, Projection, PerspectiveProjection, Entity, Gizmos, Srgba};
use bevy::render::camera::CameraProjection;
use bevy::prelude::{in_state, Camera, Children, Entity, Gizmos, GlobalTransform, Has, IntoSystemConfigs, Name, Query, Res, Resource, Transform, Update, Vec2, Visibility, With, Without};
use bevy::text::Text;
use bevy::utils::HashMap;
use bevy_mod_billboard::text::BillboardTextBounds;

use crate::simulation::components::apsis::ApsisBody;
use crate::simulation::components::body::{Diameter, Moon, Planet, Star, BillboardVisible, BodyParent};
use crate::simulation::components::camera::{pan_orbit_camera, PanOrbitCamera};
use crate::simulation::components::body::{BillboardVisible, BodyParent, BodyShape, Moon, Planet, Star};
use crate::simulation::components::camera::pan_orbit_camera;
use crate::simulation::components::scale::SimulationScale;
use crate::simulation::SimState;
use crate::simulation::render::star_billboard::STAR_IMPOSTER_DIVIDER;
use crate::simulation::SimState;

const STAR_VISIBILITY_THRESHOLD: f32 = 40_000_000.0; //if the camera's radius is less than this, stars' names will be hidden
const PLANET_VISIBILITY_THRESHOLD: f32 = 1700.0; //if the camera's radius is less than this, planets' names will be hidden
Expand Down Expand Up @@ -45,7 +44,7 @@ impl Default for BillboardSettings {
}

fn auto_scale_billboards(
mut bodies: Query<(Entity, &Name, &Children, &Transform, &Diameter, &mut BillboardVisible, Option<&ApsisBody>, Has<Planet>, Has<Star>, Option<&BodyParent>), Without<Text>>,
mut bodies: Query<(Entity, &Name, &Children, &Transform, &BodyShape, &mut BillboardVisible, Option<&ApsisBody>, Has<Planet>, Has<Star>, Option<&BodyParent>), Without<Text>>,
mut billboards: Query<(&Text, &mut Transform, &mut Visibility), With<BillboardTextBounds>>,
camera: Query<(&Transform, &GlobalTransform, &Camera), (Without<BillboardTextBounds>, Without<Planet>, Without<Moon>, Without<Star>)>,
settings: Res<BillboardSettings>,
Expand All @@ -63,7 +62,7 @@ fn auto_scale_billboards(
for (entity, n, _, transform, _, _, _, _, _, p) in &mut bodies {
parent_pos.insert(entity, transform.translation.clone());
}
for (_, name, children, p_transform, diameter, mut billboard_visible, apsis, planet, star, p) in bodies.iter_mut() {
for (_, name, children, p_transform, shape, mut billboard_visible, apsis, planet, star, p) in bodies.iter_mut() {
let mut predicate = true;
if p.is_some() {
let parent_transform = parent_pos.get(&p.unwrap().0).unwrap_or(&Vec3::ZERO);
Expand All @@ -76,7 +75,7 @@ fn auto_scale_billboards(
let offset = if star {
distance_to_cam
} else {
diameter.num * scale.0
shape.ellipsoid.mean_equatorial_radius_km() as f32 * 2. * scale.0
};
billboard_visible.0 = !settings.dynamic_hide || predicate;
billboard(
Expand Down
23 changes: 8 additions & 15 deletions src/simulation/components/body.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::simulation::asset::serialization::SerializedBody;
use crate::simulation::components::horizons::AniseMetadata;
use anise::structure::planetocentric::ellipsoid::Ellipsoid;
use bevy::color::palettes::css;
use bevy::color::Color;
use bevy::core::Name;
use bevy::math::{DVec3, Mat3, Vec3};
use bevy::prelude::{default, Bundle, Component, Entity, Handle, Reflect, Scene, Srgba, Transform};
use bevy::render::primitives::Aabb;
use std::collections::VecDeque;
use anise::structure::planetocentric::ellipsoid::Ellipsoid;
use crate::simulation::components::horizons::AniseMetadata;

#[derive(Component, Clone, Default, Reflect, Copy)]
pub struct Mass(pub f64);
Expand All @@ -18,9 +18,6 @@ pub struct Velocity(pub DVec3);
#[derive(Default, Component, Reflect, Clone, Copy)]
pub struct Acceleration(pub DVec3);

#[derive(Component, Reflect, Clone, Default, Copy)]
pub struct Scale(pub f32);

#[derive(Component, Reflect, Clone, Default, Copy)]
pub struct RotationSpeed(pub f64);

Expand Down Expand Up @@ -84,19 +81,18 @@ impl Default for OrbitSettings {
pub struct SimPosition(pub DVec3);

#[derive(Component, Clone)]
pub struct Diameter {
pub struct BodyShape {

pub num: f32,
pub applied: bool,
pub ellipsoid: Ellipsoid,
pub path: String,

}

impl Default for Diameter {
impl Default for BodyShape {

fn default() -> Self {
Diameter { num: 0.0, applied: false, ellipsoid: Ellipsoid::from_sphere(1.0), path: "".to_string() }
BodyShape { applied: false, ellipsoid: Ellipsoid::from_sphere(1.0), path: "".to_string() }
}

}
Expand Down Expand Up @@ -132,13 +128,12 @@ pub struct BodyBundle {
pub sim_position: SimPosition,
pub vel: Velocity,
pub acc: Acceleration,
pub scale: Scale,
pub name: Name,
pub model_path: ModelPath,
pub orbit: OrbitSettings,
pub rotation_speed: RotationSpeed,
pub rotation: BodyRotation,
pub diameter: Diameter,
pub diameter: BodyShape,
pub billboard_visible: BillboardVisible,
pub naif_id: AniseMetadata,

Expand All @@ -153,8 +148,7 @@ impl From<SerializedBody> for BodyBundle {
vel: Velocity(DVec3::from(value.data.starting_velocity) * 1000.0),
name: Name::new(value.data.name),
model_path: ModelPath(format!("models/{}#Scene0", value.data.model_path)),
diameter: Diameter {
num: (value.data.diameter * 1000.0) as f32,
diameter: BodyShape {
path: value.data.model_path,
ellipsoid: value.data.ellipsoid,
..default()
Expand Down Expand Up @@ -184,8 +178,7 @@ impl BodyBundle {
vel: Velocity(DVec3::ZERO),
name: Name::new(format!("New body {}", index)),
model_path: ModelPath("models/earth.glb#Scene0".to_string()),
diameter: Diameter {
num: 0.0,
diameter: BodyShape {
..default()
},
rotation: BodyRotation {
Expand Down
Loading

0 comments on commit 05238ad

Please sign in to comment.