Skip to content

Commit

Permalink
Use the scroll wheel to control the camera speed in examples (bevyeng…
Browse files Browse the repository at this point in the history
…ine#11921)

# Objective

- Closes bevyengine#9384.

## Solution

- Make the movement speed of the `CameraController` adjustable with the
scroll wheel as mentioned
[here](bevyengine#9384 (comment)).
The speed use an exponential progression (10% increase per scroll tick
by default) to allow adapting the speed to different scales.
- For the `scene_viewer` example, make the default speed proportional to
the size of the scene using what's computed for the default camera
placement. This gives a good enough default to fly over the scene from
the outside. I don't think there's a good way to get a default speed
fitting for all scenes since some are meant to be viewed from outside
while other are traversable environments.
  • Loading branch information
Kanabenki authored and msvbg committed Feb 26, 2024
1 parent 813d724 commit 3725ff4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 16 additions & 1 deletion examples/helpers/camera_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! - Copy the code for the [`CameraControllerPlugin`] and add the plugin to your App.
//! - Attach the [`CameraController`] component to an entity with a [`Camera3dBundle`].

use bevy::input::mouse::MouseMotion;
use bevy::input::mouse::{MouseMotion, MouseScrollUnit, MouseWheel};
use bevy::prelude::*;
use bevy::window::CursorGrabMode;
use std::{f32::consts::*, fmt};
Expand Down Expand Up @@ -37,6 +37,7 @@ pub struct CameraController {
pub keyboard_key_toggle_cursor_grab: KeyCode,
pub walk_speed: f32,
pub run_speed: f32,
pub scroll_factor: f32,
pub friction: f32,
pub pitch: f32,
pub yaw: f32,
Expand All @@ -60,6 +61,7 @@ impl Default for CameraController {
keyboard_key_toggle_cursor_grab: KeyCode::KeyM,
walk_speed: 5.0,
run_speed: 15.0,
scroll_factor: 0.1,
friction: 0.5,
pitch: 0.0,
yaw: 0.0,
Expand All @@ -75,6 +77,7 @@ impl fmt::Display for CameraController {
"
Freecam Controls:
Mouse\t- Move camera orientation
Scroll\t- Adjust movement speed
{:?}\t- Hold to grab cursor
{:?}\t- Toggle cursor grab
{:?} & {:?}\t- Fly forward & backwards
Expand All @@ -99,6 +102,7 @@ fn run_camera_controller(
time: Res<Time>,
mut windows: Query<&mut Window>,
mut mouse_events: EventReader<MouseMotion>,
mut scroll_events: EventReader<MouseWheel>,
mouse_button_input: Res<ButtonInput<MouseButton>>,
key_input: Res<ButtonInput<KeyCode>>,
mut toggle_cursor_grab: Local<bool>,
Expand All @@ -120,6 +124,17 @@ fn run_camera_controller(
return;
}

let mut scroll = 0.0;
for scroll_event in scroll_events.read() {
let amount = match scroll_event.unit {
MouseScrollUnit::Line => scroll_event.y,
MouseScrollUnit::Pixel => scroll_event.y / 16.0,
};
scroll += amount;
}
controller.walk_speed += scroll * controller.scroll_factor * controller.walk_speed;
controller.run_speed = controller.walk_speed * 3.0;

// Handle key input
let mut axis_input = Vec3::ZERO;
if key_input.pressed(controller.key_forward) {
Expand Down
7 changes: 6 additions & 1 deletion examples/tools/scene_viewer/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ fn setup_scene_after_load(
let mut projection = PerspectiveProjection::default();
projection.far = projection.far.max(size * 10.0);

let camera_controller = CameraController::default();
let walk_speed = size * 3.0;
let camera_controller = CameraController {
walk_speed,
run_speed: 3.0 * walk_speed,
..default()
};

// Display the controls of the scene viewer
info!("{}", camera_controller);
Expand Down

0 comments on commit 3725ff4

Please sign in to comment.