Skip to content

Commit

Permalink
work on shapes, rotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Oct 6, 2024
1 parent 8dfd82d commit 89820ef
Show file tree
Hide file tree
Showing 17 changed files with 304 additions and 121 deletions.
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,"axial_tilt":0.0,"simulate":true,"horizons_id":-1,"light_source":null}},{"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,"axial_tilt":0.0,"simulate":true,"horizons_id":-48,"light_source":null}}],"data":{"mass":5.972e24,"starting_position":{"x":139702454.0,"y":-55460536.0,"z":30732.9},"starting_velocity":{"x":10.34793,"y":27.64442411692169,"z":-0.001120445580937712},"name":"Earth","model_path":"earth_4k_2.glb","diameter":12742.0,"rotation_speed":1436.0,"axial_tilt":23.44,"simulate":true,"horizons_id":399,"light_source":null}}],"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,"axial_tilt":0.0,"simulate":true,"horizons_id":-1,"light_source":{"intensity":3.75e28,"range":9e30,"color":"#FFFFFF","enabled":true}}}],"starting_time_millis":1725148800000,"title":"Earth Satellites","description":"A scenario with all major satellites orbiting the Earth.","scale":1.0,"timestep":150}
{"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.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.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}}}}],"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.

2 changes: 1 addition & 1 deletion scenarios/solar_system_copy.sim
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"bodies":[{"children":[{"children":[{"children":[],"data":{"mass":7.34767e22,"starting_position":{"x":132652620.33908576,"y":59288362.34723856,"z":25700653.622645397},"starting_velocity":{"x":-12.522181069822803,"y":24.478434326887943,"z":10.569978575134488},"name":"Moon","model_path":"moon.glb","diameter":3744.8,"rotation_speed":0.0,"axial_tilt":0.0,"simulate":true,"gravity_param":0.0,"naif_id":301,"orientation_id":301,"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":null}}],"data":{"mass":5.97219e24,"starting_position":{"x":132661907.41300352,"y":59622737.91959965,"z":25879893.321551036},"starting_velocity":{"x":-13.555126596660894,"y":24.447022096986885,"z":10.59802290045933},"name":"Earth","model_path":"earth.glb","diameter":12742.0,"rotation_speed":1436.0,"axial_tilt":23.439281,"simulate":true,"gravity_param":0.0,"naif_id":399,"orientation_id":399,"ellipsoid":{"semi_major_equatorial_radius_km":1.0,"semi_minor_equatorial_radius_km":1.0,"polar_radius_km":1.0},"light_source":null}},{"children":[],"data":{"mass":6.4171e23,"starting_position":{"x":-179797351.86843538,"y":-141545418.16021723,"z":-60062982.27940703},"starting_velocity":{"x":16.704581229673664,"y":-14.67524995138464,"z":-7.181472156593413},"name":"Mars","model_path":"mars.glb","diameter":6779.0,"rotation_speed":1476.0,"axial_tilt":25.19,"simulate":true,"gravity_param":0.0,"naif_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}}],"data":{"mass":1.9885e30,"starting_position":{"x":-1241873.683433827,"y":-337542.17983616004,"z":-111583.58952210216},"starting_velocity":{"x":0.0,"y":0.0,"z":0.0},"name":"Sol","model_path":"sun.glb","diameter":1492000.0,"rotation_speed":38880.0,"axial_tilt":7.25,"simulate":true,"gravity_param":132712440041.93938,"naif_id":10,"orientation_id":10,"ellipsoid":{"semi_major_equatorial_radius_km":695700.0,"semi_minor_equatorial_radius_km":695700.0,"polar_radius_km":695700.0},"light_source":{"intensity":3.75e28,"range":4436820000000.0,"color":"#FFFFFF","enabled":true}}}],"data_sets":["de440s.bsp","sat441.bsp","pck11.pca"],"starting_time_millis":1697760000000,"title":"Solar System Copy","description":"","scale":1e-7,"timestep":900}
{"bodies":[{"children":[{"children":[{"children":[],"data":{"mass":7.34767e22,"starting_position":{"x":132652620.33908576,"y":59288362.34723856,"z":25700653.622645397},"starting_velocity":{"x":-12.522181069822803,"y":24.478434326887943,"z":10.569978575134488},"name":"Moon","model_path":"moon.glb","diameter":3744.8,"rotation_speed":0.0,"simulate":true,"naif_id":301,"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.97219e24,"starting_position":{"x":132661907.41300352,"y":59622737.91959965,"z":25879893.321551036},"starting_velocity":{"x":-13.555126596660894,"y":24.447022096986885,"z":10.59802290045933},"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.8844501972198486,"y":-0.46662887930870056,"z":0.002313583390787244},"y":{"x":0.46663013100624084,"y":0.8844525218009949,"z":-6.15993440078455e-6},"z":{"x":-0.0020433804020285606,"y":0.0010850358521565795,"z":0.9999973177909851}}}},{"children":[],"data":{"mass":6.4171e23,"starting_position":{"x":-179797351.86843538,"y":-141545418.16021723,"z":-60062982.27940703},"starting_velocity":{"x":16.704581229673664,"y":-14.67524995138464,"z":-7.181472156593413},"name":"Mars","model_path":"mars.glb","diameter":6779.0,"rotation_speed":1476.0,"simulate":true,"naif_id":499,"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.35471034049987793,"y":-0.8216767311096191,"z":0.44612547755241394},"y":{"x":0.898221492767334,"y":0.16700273752212524,"z":-0.4065811038017273},"z":{"x":0.2595740556716919,"y":0.5449380278587341,"z":0.7972853183746338}}}}],"data":{"mass":1.9885e30,"starting_position":{"x":-1241873.683433827,"y":-337542.17983616004,"z":-111583.58952210216},"starting_velocity":{"x":0.0,"y":0.0,"z":0.0},"name":"Sol","model_path":"sun.glb","diameter":1492000.0,"rotation_speed":38880.0,"simulate":true,"naif_id":10,"fixed_body_frame":{"target_id":-1,"orientation_id":-1},"ellipsoid":{"semi_major_equatorial_radius_km":695700.0,"semi_minor_equatorial_radius_km":695700.0,"polar_radius_km":695700.0},"light_source":{"intensity":3.75e28,"range":4436820000000.0,"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":["de440s.bsp","sat441.bsp","pck11.pca"],"starting_time_millis":1697760000000,"title":"Solar System Copy","description":"","scale":1e-7,"timestep":900}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use bevy::render::RenderPlugin;
use bevy::window::{PresentMode, Window, WindowPlugin};
use bevy::DefaultPlugins;
use bevy_egui::EguiPlugin;
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use bevy_mod_billboard::plugin::BillboardPlugin;
use crate::menu::MenuPlugin;
use simulation::scenario::setup::SetupPlugin;
Expand Down Expand Up @@ -75,10 +76,10 @@ fn main() {
..default()
})
)
// .add_plugins(WorldInspectorPlugin::default())
.add_plugins(WorldInspectorPlugin::default())
.add_plugins(EditorPlugin)
.add_plugins(SimulationPlugin)
.add_plugins(EguiPlugin)
// .add_plugins(EguiPlugin)
.add_plugins(SerializationPlugin)
.add_plugins(BillboardPlugin)
.add_plugins(SetupPlugin)
Expand Down
26 changes: 26 additions & 0 deletions src/simulation/asset/default_values.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anise::structure::planetocentric::ellipsoid::Ellipsoid;
use bevy::prelude::Mat3;
use crate::simulation::asset::serialization::{SerializedFixedBodyFrame, SerializedMat3};

pub fn default_id() -> i32 {
-1
}

pub fn default_frame() -> SerializedFixedBodyFrame {
SerializedFixedBodyFrame {
target_id: default_id(),
orientation_id: default_id()
}
}

pub fn default_spk() -> Vec<String> {
Vec::new()
}

pub fn default_ellipsoid() -> Ellipsoid {
Ellipsoid::from_sphere(1.0)
}

pub fn default_rot_matrix() -> SerializedMat3 {
SerializedMat3::from(Mat3::IDENTITY)
}
1 change: 1 addition & 0 deletions src/simulation/asset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bevy::asset::AssetPath;
use bevy::asset::io::AssetSourceId;

pub mod serialization;
mod default_values;

pub const SCENARIO_ASSET_SOURCE: &str = "scenarios";

Expand Down
62 changes: 47 additions & 15 deletions src/simulation/asset/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use anise::structure::planetocentric::ellipsoid::Ellipsoid;
use bevy::asset::io::file::FileAssetReader;
use bevy::asset::io::{AssetSource, AssetSourceBuilder, AssetSourceId, Reader};
use bevy::asset::AsyncReadExt;
use bevy::prelude::{Asset, AssetApp};
use bevy::prelude::{Asset, AssetApp, Mat3, Vec3};
use bevy::{
asset::{AssetLoader, LoadContext},
math::DVec3,
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 All @@ -35,6 +36,33 @@ pub struct SerializedVec {
pub z: f64
}

#[derive(Debug, Deserialize, Serialize, TypePath, Clone, Copy)]
pub struct SerializedMat3 {
pub x: SerializedVec,
pub y: SerializedVec,
pub z: SerializedVec
}

impl From<Mat3> for SerializedMat3 {

fn from(value: Mat3) -> Self {
SerializedMat3 {
x: SerializedVec::from(value.x_axis.as_dvec3()),
y: SerializedVec::from(value.y_axis.as_dvec3()),
z: SerializedVec::from(value.z_axis.as_dvec3())
}
}

}

impl From<SerializedMat3> for Mat3 {

fn from(value: SerializedMat3) -> Self {
Mat3::from_cols(value.x.into(), value.y.into(), value.z.into())
}

}

impl From<SerializedVec> for DVec3 {

fn from(value: SerializedVec) -> Self {
Expand All @@ -43,6 +71,15 @@ impl From<SerializedVec> for DVec3 {

}

impl From<SerializedVec> for Vec3 {

fn from(value: SerializedVec) -> Self {
DVec3::new(value.x, value.y, value.z).as_vec3()
}

}


impl From<DVec3> for SerializedVec {

fn from(value: DVec3) -> Self {
Expand All @@ -64,27 +101,22 @@ pub struct SerializedBodyData {
pub model_path: String,
pub diameter: f64,
pub rotation_speed: f64,
pub axial_tilt: f32,
pub simulate: bool,
#[serde(default = "default_id")]
pub naif_id: i32,
#[serde(default = "default_id")]
pub orientation_id: i32,
#[serde(default = "default_frame")]
pub fixed_body_frame: SerializedFixedBodyFrame,
#[serde(default = "default_ellipsoid")]
pub ellipsoid: Ellipsoid,
pub light_source: Option<SerializedLightSource>
}

fn default_id() -> i32 {
-1
pub light_source: Option<SerializedLightSource>,
#[serde(default = "default_rot_matrix")]
pub rotation_matrix: SerializedMat3
}

fn default_spk() -> Vec<String> {
Vec::new()
}

fn default_ellipsoid() -> Ellipsoid {
Ellipsoid::from_sphere(1.0)
#[derive(Debug, Serialize, Deserialize, TypePath, Clone)]
pub struct SerializedFixedBodyFrame {
pub target_id: i32,
pub orientation_id: i32
}

#[derive(Debug, Serialize, Deserialize, TypePath, Clone)]
Expand Down
22 changes: 19 additions & 3 deletions src/simulation/components/anise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use anise::prelude::{Almanac, Epoch, Frame, SPK};
use anise::structure::planetocentric::ellipsoid::Ellipsoid;
use bevy::app::Plugin;
use bevy::math::DVec3;
use bevy::prelude::{in_state, IntoSystemConfigs, Local, OnEnter, Query, Res, ResMut, Resource, State, Update};
use bevy::prelude::{in_state, IntoSystemConfigs, Local, OnEnter, Quat, Query, Res, ResMut, Resource, State, Update};
use bevy_async_task::{AsyncTaskRunner, AsyncTaskStatus};
use reqwest::get;

Expand Down Expand Up @@ -50,13 +50,13 @@ pub fn retrieve_starting_data(
let mut metadata = selected_entity.entity.map(|e| bodies.get_mut(e).ok()).flatten().unwrap();
let state = almanac.0
.translate(
Frame::new(metadata.target_id, J2000), // Target
Frame::new(metadata.ephemeris_id, J2000), // Target
SSB_J2000, // Observer
epoch,
None,
);
if let Ok(s) = state {
toasts.0.add(success_toast(&format!("Retrieved data for {}", metadata.target_id)));
toasts.0.add(success_toast(&format!("Retrieved data for {}", metadata.ephemeris_id)));
e_state.new_velocity = vector3_to_dvec3(s.velocity_km_s);
e_state.new_position = vector3_to_dvec3(s.radius_km);
} else {
Expand All @@ -70,6 +70,22 @@ pub fn retrieve_starting_data(
} else {
toasts.0.add(error_toast(format!("Error: {:?}", full_frame.unwrap_err()).as_str()));
}

let dcm = almanac.0.rotation_to_parent(Frame::new(metadata.target_id, metadata.orientation_id), 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()));
}
}

fn matrix3_to_mat3(m: anise::math::Matrix3) -> bevy::math::Mat3 {
bevy::math::Mat3::from_cols(
bevy::math::Vec3::new(m.data.0[0][0] as f32, m.data.0[0][1] as f32, m.data.0[0][2] as f32),
bevy::math::Vec3::new(m.data.0[1][0] as f32, m.data.0[1][1] as f32, m.data.0[1][2] as f32),
bevy::math::Vec3::new(m.data.0[2][0] as f32, m.data.0[2][1] as f32, m.data.0[2][2] as f32)
)
}

fn vector3_to_dvec3(v: Vector3) -> DVec3 {
Expand Down
Loading

0 comments on commit 89820ef

Please sign in to comment.