Skip to content

Commit

Permalink
camera control API improved
Browse files Browse the repository at this point in the history
  • Loading branch information
-karlos- committed Jan 13, 2024
1 parent 81f2386 commit aae3ef0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 67 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ cargo run --target wasm32-unknown-unknown

brew install mingw-w64
rustup target add x86_64-pc-windows-gnu
cargo clippy --fix --allow-dirty --target x86_64-pc-windows-gnu
cargo clippy --fix --allow-dirty --allow-staged --target x86_64-pc-windows-gnu
cargo fmt
cargo check --target x86_64-pc-windows-gnu
cargo build --target x86_64-pc-windows-gnu
Expand Down
2 changes: 1 addition & 1 deletion src/f4control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn player_move(
}

//view.set_camera_view(&space, &mut player, None);
let galactic_transform = view.to_transform(&space);
let galactic_transform = view.to_galactic_transform(&space);
let new_pos = GalacticTransformSpace {
galactic_transform,
space: &space,
Expand Down
24 changes: 12 additions & 12 deletions src/flycam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn player_move(
keys: Res<Input<KeyCode>>,
time: Res<Time>,
primary_window: Query<&Window, With<PrimaryWindow>>,
settings: Res<ControlValues>,
control_values: Res<ControlValues>,
key_bindings: Res<KeyBindings>,
mut query: Query<(&Control, &mut Transform)>, // mut query: Query<&mut Transform, With<Control>>,
) {
Expand All @@ -140,16 +140,16 @@ fn player_move(
} else if key == key_bindings.move_right {
velocity += right;
} else if key == key_bindings.move_ascend {
velocity += settings.up;
velocity += control_values.up;
} else if key == key_bindings.move_descend {
velocity -= settings.up;
velocity -= control_values.up;
}
}
}

velocity = velocity.normalize_or_zero();

