2024-01-14 13:50:33 +00:00
|
|
|
//! A freecam-style camera controller plugin.
|
|
|
|
//! To use in your own application:
|
|
|
|
//! - Copy the code for the [`CameraControllerPlugin`] and add the plugin to your App.
|
2024-10-05 01:59:52 +00:00
|
|
|
//! - Attach the [`CameraController`] component to an entity with a [`Camera3d`].
|
2024-01-14 13:50:33 +00:00
|
|
|
|
2024-09-24 11:42:59 +00:00
|
|
|
use bevy::{
|
|
|
|
input::mouse::{AccumulatedMouseMotion, AccumulatedMouseScroll, MouseScrollUnit},
|
|
|
|
prelude::*,
|
|
|
|
window::CursorGrabMode,
|
|
|
|
};
|
2024-01-14 13:50:33 +00:00
|
|
|
use std::{f32::consts::*, fmt};
|
|
|
|
|
|
|
|
pub struct CameraControllerPlugin;
|
|
|
|
|
|
|
|
impl Plugin for CameraControllerPlugin {
|
|
|
|
fn build(&self, app: &mut App) {
|
|
|
|
app.add_systems(Update, run_camera_controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Based on Valorant's default sensitivity, not entirely sure why it is exactly 1.0 / 180.0,
|
|
|
|
/// but I'm guessing it is a misunderstanding between degrees/radians and then sticking with
|
|
|
|
/// it because it felt nice.
|
|
|
|
pub const RADIANS_PER_DOT: f32 = 1.0 / 180.0;
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
pub struct CameraController {
|
|
|
|
pub enabled: bool,
|
|
|
|
pub initialized: bool,
|
|
|
|
pub sensitivity: f32,
|
|
|
|
pub key_forward: KeyCode,
|
|
|
|
pub key_back: KeyCode,
|
|
|
|
pub key_left: KeyCode,
|
|
|
|
pub key_right: KeyCode,
|
|
|
|
pub key_up: KeyCode,
|
|
|
|
pub key_down: KeyCode,
|
|
|
|
pub key_run: KeyCode,
|
|
|
|
pub mouse_key_cursor_grab: MouseButton,
|
|
|
|
pub keyboard_key_toggle_cursor_grab: KeyCode,
|
|
|
|
pub walk_speed: f32,
|
|
|
|
pub run_speed: f32,
|
2024-02-19 16:57:20 +00:00
|
|
|
pub scroll_factor: f32,
|
2024-01-14 13:50:33 +00:00
|
|
|
pub friction: f32,
|
|
|
|
pub pitch: f32,
|
|
|
|
pub yaw: f32,
|
|
|
|
pub velocity: Vec3,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CameraController {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
enabled: true,
|
|
|
|
initialized: false,
|
|
|
|
sensitivity: 1.0,
|
|
|
|
key_forward: KeyCode::KeyW,
|
|
|
|
key_back: KeyCode::KeyS,
|
|
|
|
key_left: KeyCode::KeyA,
|
|
|
|
key_right: KeyCode::KeyD,
|
|
|
|
key_up: KeyCode::KeyE,
|
|
|
|
key_down: KeyCode::KeyQ,
|
|
|
|
key_run: KeyCode::ShiftLeft,
|
|
|
|
mouse_key_cursor_grab: MouseButton::Left,
|
|
|
|
keyboard_key_toggle_cursor_grab: KeyCode::KeyM,
|
|
|
|
walk_speed: 5.0,
|
|
|
|
run_speed: 15.0,
|
2024-02-19 16:57:20 +00:00
|
|
|
scroll_factor: 0.1,
|
2024-01-14 13:50:33 +00:00
|
|
|
friction: 0.5,
|
|
|
|
pitch: 0.0,
|
|
|
|
yaw: 0.0,
|
|
|
|
velocity: Vec3::ZERO,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for CameraController {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"
|
|
|
|
Freecam Controls:
|
|
|
|
Mouse\t- Move camera orientation
|
2024-02-19 16:57:20 +00:00
|
|
|
Scroll\t- Adjust movement speed
|
2024-01-14 13:50:33 +00:00
|
|
|
{:?}\t- Hold to grab cursor
|
|
|
|
{:?}\t- Toggle cursor grab
|
|
|
|
{:?} & {:?}\t- Fly forward & backwards
|
|
|
|
{:?} & {:?}\t- Fly sideways left & right
|
|
|
|
{:?} & {:?}\t- Fly up & down
|
|
|
|
{:?}\t- Fly faster while held",
|
|
|
|
self.mouse_key_cursor_grab,
|
|
|
|
self.keyboard_key_toggle_cursor_grab,
|
|
|
|
self.key_forward,
|
|
|
|
self.key_back,
|
|
|
|
self.key_left,
|
|
|
|
self.key_right,
|
|
|
|
self.key_up,
|
|
|
|
self.key_down,
|
|
|
|
self.key_run,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
fn run_camera_controller(
|
|
|
|
time: Res<Time>,
|
|
|
|
mut windows: Query<&mut Window>,
|
2024-07-29 23:38:59 +00:00
|
|
|
accumulated_mouse_motion: Res<AccumulatedMouseMotion>,
|
|
|
|
accumulated_mouse_scroll: Res<AccumulatedMouseScroll>,
|
2024-01-14 13:50:33 +00:00
|
|
|
mouse_button_input: Res<ButtonInput<MouseButton>>,
|
|
|
|
key_input: Res<ButtonInput<KeyCode>>,
|
|
|
|
mut toggle_cursor_grab: Local<bool>,
|
|
|
|
mut mouse_cursor_grab: Local<bool>,
|
|
|
|
mut query: Query<(&mut Transform, &mut CameraController), With<Camera>>,
|
|
|
|
) {
|
|
|
|
let dt = time.delta_seconds();
|
|
|
|
|
|
|
|
if let Ok((mut transform, mut controller)) = query.get_single_mut() {
|
|
|
|
if !controller.initialized {
|
|
|
|
let (yaw, pitch, _roll) = transform.rotation.to_euler(EulerRot::YXZ);
|
|
|
|
controller.yaw = yaw;
|
|
|
|
controller.pitch = pitch;
|
|
|
|
controller.initialized = true;
|
|
|
|
info!("{}", *controller);
|
|
|
|
}
|
|
|
|
if !controller.enabled {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:57:20 +00:00
|
|
|
let mut scroll = 0.0;
|
2024-07-29 23:38:59 +00:00
|
|
|
|
|
|
|
let amount = match accumulated_mouse_scroll.unit {
|
|
|
|
MouseScrollUnit::Line => accumulated_mouse_scroll.delta.y,
|
|
|
|
MouseScrollUnit::Pixel => accumulated_mouse_scroll.delta.y / 16.0,
|
|
|
|
};
|
|
|
|
scroll += amount;
|
2024-02-19 16:57:20 +00:00
|
|
|
controller.walk_speed += scroll * controller.scroll_factor * controller.walk_speed;
|
|
|
|
controller.run_speed = controller.walk_speed * 3.0;
|
|
|
|
|
2024-01-14 13:50:33 +00:00
|
|
|
// Handle key input
|
|
|
|
let mut axis_input = Vec3::ZERO;
|
|
|
|
if key_input.pressed(controller.key_forward) {
|
|
|
|
axis_input.z += 1.0;
|
|
|
|
}
|
|
|
|
if key_input.pressed(controller.key_back) {
|
|
|
|
axis_input.z -= 1.0;
|
|
|
|
}
|
|
|
|
if key_input.pressed(controller.key_right) {
|
|
|
|
axis_input.x += 1.0;
|
|
|
|
}
|
|
|
|
if key_input.pressed(controller.key_left) {
|
|
|
|
axis_input.x -= 1.0;
|
|
|
|
}
|
|
|
|
if key_input.pressed(controller.key_up) {
|
|
|
|
axis_input.y += 1.0;
|
|
|
|
}
|
|
|
|
if key_input.pressed(controller.key_down) {
|
|
|
|
axis_input.y -= 1.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut cursor_grab_change = false;
|
|
|
|
if key_input.just_pressed(controller.keyboard_key_toggle_cursor_grab) {
|
|
|
|
*toggle_cursor_grab = !*toggle_cursor_grab;
|
|
|
|
cursor_grab_change = true;
|
|
|
|
}
|
|
|
|
if mouse_button_input.just_pressed(controller.mouse_key_cursor_grab) {
|
|
|
|
*mouse_cursor_grab = true;
|
|
|
|
cursor_grab_change = true;
|
|
|
|
}
|
|
|
|
if mouse_button_input.just_released(controller.mouse_key_cursor_grab) {
|
|
|
|
*mouse_cursor_grab = false;
|
|
|
|
cursor_grab_change = true;
|
|
|
|
}
|
|
|
|
let cursor_grab = *mouse_cursor_grab || *toggle_cursor_grab;
|
|
|
|
|
|
|
|
// Apply movement update
|
|
|
|
if axis_input != Vec3::ZERO {
|
|
|
|
let max_speed = if key_input.pressed(controller.key_run) {
|
|
|
|
controller.run_speed
|
|
|
|
} else {
|
|
|
|
controller.walk_speed
|
|
|
|
};
|
|
|
|
controller.velocity = axis_input.normalize() * max_speed;
|
|
|
|
} else {
|
|
|
|
let friction = controller.friction.clamp(0.0, 1.0);
|
|
|
|
controller.velocity *= 1.0 - friction;
|
|
|
|
if controller.velocity.length_squared() < 1e-6 {
|
|
|
|
controller.velocity = Vec3::ZERO;
|
|
|
|
}
|
|
|
|
}
|
return Direction3d from Transform::up and friends (#11604)
# Objective
Drawing a `Gizmos::circle` whose normal is derived from a Transform's
local axes now requires converting a Vec3 to a Direction3d and
unwrapping the result, and I think we shold move the conversion into
Bevy.
## Solution
We can make
`Transform::{left,right,up,down,forward,back,local_x,local_y,local_z}`
return a Direction3d, because they know that their results will be of
finite non-zero length (roughly 1.0).
---
## Changelog
`Transform::up()` and similar functions now return `Direction3d` instead
of `Vec3`.
## Migration Guide
Callers of `Transform::up()` and similar functions may have to
dereference the returned `Direction3d` to get to the inner `Vec3`.
---------
Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: Joona Aalto <jondolf.dev@gmail.com>
2024-02-02 15:05:35 +00:00
|
|
|
let forward = *transform.forward();
|
|
|
|
let right = *transform.right();
|
2024-01-14 13:50:33 +00:00
|
|
|
transform.translation += controller.velocity.x * dt * right
|
|
|
|
+ controller.velocity.y * dt * Vec3::Y
|
|
|
|
+ controller.velocity.z * dt * forward;
|
|
|
|
|
|
|
|
// Handle cursor grab
|
|
|
|
if cursor_grab_change {
|
|
|
|
if cursor_grab {
|
|
|
|
for mut window in &mut windows {
|
|
|
|
if !window.focused {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-12 15:49:03 +00:00
|
|
|
window.cursor_options.grab_mode = CursorGrabMode::Locked;
|
|
|
|
window.cursor_options.visible = false;
|
2024-01-14 13:50:33 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for mut window in &mut windows {
|
2024-08-12 15:49:03 +00:00
|
|
|
window.cursor_options.grab_mode = CursorGrabMode::None;
|
|
|
|
window.cursor_options.visible = true;
|
2024-01-14 13:50:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle mouse input
|
2024-07-29 23:38:59 +00:00
|
|
|
if accumulated_mouse_motion.delta != Vec2::ZERO && cursor_grab {
|
2024-01-14 13:50:33 +00:00
|
|
|
// Apply look update
|
|
|
|
controller.pitch = (controller.pitch
|
2024-07-29 23:38:59 +00:00
|
|
|
- accumulated_mouse_motion.delta.y * RADIANS_PER_DOT * controller.sensitivity)
|
2024-01-14 13:50:33 +00:00
|
|
|
.clamp(-PI / 2., PI / 2.);
|
2024-07-29 23:38:59 +00:00
|
|
|
controller.yaw -=
|
|
|
|
accumulated_mouse_motion.delta.x * RADIANS_PER_DOT * controller.sensitivity;
|
2024-01-14 13:50:33 +00:00
|
|
|
transform.rotation =
|
|
|
|
Quat::from_euler(EulerRot::ZYX, 0.0, controller.yaw, controller.pitch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|