spawn a sphere and be able to move the camera

This commit is contained in:
Joe Ardent 2025-05-24 15:08:04 -07:00
commit 9edc499a53
4 changed files with 7863 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

7694
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "spointer"
version = "0.1.0"
edition = "2024"
[dependencies]
anise = "0.5.4"
bevy = "0.16.0"
hifitime = "4.0.2"
log = { version = "0.4.27", features = ["std"] }
nyx-space = "2.1.0"

157
src/main.rs Normal file
View file

@ -0,0 +1,157 @@
use anise::{
almanac::metaload::MetaFile,
constants::{
celestial_objects::{MOON, SUN},
frames::{EARTH_J2000, IAU_EARTH_FRAME},
},
};
use hifitime::{Epoch, Unit};
use log::warn;
use nyx_space::{
cosmic::{Mass, MetaAlmanac, Orbit, SRPData},
dynamics::{Harmonics, OrbitalDynamics, SolarPressure, SpacecraftDynamics},
io::{gravity::HarmonicsMem, ExportCfg},
od::GroundStation,
propagators::Propagator,
Spacecraft, State,
};
use bevy::{
color::palettes::css::BLUE,
pbr::wireframe::{WireframeColor, WireframeConfig, WireframePlugin},
platform::collections::HashSet,
prelude::*,
};
fn main() {
let mut app = App::new();
app.add_plugins((DefaultPlugins, WireframePlugin::default()))
.insert_gizmo_config(DefaultGizmoConfigGroup, GizmoConfig::default())
.insert_resource(DebugCamOffset::default())
.insert_resource(WireframeConfig {
global: true,
..Default::default()
})
.add_systems(Startup, (spawn_earth, spawn_camera))
.add_systems(Update, (close_on_esc, update_camera_offset, move_camera))
.run();
}
#[derive(Debug, Resource)]
pub struct DebugCamOffset {
pub long: f32,
pub dist: f32,
pub lat: f32,
}
impl Default for DebugCamOffset {
fn default() -> Self {
DebugCamOffset {
// 500 units above los angeles
dist: 500.0,
lat: 34.2,
long: -118.45,
}
}
}
fn spawn_earth(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let spatial_bundle = (Transform::default(), Visibility::default());
let mat = materials.add(StandardMaterial::default());
commands.spawn((
spatial_bundle,
Mesh3d(meshes.add(Sphere::new(100.0).mesh().ico(10).unwrap())),
MeshMaterial3d(mat),
WireframeColor { color: BLUE.into() },
AmbientLight {
brightness: 2_000.0,
..Default::default()
},
));
}
fn close_on_esc(
mut commands: Commands,
focused_windows: Query<(Entity, &Window)>,
input: Res<ButtonInput<KeyCode>>,
) {
for (window, focus) in focused_windows.iter() {
if !focus.focused {
continue;
}
if input.just_pressed(KeyCode::Escape) {
commands.entity(window).despawn();
}
}
}
#[derive(Debug, Default, Component)]
struct SpaceCamera;
fn spawn_camera(mut commands: Commands) {
commands
.spawn(Camera3d::default())
.insert(SpaceCamera)
.insert(Transform::default());
}
fn move_camera(camera: Single<&mut Transform, With<SpaceCamera>>, offset: Res<DebugCamOffset>) {
let mut xform = camera.into_inner();
let origin = Vec3::ZERO;
let north = Vec3::Y;
let mut new_xform = Transform::from_translation(origin);
new_xform.rotate(Quat::from_axis_angle(north, offset.long.to_radians()));
new_xform.rotate(Quat::from_axis_angle(
*xform.left(),
offset.lat.to_radians(),
));
new_xform.translation += new_xform.forward() * offset.dist;
new_xform.look_at(origin, north);
*xform = new_xform;
}
fn update_camera_offset(
mut offset: ResMut<DebugCamOffset>,
mut keys: ResMut<ButtonInput<KeyCode>>,
) {
let keyset: HashSet<_> = keys.get_pressed().collect();
let shifted = keyset.contains(&KeyCode::ShiftLeft) || keyset.contains(&KeyCode::ShiftRight);
for key in keyset {
match key {
KeyCode::ArrowLeft => offset.long -= 1.0,
KeyCode::ArrowRight => offset.long += 1.0,
KeyCode::ArrowUp => {
if shifted {
offset.lat += 0.5;
} else {
offset.dist += 10.0;
}
}
KeyCode::ArrowDown => {
if shifted {
offset.lat -= 0.5;
} else {
offset.dist -= 10.0;
}
}
KeyCode::KeyR => {
*offset = DebugCamOffset::default();
}
_ => continue,
}
}
let released: Vec<_> = keys.get_just_released().copied().collect();
for key in released {
keys.clear_just_released(key);
}
}