From c454db88a3593f57a59ad2216e6f6da5a3a52de6 Mon Sep 17 00:00:00 2001 From: Rich Churcher Date: Sat, 14 Sep 2024 03:47:04 +1200 Subject: [PATCH] 2580 Split examples PR feedback (#15181) # Objective Applies feedback from previous PR #15135 'cause it got caught up in the merge train :steam_locomotive: I couldn't resist including roll, both for completeness and due to playing too many games that implemented it as a child. cc: @janhohenheim --- Cargo.toml | 1 - examples/camera/camera_orbit.rs | 85 +++++++++++++++++---------------- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0609e02c33..ddd4b94448 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3266,7 +3266,6 @@ description = "Shows how to orbit a static scene using pitch, yaw, and roll." category = "Camera" wasm = true - [package.metadata.example.fps_overlay] name = "FPS overlay" description = "Demonstrates FPS overlay" diff --git a/examples/camera/camera_orbit.rs b/examples/camera/camera_orbit.rs index c0e80e636a..366bf8be44 100644 --- a/examples/camera/camera_orbit.rs +++ b/examples/camera/camera_orbit.rs @@ -1,16 +1,36 @@ //! Shows how to orbit camera around a static scene using pitch, yaw, and roll. +//! +//! See also: `first_person_view_model` example, which does something similar but as a first-person +//! camera view. use std::{f32::consts::FRAC_PI_2, ops::Range}; -use bevy::prelude::*; +use bevy::{input::mouse::AccumulatedMouseMotion, prelude::*}; -#[derive(Debug, Default, Resource)] +#[derive(Debug, Resource)] struct CameraSettings { pub orbit_distance: f32, - // Multiply keyboard inputs by this factor - pub orbit_speed: f32, + pub pitch_speed: f32, // Clamp pitch to this range pub pitch_range: Range, + pub roll_speed: f32, + pub yaw_speed: f32, +} + +impl Default for CameraSettings { + fn default() -> Self { + // Limiting pitch stops some unexpected rotation past 90° up or down. + let pitch_limit = FRAC_PI_2 - 0.01; + Self { + // These values are completely arbitrary, chosen because they seem to produce + // "sensible" results for this example. Adjust as required. + orbit_distance: 20.0, + pitch_speed: 0.003, + pitch_range: -pitch_limit..pitch_limit, + roll_speed: 1.0, + yaw_speed: 0.004, + } + } } fn main() { @@ -24,18 +44,10 @@ fn main() { /// Set up a simple 3D scene fn setup( - mut camera_settings: ResMut, mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { - // Limiting pitch stops some unexpected rotation past 90° up or down. - let pitch_limit = FRAC_PI_2 - 0.01; - - camera_settings.orbit_distance = 10.0; - camera_settings.orbit_speed = 1.0; - camera_settings.pitch_range = -pitch_limit..pitch_limit; - commands.spawn(( Name::new("Camera"), Camera3dBundle { @@ -94,15 +106,15 @@ fn instructions(mut commands: Commands) { )) .with_children(|parent| { parent.spawn(TextBundle::from_section( - "W or S: pitch", + "Mouse up or down: pitch", TextStyle::default(), )); parent.spawn(TextBundle::from_section( - "A or D: yaw", + "Mouse left or right: yaw", TextStyle::default(), )); parent.spawn(TextBundle::from_section( - "Q or E: roll", + "Mouse buttons: roll", TextStyle::default(), )); }); @@ -111,40 +123,29 @@ fn instructions(mut commands: Commands) { fn orbit( mut camera: Query<&mut Transform, With>, camera_settings: Res, - keyboard_input: Res>, + mouse_buttons: Res>, + mouse_motion: Res, time: Res