Skip to content

Commit

Permalink
add even more moons
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-tennert committed Oct 17, 2023
1 parent 6dcdf64 commit 2213aa6
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assets/bodies.sim

Large diffs are not rendered by default.

Binary file added assets/models/ariel.glb
Binary file not shown.
Binary file added assets/models/dione.glb
Binary file not shown.
Binary file added assets/models/enceladus.glb
Binary file not shown.
Binary file added assets/models/iapetus.glb
Binary file not shown.
Binary file added assets/models/mimas.glb
Binary file not shown.
Binary file added assets/models/miranda.glb
Binary file not shown.
Binary file added assets/models/oberon.glb
Binary file not shown.
Binary file added assets/models/tethys.glb
Binary file not shown.
Binary file added assets/models/titania.glb
Binary file not shown.
Binary file added assets/models/umbriel.glb
Binary file not shown.
2 changes: 1 addition & 1 deletion src/bodies.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::core::Name;
use bevy::math::{Vec3, DVec3};
use bevy::prelude::{default, Transform, Quat, EulerRot};
use crate::body::{BodyBundle, Mass, ModelPath, Scale, Velocity, LightSource, SimPosition, RotationSpeed, StartingRotation, Diameter};
use crate::body::{BodyBundle, Mass, ModelPath, Scale, Velocity, LightSource, SimPosition, RotationSpeed, Diameter};
use crate::constants::M_TO_UNIT;

pub struct Bodies;
Expand Down
14 changes: 11 additions & 3 deletions src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ pub struct Acceleration(pub DVec3);
pub struct Scale(pub f32);

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

#[derive(Component, Reflect, Clone, Default)]
pub struct StartingRotation(pub Quat);
pub struct AxialTilt {
pub num: f32,
pub applied: bool
}

#[derive(Component, Reflect, Clone, Default)]
pub struct ModelPath(pub String);
Expand Down Expand Up @@ -90,7 +93,7 @@ pub struct BodyBundle {
pub light: LightSource,
pub orbit: OrbitSettings,
pub rotation_speed: RotationSpeed,
pub starting_rotation: StartingRotation,
pub axial_tilt: AxialTilt,
pub diameter: Diameter,

}
Expand All @@ -108,6 +111,11 @@ impl From<SerializedBody> for BodyBundle {
num: value.data.diameter * 1000.0,
..default()
},
axial_tilt: AxialTilt {
num: value.data.axial_tilt,
..default()
},
rotation_speed: RotationSpeed(value.data.rotation_speed),
..default()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub const G: f64 = 6.67430e-11_f64; //gravitational constant
pub const M_TO_UNIT: f64 = 6.684587e-9_f64;
pub const M_TO_UNIT: f64 = 6.684587e-8_f64;

pub const HOUR_IN_SECONDS: f32 = 60.0 * 60.0;
pub const DAY_IN_SECONDS: f32 = HOUR_IN_SECONDS * 24.0;
Expand Down
1 change: 1 addition & 0 deletions src/diameter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use bevy::{app::{App, Plugin}, prelude::{Query, Transform, Res, Entity, PreUpdate, Local, GizmoConfig, ResMut, AabbGizmo, GlobalTransform, PostUpdate, Update, With, Handle, Mesh, Vec3, Name, Children, in_state, IntoSystemConfigs, Visibility}, render::primitives::{Aabb, Sphere}, math::Vec3A, scene::{SceneSpawner, SceneInstance}};

use crate::{body::Diameter, SimState, constants::M_TO_UNIT};

pub struct DiameterPlugin;

