draw an arrow from home to space
This commit is contained in:
parent
5f7bdc588a
commit
188aba8bcf
1 changed files with 52 additions and 15 deletions
67
src/main.rs
67
src/main.rs
|
@ -32,11 +32,20 @@ fn main() {
|
||||||
global: true,
|
global: true,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
|
.insert_resource(HomeCartesian::default())
|
||||||
.add_systems(Startup, (spawn_earth, spawn_camera))
|
.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();
|
.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)]
|
#[derive(Debug, Resource)]
|
||||||
pub struct DebugCamOffset {
|
pub struct DebugCamOffset {
|
||||||
pub long: f32,
|
pub long: f32,
|
||||||
|
@ -44,27 +53,53 @@ pub struct DebugCamOffset {
|
||||||
pub lat: f32,
|
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 {
|
impl Default for DebugCamOffset {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
DebugCamOffset {
|
DebugCamOffset {
|
||||||
// 500 units above los angeles
|
|
||||||
dist: 500.0,
|
dist: 500.0,
|
||||||
lat: 34.2,
|
lat: 0.0,
|
||||||
long: -118.45,
|
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(
|
fn spawn_earth(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut meshes: ResMut<Assets<Mesh>>,
|
mut meshes: ResMut<Assets<Mesh>>,
|
||||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||||
) {
|
) {
|
||||||
let spatial_bundle = (Transform::default(), Visibility::default());
|
let spatial_bundle = (
|
||||||
let mat = materials.add(StandardMaterial::default());
|
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((
|
commands.spawn((
|
||||||
spatial_bundle,
|
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),
|
MeshMaterial3d(mat),
|
||||||
WireframeColor { color: BLUE.into() },
|
WireframeColor { color: BLUE.into() },
|
||||||
AmbientLight {
|
AmbientLight {
|
||||||
|
@ -106,13 +141,9 @@ fn move_camera(camera: Single<&mut Transform, With<SpaceCamera>>, offset: Res<De
|
||||||
let origin = Vec3::ZERO;
|
let origin = Vec3::ZERO;
|
||||||
let north = Vec3::Y;
|
let north = Vec3::Y;
|
||||||
|
|
||||||
let mut new_xform = Transform::from_translation(origin);
|
let pos = polar2cart(offset.lat, offset.long, offset.dist);
|
||||||
new_xform.rotate(Quat::from_axis_angle(north, offset.long.to_radians()));
|
let mut new_xform = Transform::from_translation(pos);
|
||||||
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);
|
new_xform.look_at(origin, north);
|
||||||
|
|
||||||
*xform = new_xform;
|
*xform = new_xform;
|
||||||
|
@ -133,14 +164,14 @@ fn update_camera_offset(
|
||||||
if shifted {
|
if shifted {
|
||||||
offset.lat += 0.5;
|
offset.lat += 0.5;
|
||||||
} else {
|
} else {
|
||||||
offset.dist += 10.0;
|
offset.dist -= 10.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::ArrowDown => {
|
KeyCode::ArrowDown => {
|
||||||
if shifted {
|
if shifted {
|
||||||
offset.lat -= 0.5;
|
offset.lat -= 0.5;
|
||||||
} else {
|
} else {
|
||||||
offset.dist -= 10.0;
|
offset.dist += 10.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::KeyR => {
|
KeyCode::KeyR => {
|
||||||
|
@ -155,3 +186,9 @@ fn update_camera_offset(
|
||||||
keys.clear_just_released(key);
|
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));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue