draw an arrow from home to space

This commit is contained in:
Joe Ardent 2025-05-25 10:34:13 -07:00
parent 5f7bdc588a
commit 188aba8bcf

View file

@ -32,11 +32,20 @@ fn main() {
global: true,
..Default::default()
})
.insert_resource(HomeCartesian::default())
.add_systems(Startup, (spawn_earth, spawn_camera))
.add_systems(Update, (close_on_esc, update_camera_offset, move_camera))
.add_systems(
Update,
(close_on_esc, update_camera_offset, move_camera, draw_arrow),
)
.run();
}
// const RADIUS: f32 = 6378.0;
const RADIUS: f32 = 100.0;
const HOME_LAT: f32 = 34.2;
const HOME_LONG: f32 = -118.45;
#[derive(Debug, Resource)]
pub struct DebugCamOffset {
pub long: f32,
@ -44,27 +53,53 @@ pub struct DebugCamOffset {
pub lat: f32,
}
#[derive(Debug, Resource)]
pub struct HomeCartesian {
pub pos: Vec3,
}
impl Default for HomeCartesian {
fn default() -> Self {
Self {
pos: polar2cart(HOME_LAT, HOME_LONG, RADIUS),
}
}
}
impl Default for DebugCamOffset {
fn default() -> Self {
DebugCamOffset {
// 500 units above los angeles
dist: 500.0,
lat: 34.2,
lat: 0.0,
long: -118.45,
}
}
}
/// From degrees lat/long to X,Y,Z (Y-up)
fn polar2cart(lat: f32, long: f32, radius: f32) -> Vec3 {
let r_cos_lat = radius * lat.to_radians().cos();
let x = r_cos_lat * long.to_radians().sin();
let y = radius * lat.to_radians().sin();
let z = r_cos_lat * long.to_radians().cos();
Vec3::new(x, y, z)
}
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());
let spatial_bundle = (
Transform::from_rotation(Quat::from_rotation_x(90.0f32.to_radians())),
Visibility::default(),
);
let mat = materials.add(StandardMaterial::from_color(Color::linear_rgba(
0., 0., 0., 0.,
)));
commands.spawn((
spatial_bundle,
Mesh3d(meshes.add(Sphere::new(100.0).mesh().ico(10).unwrap())),
Mesh3d(meshes.add(Sphere::new(100.0).mesh().uv(32, 18))),
MeshMaterial3d(mat),
WireframeColor { color: BLUE.into() },
AmbientLight {
@ -106,13 +141,9 @@ fn move_camera(camera: Single<&mut Transform, With<SpaceCamera>>, offset: Res<De
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;
let pos = polar2cart(offset.lat, offset.long, offset.dist);
let mut new_xform = Transform::from_translation(pos);
new_xform.look_at(origin, north);
*xform = new_xform;
@ -133,14 +164,14 @@ fn update_camera_offset(
if shifted {
offset.lat += 0.5;
} else {
offset.dist += 10.0;
offset.dist -= 10.0;
}
}
KeyCode::ArrowDown => {
if shifted {
offset.lat -= 0.5;
} else {
offset.dist -= 10.0;
offset.dist += 10.0;
}
}
KeyCode::KeyR => {
@ -155,3 +186,9 @@ fn update_camera_offset(
keys.clear_just_released(key);
}
}
fn draw_arrow(home: Res<HomeCartesian>, mut gizmos: Gizmos) {
let start = home.pos;
let end = home.pos * 2.0;
gizmos.arrow(start, end, Color::linear_rgb(1.0, 1.0, 0.0));
}