Skip to content

Commit

Permalink
F4 arrows now correct to directon
Browse files Browse the repository at this point in the history
  • Loading branch information
-karlos- committed Jan 14, 2024
1 parent e227842 commit 507b87a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 33 deletions.
31 changes: 21 additions & 10 deletions src/f4control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,38 @@ fn player_move(
const ELEVATION_LIMIT: f32 = 20_000_000.0; // meter
let rotation_fact = time.delta_seconds() * 20.0; // delta time * degrees per second = delta degrees

let dir = view.direction.to_radians();
// Todo?: make a geo_forward/east? Put lat/lon in a vec3 or 2?
let north = dir.cos();
let east = -dir.sin();

for key in keys.get_pressed() {
// match key does not work with struct key_bindings
let key = *key;
// ahead
// ahead/backward
if key == key_bindings.move_forward {
view.geo_coord.lat += groundmove_fact_lat;
view.geo_coord.lat = view.geo_coord.lat.min(OSM_LAT_LIMIT);
view.geo_coord.lat += north * groundmove_fact_lat;
view.geo_coord.lon += east * groundmove_fact_lon;
} else if key == key_bindings.move_backward {
view.geo_coord.lat -= groundmove_fact_lat;
view.geo_coord.lat = view.geo_coord.lat.max(-OSM_LAT_LIMIT);
view.geo_coord.lat -= north * groundmove_fact_lat;
view.geo_coord.lon -= east * groundmove_fact_lon;
//
// sidewise
} else if key == key_bindings.move_left {
view.geo_coord.lon -= groundmove_fact_lon;
} else if key == key_bindings.move_right {
view.geo_coord.lon += groundmove_fact_lon;
view.geo_coord.lat -= east * groundmove_fact_lat;
view.geo_coord.lon += north * groundmove_fact_lon;
} else if key == key_bindings.move_left {
view.geo_coord.lat += east * groundmove_fact_lat;
view.geo_coord.lon -= north * groundmove_fact_lon;
//
// elevate
} else if key == key_bindings.move_ascend {
view.elevation *= elevation_fakt;
view.elevation = view.elevation.min(ELEVATION_LIMIT);
} else if key == key_bindings.move_descend {
view.elevation /= elevation_fakt;
view.elevation = view.elevation.max(0.4);
//
// rotate
} else if key == key_bindings.rotate_right {
view.direction -= rotation_fact;
Expand All @@ -153,6 +163,7 @@ fn player_move(
} else if key == key_bindings.rotate_down {
view.up_view -= rotation_fact;
view.up_view = view.up_view.max(-OSM_LAT_LIMIT);
//
// zoom
} else if key == key_bindings.zoom_out {
view.distance *= elevation_fakt;
Expand All @@ -163,8 +174,8 @@ fn player_move(
}
}

//view.set_camera_view(&space, &mut player, None);
let galactic_transform = view.to_galactic_transform(&space);
view.geo_coord.lat = view.geo_coord.lat.clamp(-OSM_LAT_LIMIT, OSM_LAT_LIMIT);
let galactic_transform = view.to_galactic_transform(&space, true);
let new_pos = GalacticTransformSpace {
galactic_transform,
space: &space,
Expand Down
28 changes: 20 additions & 8 deletions src/geoview.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::geocoord::*;
use super::GalacticTransformOwned;
use crate::player::CamControlMode;
use crate::player::{ControlValues, GalacticTransformSpace, Player};
use bevy::{
prelude::*,
Expand Down Expand Up @@ -102,7 +103,11 @@ impl GeoView {
}
}

pub fn to_galactic_transform(self, space: &FloatingOriginSettings) -> GalacticTransformOwned {
pub fn to_galactic_transform(
self,
space: &FloatingOriginSettings,
use_distance: bool,
) -> GalacticTransformOwned {
// Position on Earth ground
let mut starting_transform: GalacticTransformSpace<'_> = self
.geo_coord
Expand Down Expand Up @@ -130,7 +135,7 @@ impl GeoView {
.transform
.rotate_local_x(self.up_view.to_radians());

if self.distance > 0.0 {
if use_distance {
//let beam_directon = starting_transform.transform.rotation; // quat
//let (angle,_) = beam_directon.to_axis_angle();
let beam_directon = -starting_transform.transform.forward();
Expand All @@ -147,9 +152,9 @@ impl GeoView {
player: &mut Player,
control_values: &mut ControlValues,
) {
control_values.view = *self;

let galactic_transform = self.to_galactic_transform(space);
control_values.view = *self; //
let use_distance = control_values.cam_control_mode == CamControlMode::F4;
let galactic_transform = self.to_galactic_transform(space, use_distance);

let new_pos = GalacticTransformSpace {
galactic_transform,
Expand All @@ -160,7 +165,7 @@ impl GeoView {
}

#[instrument(level = "debug", skip(space, player), ret)]
pub fn get_camera_view(space: &FloatingOriginSettings, player: &Player) -> Self {
pub fn from_player(space: &FloatingOriginSettings, player: &Player) -> Self {
let position = player.pos();

let geo_coord = position.to_planetary_position().to_geocoord();
Expand Down Expand Up @@ -191,7 +196,7 @@ impl GeoView {
elevation,
direction,
up_view,
distance: 66.,
distance: 0.0,
camera_fov: 77.,
}
}
Expand Down Expand Up @@ -225,7 +230,14 @@ fn keys_ui(
if keys.pressed(KeyCode::ShiftRight) {
info!("*** KEY: {:?}", key_string);
if key != KeyCode::Key0 {
let geo_view = GeoView::get_camera_view(&space, &player);

let is_orbit_control = control_values.cam_control_mode == CamControlMode::F4;
let mut geo_view = if is_orbit_control {
control_values.view
} else {
GeoView::from_player(&space, &player)
};
geo_view.distance = control_values.view.distance; // keep distance of orbid control
geo_view.store(key_string, &mut views.map);
}
} else {
Expand Down
24 changes: 9 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use flycontrol::update_camera_orientations;
use geocoord::GeoCoord;
use geoview::GeoView;
use http_assets::HttpAssetReaderPlugin;
use player::{ControlValues, PlanetaryPosition};
use player::{CamControlMode, ControlValues, PlanetaryPosition};
use tilemap::TileMap;
#[cfg(all(feature = "xr", not(any(target_os = "macos", target_arch = "wasm32"))))]
use xr::pull_to_ground;
Expand All @@ -43,19 +43,13 @@ type GalacticTransformReadOnlyItem<'a> = GridTransformReadOnlyItem<'a, GridPreci
#[allow(dead_code)]
type GalacticTransformItem<'a> = GridTransformItem<'a, GridPrecision>;

pub enum CamControlMode {
F4,
Fly,
// todo: more to come
}

#[derive(Resource)]
struct StartingValues {
// was Args
_args: Vec<String>, // Todo: You never know where you may need it
planetary_position: PlanetaryPosition,
start_view: GeoView,
_cam_control_mode: CamControlMode,
cam_control_mode: CamControlMode,
xr: bool,
}

Expand Down Expand Up @@ -84,7 +78,7 @@ pub fn main() {
args.extend(std::env::args().skip(1));
}

let mut _cam_control_mode = CamControlMode::F4; // default: F4, test: Fly
let mut cam_control_mode = CamControlMode::F4; // default: F4, test: Fly

let mut geo_coord = GeoCoord {
lat: 48.1408, // Germany, Munic, Main railway station
Expand All @@ -93,7 +87,7 @@ pub fn main() {
let mut elevation: f32 = 50.0; // todo: default 1.4 for f4control

// GeoView to city center, Marienplatz
let mut direction: f32 = -105.0; // Compass view-direction to Oeast-Southeast. 0 = Nord, -90 = East Todo: Why minus?
let mut direction: f32 = 0.; //-105.0; // Compass view-direction to Oeast-Southeast. 0 = Nord, -90 = East Todo: Why minus?
let mut up_view: f32 = -30.0; // Up-view slightly down. -90 = down, 0 = horizontal 90 = Up
let mut distance: f32 = 500.; // radius of the sphere, the arc rotate camera rotates on
let mut camera_fov: f32 = 30.; // todo: default? field of view, the angle widht of the world, the camera is showing
Expand All @@ -112,9 +106,9 @@ pub fn main() {
let arg: String = v.parse().unwrap();
let arg: &str = arg.as_str(); // todo: better rust?
match arg {
"fly" => _cam_control_mode = CamControlMode::Fly,
"ufo" => _cam_control_mode = CamControlMode::Fly,
_ => _cam_control_mode = CamControlMode::F4,
"fly" => cam_control_mode = CamControlMode::Fly,
"ufo" => cam_control_mode = CamControlMode::Fly,
_ => cam_control_mode = CamControlMode::F4,
}
}
"lat" => geo_coord.lat = v.parse().unwrap(),
Expand Down Expand Up @@ -181,7 +175,7 @@ pub fn main() {
//.add_systems(Update, init_controls);
.init_resource::<ControlValues>();

match _cam_control_mode {
match cam_control_mode {
CamControlMode::F4 => {
app.add_plugins(f4control::Plugin);
}
Expand All @@ -196,8 +190,8 @@ pub fn main() {
_args: args,
planetary_position: geo_coord.to_cartesian(),
start_view,
cam_control_mode,
xr,
_cam_control_mode,
});

app.add_plugins(geoview::Plugin)
Expand Down
21 changes: 21 additions & 0 deletions src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,30 @@ impl<'w, 's> Player<'w, 's> {
#[derive(Component)]
pub struct Control;

#[derive(Clone, Copy, PartialEq)]
pub enum CamControlMode {
F4,
Fly,
// todo: more to come
}
/*
impl Clone for CamControlMode {
fn clone(&self) -> CamControlMode {
match self {
F4 => CamControlMode::F4,
Fly => CamControlMode::Fly,
}
}
}
*/
/// Used by all controlers: Mouse sensitivity and movement speed, up vector and set view
#[derive(Resource)]
pub struct ControlValues {
pub sensitivity: f32,
pub speed: f32,
pub up: Vec3,
pub view: GeoView,
pub cam_control_mode: CamControlMode,
}

impl Default for ControlValues {
Expand All @@ -176,6 +193,7 @@ impl Default for ControlValues {
speed: 12.,
up: Vec3::Y,
view: GeoView::new(),
cam_control_mode: CamControlMode::F4,
}
}
}
Expand Down Expand Up @@ -215,7 +233,10 @@ pub fn setup_player_controls(
mut materials: ResMut<Assets<StandardMaterial>>,
space: Res<FloatingOriginSettings>,
starting_values: Res<crate::StartingValues>,
mut control_values: ResMut<ControlValues>,
) {
control_values.cam_control_mode = starting_values.cam_control_mode;

let (grid, _): (GalacticGrid, _) =
space.translation_to_grid(starting_values.planetary_position);

Expand Down

0 comments on commit 507b87a

Please sign in to comment.