Skip to content

Commit

Permalink
First moves of F4, and some renames
Browse files Browse the repository at this point in the history
  • Loading branch information
-karlos- committed Dec 26, 2023
1 parent 4ab8595 commit 50e0d46
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 98 deletions.
4 changes: 2 additions & 2 deletions notes_OSMeta.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ Rotatons are allways relative. Bevy starts with a defined default rotation. geo-

What "classes" and to/from-functions do we have or need for our project and our futur users?
* GeoCoords {lat,lon}: to_cartesian -> PlanetaryPosition, -> GeoView::new
* PlanetaryPosition {pos: DVec3/3*f64}: to_galactic_position -> GalacticPosition
* GalacticPosition {GalacticTransform,space/FloatingOriginSettings}:
* PlanetaryPosition {pos: DVec3/3*f64}: to_galactic_transform -> GalacticTransfor
* GalacticTransfor {GalacticTransform,space/FloatingOriginSettings}:
* GalacticTransform/GridTransform){Transform,GridCell}:
* GridCell {x,y,z}:

Expand Down
130 changes: 67 additions & 63 deletions src/f4control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ use bevy::input::mouse::MouseMotion;
use bevy::prelude::*;
use bevy::window::{CursorGrabMode, PrimaryWindow};

use crate::geoview::GeoView;
use crate::GalacticGrid;
use big_space::{FloatingOrigin, FloatingOriginSettings};

use crate::player::Player;

pub mod prelude {
pub use crate::*;
}

/// Keeps track of mouse motion events, pitch, and yaw
#[derive(Resource, Default)]
struct InputState {
reader_motion: ManualEventReader<MouseMotion>,
_reader_motion: ManualEventReader<MouseMotion>,
}

/// Mouse sensitivity and movement speed
Expand All @@ -58,13 +61,7 @@ pub struct MovementValues {
pub sensitivity: f32,
pub speed: f32,
pub up: Vec3,
pub lat: f32,
pub lon: f32,
pub elevation: f32,
pub direction: f32,
pub up_view: f32,
pub distance: f32,
pub camera_fov: f32,
pub view: GeoView,
}

