diff --git a/src/main.rs b/src/main.rs index 10f5c2e..55c3c74 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>, mut materials: ResMut>, ) { - 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>, offset: Res { 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, 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)); +}