Skip to content

Commit

Permalink
set camera by GeoView
Browse files Browse the repository at this point in the history
  • Loading branch information
-karlos- committed Dec 22, 2023
1 parent dba76c2 commit 7b6c0cb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
28 changes: 4 additions & 24 deletions src/flycam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,26 +61,9 @@ fn setup(
args: Res<crate::Args>,
space: Res<FloatingOriginSettings>,
) {
let height_direction = args.starting_position.normalize().as_vec3();
let (grid, subgrid): (GalacticGrid, _) = space.translation_to_grid(args.starting_position);

let mut transform = Transform::from_translation(subgrid + height_direction * args.elevation)
// This "hack" rotates the camera-view down and accroding to Lat/Lon
.looking_at(subgrid, Vec3::Z) // ::Z for Nord
;

// Todo?: Rotate realy by Lat/Lon
// Bevy Camera default-rotation is: view to -z and up = +y
// OSMeta Earth is Nord = +Z and Greenwich at 0 degrees to -Y? so the default view should be +Y.
// So the initial rotaton needs to be 90 degrees -X ???
// let mut rotation = Quat::from_axis_angle(Vec3::Y, (-90_f32).to_radians())
// * Quat::from_axis_angle(Vec3::Z, (-90_f32).to_radians())

let rotation = Quat::from_axis_angle(Vec3::Z, args.direction.to_radians())
* Quat::from_axis_angle(Vec3::X, args.up_view.to_radians());
transform = transform * Transform::from_rotation(rotation);

movement_settings.up = height_direction;
// set up accroding to lat/lon relative to Earth center
movement_settings.up = args.starting_position.normalize().as_vec3();
let (grid, _): (GalacticGrid, _) = space.translation_to_grid(args.starting_position);

let material = materials.add(StandardMaterial {
base_color_texture: Some(images.add(uv_debug_texture())),
Expand All @@ -103,10 +86,7 @@ fn setup(
.id();

let mut camera = commands.spawn((
Camera3dBundle {
transform,
..default()
},
Camera3dBundle { ..default() },
InheritedVisibility::default(),
FlyCam,
grid,
Expand Down
8 changes: 8 additions & 0 deletions src/geoview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ impl GeoView {
}
}

fn keys_ui_setup(mut player: Player, mut views: ResMut<Views>, space: Res<FloatingOriginSettings>) {
let start_view = GeoView::restore(("Key0").to_string(), &mut views.map);
if let Some(start_view) = start_view {
start_view.set_camera_view(&space, &mut player);
}
}

// System: If keys pressed, store and restore camera views
fn keys_ui(
keys: Res<Input<KeyCode>>,
Expand Down Expand Up @@ -214,6 +221,7 @@ impl bevy::prelude::Plugin for Plugin {
self.start_view.store("Key0".to_string(), &mut map);
//lf.start_view.set_camera_view(&space, &mut player);
app.insert_resource(Views { map });
app.add_systems(PostStartup, keys_ui_setup);
}
}

Expand Down
16 changes: 5 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ type GalacticTransformItem<'a> = GridTransformItem<'a, GridPrecision>;
#[derive(Resource)]
struct Args {
starting_position: PlanetaryPosition,
elevation: f32,
direction: f32,
up_view: f32,
xr: bool,
}

Expand All @@ -77,14 +74,14 @@ pub fn main() {
}

let mut geo_coord = GeoCoord {
lat: 0., //48.1408, // Germany, Munic, Main railway station
lon: 0., //11.5577,
lat: 48.1408, // Germany, Munic, Main railway station
lon: 11.5577,
};
let mut elevation: f32 = 5000000.; //300.0; // Camare hight about ground
let mut elevation: f32 = 300.0; // Camare hight about ground 5000000.; //

// GeoView to city center, Marienplatz
let mut direction: f32 = 0.; //(-105.0_f32); // Compass view-direction to Oeast-Southeast. 0 = Nord, -90 = East Todo: Why minus?
let mut up_view: f32 = 0.; //(75.0_f32); // Up-view slightly down. -90 = down, 0 = horizontal 90 = Up
let mut direction: f32 = -105.0; // Compass view-direction to Oeast-Southeast. 0 = Nord, -90 = East Todo: Why minus?
let mut up_view: f32 = 75.0; // Up-view slightly down. -90 = down, 0 = horizontal 90 = Up

let mut xr = false;

Expand All @@ -106,9 +103,6 @@ pub fn main() {
let mut app = App::new();
app.insert_resource(Args {
starting_position: geo_coord.to_cartesian(),
elevation,
direction,
up_view,
xr,
});

Expand Down

0 comments on commit 7b6c0cb

Please sign in to comment.