transform.translation += velocity * time.delta_seconds() * settings.speed
transform.translation += velocity * time.delta_seconds() * control_values.speed
}
}
} else {
Expand Down Expand Up @@ -243,7 +243,7 @@ impl bevy::prelude::Plugin for Plugin {
app.add_systems(Startup, setup)
.add_systems(Update, update_camera_speed)
.add_systems(Update, grab_cursor)
// no_camera_player_plugin
// This was the "no_camera_player_plugin"
// Contains everything needed to add first-person fly camera behavior to your game, but does not spawn a camera
.init_resource::<InputState>()
.init_resource::<ControlValues>()
Expand Down Expand Up @@ -289,13 +289,13 @@ fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut images: ResMut<Assets<Image>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut movement_settings: ResMut<ControlValues>,
mut control_values: ResMut<ControlValues>,
mut keys: ResMut<KeyBindings>,
starting_values: Res<crate::StartingValues>,
space: Res<FloatingOriginSettings>,
) {
// set up accroding to lat/lon relative to Earth center
movement_settings.up = starting_values.planetary_position.normalize().as_vec3();
control_values.up = starting_values.planetary_position.normalize().as_vec3();
let (grid, _): (GalacticGrid, _) =
space.translation_to_grid(starting_values.planetary_position);

Expand Down Expand Up @@ -343,19 +343,19 @@ fn setup(
// We'll need this when the player moves very far or teleports to another place, as we need to ensure we don't go into
// regions where the floating point numbers become imprecise.

movement_settings.speed = 100.0;
control_values.speed = 100.0;
// Don't use ESC for grabbing/releasing the cursor. That's what browsers use, too, so it gets grabbed by bevy and released by the browser at the same time.
keys.toggle_grab_cursor = KeyCode::G;
}

fn update_camera_speed(
mut movement_settings: ResMut<ControlValues>,
mut control_values: ResMut<ControlValues>,
fly_cam: Query<GalacticTransform, With<Control>>,
space: Res<FloatingOriginSettings>,
) {
let elevation = fly_cam.single().position_double(&space).length() as f32;
let speed = (1. * (elevation - crate::geocoord::EARTH_RADIUS - 300.0)).max(100.0);
movement_settings.speed = speed;
control_values.speed = speed;
}

// Todo ? Merge both to fn update? To many different parameters?
Expand Down Expand Up @@ -387,7 +387,7 @@ fn grab_cursor(
}

pub fn update_camera_orientations(
mut movement_settings: ResMut<ControlValues>,
mut control_values: ResMut<ControlValues>,
mut fly_cam: Query<GalacticTransform, With<Control>>,
space: Res<FloatingOriginSettings>,
) {
Expand All @@ -398,7 +398,7 @@ pub fn update_camera_orientations(
.position_double(&space)
.normalize() // direction from galactic NULL = from the Earth center
.as_vec3();
movement_settings.up = up;
control_values.up = up;

// Reorient "up" axis without introducing other rotations.
let forward = fly_cam.transform.forward();
Expand Down
61 changes: 9 additions & 52 deletions src/geoview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl GeoView {
}
}

pub fn to_transform(self, space: &FloatingOriginSettings) -> GalacticTransformOwned {
pub fn to_galactic_transform(self, space: &FloatingOriginSettings) -> GalacticTransformOwned {
// Position on Earth ground
let mut starting_transform: GalacticTransformSpace<'_> = self
.geo_coord
Expand Down Expand Up @@ -144,57 +144,11 @@ impl GeoView {
&self,
space: &FloatingOriginSettings,
player: &mut Player,
control_values: Option<&mut ControlValues>,
control_values: &mut ControlValues,
) {
/*
match starting_values.cam_control_mode {
CamControlMode::F4 => {set_camera_view_f4()},
_ => {set_camera_view_fly()},
}
*/

if let Some(control_values) = control_values {
control_values.view = *self;
}

let galactic_transform = self.to_transform(space);

/* * /
// Position on Earth ground
let mut starting_transform = self
.geo_coord
.to_cartesian()
.to_galactic_transform_space(space);
let directions = starting_transform.directions();
control_values.view = *self;

// Add camera / player height above ground
starting_transform.transform.translation += directions.up * self.elevation;
let _camera_spot = starting_transform.transform.translation;
// Look northwards (to Earth center)
starting_transform
.transform
.look_to(directions.north, directions.up);
// Rotate to west or east
starting_transform
.transform
.rotate_axis(directions.up, self.direction.to_radians());
// 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_transform
.transform
.rotate_local_x(self.up_view.to_radians());
if self.distance > 0.0 {
//let beam_directon = starting_transform.transform.rotation; // quat
//let (angle,_) = beam_directon.to_axis_angle();
starting_transform.transform.translation += directions.west * self.distance;
//starting_transform.transform.look_at(_camera_spot);
}
player.set_pos(starting_transform);
/ * */
let galactic_transform = self.to_galactic_transform(space);

let new_pos = GalacticTransformSpace {
galactic_transform,
Expand Down Expand Up @@ -277,7 +231,7 @@ fn keys_ui(
info!("*** key: {:?}", key_string);
let view3 = GeoView::restore(key_string, &mut views.map);
if let Some(view3) = view3 {
view3.set_camera_view(&space, &mut player, Some(&mut control_values));
view3.set_camera_view(&space, &mut player, &mut control_values);
}
}
}
Expand All @@ -291,14 +245,17 @@ fn keys_ui_setup(
starting_values: Res<crate::StartingValues>,
mut player: Player,
mut views: ResMut<Views>,
mut control_values: ResMut<ControlValues>,
space: Res<FloatingOriginSettings>,
) {
// The start view is placed for Key0 and
// all controls are set here (also the camera)
starting_values
.start_view
.store("Key0".to_string(), &mut views.map);
starting_values
.start_view
.set_camera_view(&space, &mut player, None);
.set_camera_view(&space, &mut player, &mut control_values);
}

pub struct Plugin;
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub fn main() {
args.extend(std::env::args().skip(1));
}

let mut _cam_control_mode = CamControlMode::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 Down

0 comments on commit aae3ef0

Please sign in to comment.