impl Plugin for DiameterPlugin {
Expand Down
49 changes: 49 additions & 0 deletions src/lock_on.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use bevy::prelude::{App, Entity, Plugin, Resource, Res, Update, IntoSystemConfigs, in_state, Transform, Query, Camera, Vec3, Without};

use crate::{selection::{SelectedEntity, apply_camera_to_selection}, SimState, body::{BodyChildren, Mass}, pan_orbit::lib::pan_orbit_camera, physics::update_position};

pub struct LockOnPlugin;

impl Plugin for LockOnPlugin {

fn build(&self, app: &mut App) {
app
.init_resource::<LockOn>()
.add_systems(Update, (lock_on.after(update_position)).run_if(in_state(SimState::Simulation)));
}

}

#[derive(Resource, Default)]
pub struct LockOn(pub bool);

fn lock_on(
lock_on: Res<LockOn>,
mut query: Query<(Entity, &Transform, Option<&BodyChildren>, Without<Camera>)>,
mut camera: Query<(&Camera, &mut Transform, Without<Mass>, Without<BodyChildren>)>,
selected_entity: Res<SelectedEntity>
) {
if !lock_on.0 {
return;
}
if let Some(s_entity) = selected_entity.0 {
let mut parent: Option<&Transform> = None;
let mut selected: Option<&Transform> = None;
for (entity, transform, children, _) in query.iter_mut() {
if let Some(children) = children {
if children.0.contains(&s_entity) {
parent = Some(transform);
}
}
if s_entity == entity {
selected = Some(transform);
}
}
if let Some(p_transform) = parent {
if let Some(s_transform) = selected {
let (_, mut c_transform, _, _) = camera.single_mut();
c_transform.look_at(p_transform.translation, Vec3::Y);
}
}
}
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod orbit_lines;
mod reset;
mod rotation;
mod serialization;
mod lock_on;

use std::fs::File;
use std::io::{BufReader, Read};
Expand All @@ -32,6 +33,7 @@ use bevy::window::{WindowPlugin, Window, PresentMode};
use bevy_inspector_egui::quick::WorldInspectorPlugin;
use diameter::DiameterPlugin;
use fps::FpsPlugin;
use lock_on::LockOnPlugin;
use orbit_lines::OrbitLinePlugin;
use pan_orbit::lib::PanOrbitCameraPlugin;
use reset::ResetPlugin;
Expand Down Expand Up @@ -75,6 +77,7 @@ fn main() {
)
.add_plugins(WorldInspectorPlugin::new())
// .add_plugins(DefaultPickingPlugins)
.add_plugins(LockOnPlugin)
.add_plugins(SerializationPlugin)
.add_plugins(PanOrbitCameraPlugin)
.add_plugins(SetupPlugin)
Expand Down
25 changes: 22 additions & 3 deletions src/rotation.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use bevy::app::{App, Plugin};
use bevy::prelude::{Resource, Query, Transform, Res, Quat, IntoSystemConfigs, Entity, in_state, Update, Without, With};
use bevy::prelude::{Resource, Query, Transform, Res, Quat, IntoSystemConfigs, Entity, in_state, Update, Without, With, Vec3};
use bevy::time::Time;

use crate::SimState;
use crate::body::{RotationSpeed, Diameter, Star, Moon, Planet, BodyChildren};
use crate::body::{RotationSpeed, Diameter, Star, Moon, Planet, BodyChildren, AxialTilt};
use crate::physics::{update_position, Pause};
use crate::speed::Speed;

Expand All @@ -14,11 +14,30 @@ impl Plugin for RotationPlugin {

fn build(&self, app: &mut App) {
app
.add_systems(Update, (rotate_bodies.after(update_position)).run_if(in_state(SimState::Simulation)));
.add_systems(Update, (axial_tilt_planets).run_if(in_state(SimState::Simulation)));
}

}

fn axial_tilt_planets(
mut query: Query<(Entity, &mut AxialTilt, &mut Transform, With<Planet>, Without<Star>, Without<Moon>)>,
parents_query: Query<(&Transform, &BodyChildren, With<Star>, Without<Planet>, Without<Moon>)>
) {
for (entity, mut tilt, mut transform, _, _, _) in &mut query {
if tilt.applied {
continue;
}

let parent = parents_query.iter().find(|(_, children, _, _, _)| {
children.0.contains(&entity)
});
if let Some((p_transform, _, _, _, _)) = parent {
transform.look_at(p_transform.translation, Vec3::NEG_Z);
tilt.applied = true;
}
}
}

fn rotate_bodies(
mut planet_query: Query<(&RotationSpeed, &mut Transform, &Diameter, With<Planet>, Without<Star>, Without<Moon>)>,
time: Res<Time>,
Expand Down
4 changes: 3 additions & 1 deletion src/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ pub struct SerializedBodyData {
pub starting_velocity: SerializedVec,
pub name: String,
pub model_path: String,
pub diameter: f64
pub diameter: f64,
pub rotation_speed: f64,
pub axial_tilt: f32,
}

#[derive(Default)]
Expand Down
4 changes: 2 additions & 2 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ pub fn setup_planets(
star.insert(PointLightBundle {
point_light: PointLight {
color: Color::rgba(1.0, 1.0, 1.0, 1.0),
intensity: 150000000.0,
intensity: 15000000000.0,
shadows_enabled: false,
range: 3000000000.0,
range: 300000000000.0,
radius: 100.0,
..default()
},
Expand Down
8 changes: 4 additions & 4 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use bevy_egui::{egui::{self, Ui, InnerResponse, Response, ComboBox}, EguiContext
use bevy_inspector_egui::egui::{RichText, TextEdit};
use chrono::{Days, NaiveDate, Utc, DateTime, NaiveDateTime};
//use crate::fps::Fps;
use crate::{input::BlockInputPlugin, body::{Mass, Velocity, Star, Moon, Planet, BodyChildren, OrbitSettings, SimPosition}, constants::{DAY_IN_SECONDS, M_TO_UNIT}, selection::SelectedEntity, orbit_lines, fps::Fps, skybox::Cubemap, setup::StartingTime};
use crate::{input::BlockInputPlugin, body::{Mass, Velocity, Star, Moon, Planet, BodyChildren, OrbitSettings, SimPosition}, constants::{DAY_IN_SECONDS, M_TO_UNIT}, selection::SelectedEntity, orbit_lines, fps::Fps, skybox::Cubemap, setup::StartingTime, lock_on::LockOn};
use crate::physics::{Pause, update_position};
use crate::SimState;
use crate::speed::Speed;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn time_ui(
mut speed: ResMut<Speed>,
fps: Res<Fps>,
mut windows: Query<&mut Window>,
// mut lock_on_sun: ResMut<LockSun>,
mut lock_on_parent: ResMut<LockOn>,
mut pause: ResMut<Pause>,
keys: Res<Input<KeyCode>>,
mut state: ResMut<NextState<SimState>>,
Expand Down Expand Up @@ -97,7 +97,7 @@ pub fn time_ui(
if ui.button("Reset").clicked() {
let _ = state.set(SimState::Reset);
}
// ui.checkbox(&mut lock_on_sun.0, "Lock on Sun");
ui.checkbox(&mut lock_on_parent.0, "Lock on Parent");
let mut vsync = window.present_mode == PresentMode::AutoVsync;
let old_option = vsync;
ui.checkbox(&mut vsync, "VSync");
Expand Down Expand Up @@ -310,7 +310,7 @@ fn body_ui(
// Distance to parent
if let Some((parent_pos, _, p_name)) = parent {
ui.label(RichText::new(format!("Distance to {}", p_name)).size(16.0).underline());
let distance_in_km = parent_pos.0.distance(pos.0) / 1000.0;
let distance_in_km = parent_pos.0.distance(pos.0) / 10000.0;
ui.label(format!("{:.3} km", distance_in_km));
ui.label(format!("{:.3} au", distance_in_km * M_TO_UNIT));

Expand Down

0 comments on commit 2213aa6

Please sign in to comment.