impl Default for MovementValues {
Expand All @@ -73,13 +70,7 @@ impl Default for MovementValues {
sensitivity: 0.00012,
speed: 12.,
up: Vec3::Y,
lat: 0.,
lon: 0.,
elevation: 0.,
direction: 0.,
up_view: 0.,
distance: 0.,
camera_fov: 0.,
view: GeoView::new(),
}
}
}
Expand Down Expand Up @@ -114,56 +105,72 @@ impl Default for KeyBindings {
// pub struct FlyCam;
use bevy_flycam::FlyCam; // not F4Control Todo: name it CamControl for the just running control --

#[derive(Component)]
pub struct F4Control;

/*
thread 'main' panicked at /Users/karlos/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.12.1/src/system/system_param.rs:225:5:
error[B0001]:
Query<(&bevy_flycam::FlyCam, &mut bevy_transform::components::transform::Transform), ()>
in system osmeta::f4control::player_move accesses component(s) bevy_transform::components::transform::Transform
in a way that conflicts with a previous system parameter.
Consider using `Without<T>` to create disjoint Queries or merging conflicting Queries into a `ParamSet`.
thread 'main' panicked at /Users/karlos/.cargo/registry/src/index.crates.io-6f17d22bba15001f/bevy_ecs-0.12.1/src/system/system_param.rs:225:5:
error[B0001]:
Query<big_space::world_query::GridTransform<i64>, (bevy_ecs::query::filter::With<bevy_flycam::FlyCam>, bevy_ecs::query::filter::Without<osmeta::OpenXRTrackingRoot>,
bevy_ecs::query::filter::Without<osmeta::Compass>)>
in system osmeta::f4control::player_move accesses component(s)
bevy_transform::components::transform::Transform
in a way that conflicts with a previous system parameter.
Consider using `Without<T>` to create disjoint Queries or merging conflicting Queries into a `ParamSet`.
*/

/// Handles keyboard input and movement
fn player_move(
keys: Res<Input<KeyCode>>,
key_bindings: Res<KeyBindings>,
time: Res<Time>,
primary_window: Query<&Window, With<PrimaryWindow>>,
space: Res<FloatingOriginSettings>,
mut movement_values: ResMut<MovementValues>,
key_bindings: Res<KeyBindings>,
mut query: Query<(&FlyCam, &mut Transform)>, // mut query: Query<&mut Transform, With<F4Control>>,
mut player: Player,
) {
if let Ok(_window) = primary_window.get_single() {
for (_camera, mut transform) in query.iter_mut() {

let speed = (1. * (movement_values.elevation - crate::geocoord::EARTH_RADIUS - 300.0)).max(100.0);
movement_values.speed = speed;

let mut velocity = Vec3::ZERO;
let forward = transform.forward();
let right = transform.right();

for key in keys.get_pressed() {
let key = *key;
if key == key_bindings.move_forward {
velocity += forward;
} else if key == key_bindings.move_backward {
velocity -= forward;
} else if key == key_bindings.move_left {
velocity -= right;
} else if key == key_bindings.move_right {
velocity += right;
} else if key == key_bindings.move_ascend {
velocity += movement_values.up;
} else if key == key_bindings.move_descend {
velocity -= movement_values.up;
}

velocity = velocity.normalize_or_zero();

transform.translation += velocity * time.delta_seconds() * movement_values.speed;



let speed = (1. * (movement_values.view.elevation - 300.0)).max(100.0);
movement_values.speed = speed;

let view = &mut movement_values.view;
let elevation_fakt = 1. + time.delta_seconds() / 1.0;
let groundmove_fact = speed * time.delta_seconds() / 100000.0;

for key in keys.get_pressed() {
// match key does not work with struct key_bindings
let key = *key;
if key == key_bindings.move_forward {
view.geo_coord.lat += groundmove_fact;
} else if key == key_bindings.move_backward {
view.geo_coord.lat -= groundmove_fact;
} else if key == key_bindings.move_left {
view.geo_coord.lon -= groundmove_fact;
} else if key == key_bindings.move_right {
view.geo_coord.lon += groundmove_fact;
} else if key == key_bindings.move_ascend {
view.elevation *= elevation_fakt;
} else if key == key_bindings.move_descend {
view.elevation /= elevation_fakt;
}
}
view.set_camera_view(&space, &mut player);
} else {
warn!("Primary window not found for `player_move`!");
}
}

/// Handles looking around if cursor is locked
fn player_look(
fn _player_look(
settings: Res<MovementValues>,
primary_window: Query<&Window, With<PrimaryWindow>>,
mut state: ResMut<InputState>,
Expand All @@ -172,7 +179,7 @@ fn player_look(
) {
if let Ok(window) = primary_window.get_single() {
for mut transform in query.iter_mut() {
for ev in state.reader_motion.read(&motion) {
for ev in state._reader_motion.read(&motion) {
let mut yaw = 0.0;
let mut pitch = 0.0;
match window.cursor.grab_mode {
Expand Down Expand Up @@ -213,38 +220,35 @@ fn setup(
starting_values: Res<crate::StartingValues>,
space: Res<FloatingOriginSettings>,
) {
// set up accroding to lat/lon relative to Earth center
movement_values.up = starting_values.planetary_position.normalize().as_vec3();
let (grid, _): (GalacticGrid, _) =
space.translation_to_grid(starting_values.planetary_position);

let mut camera = commands.spawn((
Camera3dBundle { ..default() },
InheritedVisibility::default(),
FlyCam,
F4Control,
grid,
));
camera.insert(FloatingOrigin);

// set up accroding to lat/lon relative to Earth center
movement_values.up = starting_values.planetary_position.normalize().as_vec3();
movement_values.speed = 100.0;
movement_values.lat = starting_values.start_view.geo_coord.lat;
movement_values.lon = starting_values.start_view.geo_coord.lon;
movement_values.elevation = starting_values.start_view.elevation;
movement_values.direction = starting_values.start_view.direction;
movement_values.up_view = starting_values.start_view.up_view;
movement_values.distance = starting_values.start_view.distance;
movement_values.camera_fov = starting_values.start_view.camera_fov;

movement_values.view = starting_values.start_view;
}

pub struct Plugin;

impl bevy::prelude::Plugin for Plugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup)
.init_resource::<InputState>()
app
.init_resource::<MovementValues>()
.add_systems(Startup, setup)
.init_resource::<KeyBindings>()
.add_systems(Update, player_move)
.add_systems(Update, player_look);
//.init_resource::<InputState>()
//.add_systems(Update, player_look)
;
}
}
5 changes: 3 additions & 2 deletions src/flycam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ fn setup(
) {
// set up accroding to lat/lon relative to Earth center
movement_settings.up = starting_values.planetary_position.normalize().as_vec3();
let (grid, _): (GalacticGrid, _) = space.translation_to_grid(starting_values.planetary_position);
let (grid, _): (GalacticGrid, _) =
space.translation_to_grid(starting_values.planetary_position);

let material = materials.add(StandardMaterial {
base_color_texture: Some(images.add(uv_debug_texture())),
Expand Down Expand Up @@ -157,7 +158,7 @@ pub fn update_camera_orientations(
mut fly_cam: Query<GalacticTransform, With<FlyCam>>,
space: Res<FloatingOriginSettings>,
) {
// the only FlyCam's GalacticPosition <grid,f32>
// the only FlyCam's GalacticTransfor <grid,f32>
let mut fly_cam = fly_cam.single_mut();

let up = fly_cam
Expand Down
35 changes: 23 additions & 12 deletions src/geoview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ pub struct GeoView {
}

impl GeoView {
pub fn new() -> GeoView {
GeoView {
geo_coord: GeoCoord { lat: 0., lon: 0. },
elevation: 0.,
direction: 0.,
up_view: 0.,
distance: 0.,
camera_fov: 0.,
}
}
/**
* Store self GeoView in a browser cookie
* To restore it into your viewer, use [[GeoView]].[[restore]]
Expand Down Expand Up @@ -92,26 +102,28 @@ impl GeoView {
}

pub fn set_camera_view(&self, space: &FloatingOriginSettings, player: &mut Player) {
let mut starting_position = self.geo_coord.to_cartesian().to_galactic_position(space);
let directions = starting_position.directions();

starting_position.transform.translation += directions.up * self.elevation;
// Look northwards
starting_position
// Position on Earth ground
let mut starting_transform = self.geo_coord.to_cartesian().to_galactic_transform(space);
let directions = starting_transform.directions();

// Add camera / player height above ground
starting_transform.transform.translation += directions.up * self.elevation;
// Look northwards (to Earth center)
starting_transform
.transform
.look_to(directions.north, directions.up);

// Rotate to west or east
starting_position
starting_transform
.transform
.rotate_axis(directions.up, self.direction.to_radians());
// Pan up or down. We subtract 90°, because the up-view is an angle from looking
// Pan up or down. We subtract 90° (FRAC_PI_2), because the up-view is an angle from looking
// straight down. We don't default to looking down, as that doesn't guarantee us
// that the forward direction is north.
starting_position
starting_transform
.transform
.rotate_local_x(self.up_view.to_radians() - FRAC_PI_2);
player.set_pos(starting_position);

player.set_pos(starting_transform);
}

#[instrument(level = "debug", skip(space, player), ret)]
Expand Down Expand Up @@ -210,7 +222,6 @@ impl bevy::prelude::Plugin for Plugin {
// todo: the reaction is bad? Mayh be this helps: Pairing with bevy_framepace to smooth out input latency
app.add_systems(Update, keys_ui);
let map = HashMap::new();
//lf.start_view.set_camera_view(&space, &mut player);
app.insert_resource(Views { map });
app.add_systems(PostStartup, keys_ui_setup);
}
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn main() {
}
}

let start_view = GeoView {
let _start_view = GeoView {
geo_coord,
elevation,
direction,
Expand All @@ -131,14 +131,14 @@ pub fn main() {
camera_fov,
};

let _start_view = GeoView {
let start_view = GeoView {
// test only
geo_coord: GeoCoord { lat: 33., lon: 0. }, // up,dir
elevation: 5000000.,
geo_coord, //: GeoCoord { lat: 33., lon: 0. }, // up,dir
elevation: 1000.,
direction: 0.,
up_view: 0.02,
distance,
camera_fov,
up_view: 0.,
distance: 1000.,
camera_fov: 44.,
};

let mut app = App::new();
Expand Down Expand Up @@ -183,15 +183,15 @@ pub fn main() {
}
CamControlMode::Fly => {
app.add_plugins(flycam::Plugin)
.add_systems(Update, update_camera_orientations);
.add_systems(Update, update_camera_orientations)
.add_systems(PostUpdate, reposition_compass);
}
}

app.add_plugins(geoview::Plugin)
.insert_resource(TileMap::default())
.add_systems(Startup, setup)
.add_plugins(tilemap::Plugin)
.add_systems(PostUpdate, reposition_compass)
.run();
}

Expand Down
Loading

0 comments on commit 50e0d46

Please sign in to comment.