Skip to content

Commit 63a70ed

Browse files
authored
Change friction behavior in freecam to use smooth nudge #21455 (#21464)
# Objective - Fixes #21455 - Change the behavior of freecam to use smooth nudge instead of an exponential, per-frame fall off ## Solution - As written, the freecam's velocity decays by half every loop. - Instead, I used smooth_nudge, which takes the time difference into account so that its behavior isn't depended on the fixed main loop's hz. - The freecam plugin took a f32 that defaulted to 0.5, and was written so lower numbers would have faster decay. In smooth nudge terms, you could describe this as 64 log 2 at the fixed time step of 64 hz, which is a decay rate of around 44.3. - I used 40.0 as this seems like an easily understandable default. - This is a breaking change because now higher numbers (instead of lower numbers) supplied to `friction`, will increase the decay rate. However, all of the examples that use freecamplugin relied on the default() friction value, and it was only recently added, so I think this is okay. ## Testing - I tested this change in the examples that use freecam; ---
1 parent 78f36cf commit 63a70ed

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

crates/bevy_camera_controller/src/free_cam.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use bevy_input::mouse::{
2323
};
2424
use bevy_input::ButtonInput;
2525
use bevy_log::info;
26-
use bevy_math::{EulerRot, Quat, Vec2, Vec3};
26+
use bevy_math::{EulerRot, Quat, StableInterpolate, Vec2, Vec3};
2727
use bevy_time::{Real, Time};
2828
use bevy_transform::prelude::Transform;
2929
use bevy_window::{CursorGrabMode, CursorOptions, Window};
@@ -119,7 +119,7 @@ impl Default for FreeCam {
119119
walk_speed: 5.0,
120120
run_speed: 15.0,
121121
scroll_factor: 0.5,
122-
friction: 0.5,
122+
friction: 40.0,
123123
pitch: 0.0,
124124
yaw: 0.0,
125125
velocity: Vec3::ZERO,
@@ -243,8 +243,8 @@ pub fn run_freecam_controller(
243243
};
244244
controller.velocity = axis_input.normalize() * max_speed;
245245
} else {
246-
let friction = controller.friction.clamp(0.0, 1.0);
247-
controller.velocity *= 1.0 - friction;
246+
let friction = controller.friction.clamp(0.0, f32::MAX);
247+
controller.velocity.smooth_nudge(&Vec3::ZERO, friction, dt);
248248
if controller.velocity.length_squared() < 1e-6 {
249249
controller.velocity = Vec3::ZERO;
250250
}

0 commit comments

Comments
 (0)