Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions crates/bevy_camera_controller/src/free_cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,25 @@ pub fn run_freecam_controller(
return;
}

let mut scroll = 0.0;

let amount = match accumulated_mouse_scroll.unit {
MouseScrollUnit::Line => accumulated_mouse_scroll.delta.y,
MouseScrollUnit::Pixel => {
accumulated_mouse_scroll.delta.y / MouseScrollUnit::SCROLL_UNIT_CONVERSION_FACTOR
}
};
scroll += amount;
controller.walk_speed += scroll * controller.scroll_factor * controller.walk_speed;
controller.run_speed = controller.walk_speed * 3.0;

if amount != 0.0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this if is kind of redundant, but that's a nitpick.

// scale the speed exponentially with the scroll factor as the base.
let scroll_plus_one = controller.scroll_factor.max(0.0) + 1.0;
let log_speed_plus_one = bevy_math::ops::ln(controller.walk_speed + 1.0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the logic behind doing ln(x + 1) here? This seems like a very unnatural choice of function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my thought process was that I want the speed to represent a^x - 1 where a is the scroll factor, and adjust the speed with the derivative at each scroll step.
So ln(speed+1) is roughly the inverse of that to get x from the speed,
scroll_factor starts at 0, but I need it to start at 1 to be the base
and a^x is (very) roughly the derivative of the function I started with.
Then subtract a value close to 1 to get the scaling factor below 1.

// `factor` will approach but never reach 0 as `walk_speed` approaches 0:
// Importantly, we want to avoid ever reaching exactly 0 so we don't get stuck there.
let factor = bevy_math::ops::powf(scroll_plus_one, log_speed_plus_one) - 0.999;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(scroll + 1)^ln(speed + 1) is difficult to interpret. One could equally rewrite it as:
(speed + 1)^ln(scroll + 1), which is slightly easier to see that the walking speed is being... kinda raised to some power given by the scroll factor. But still, this doesn't seem to make any sense unit wise. The speed is some sort of velocity, in say, m/s. 1 has no unit, so it's weird we're adding it on there, and then we're raising it to the power ln(scroll + 1), giving the result a unit of (m/s)^ln(scroll + 1), which is really weird, what does this value mean?

controller.walk_speed += factor * amount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the goal is to scale the walk speed with scrolling, I would suggest refraining from additions on it completely and directly scale the value:

Suggested change
controller.walk_speed += factor * amount;
let scale_factor = bevy_math::ops::exp(amount * controller.scroll_factor);
controller.walk_speed *= scale_factor;

alternatively one can do (if this kind of interpretation for scroll_factor is preferred, but it additionaly requires that it is strictly positive):

Suggested change
controller.walk_speed += factor * amount;
let scale_factor = bevy_math::ops::powf(controller.scroll_factor, amount);
controller.walk_speed *= scale_factor;

Importantly this achieves things like being independent of how the scroll amounts are split across frames, which I believe the current code doesn't do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

xa^e looks much simpler so I think I'll switch it to that, but if you don't mind I would really appreciate if you could comment on my train of thought behind my original logic above.

// avoid negative speeds.
controller.walk_speed = controller.walk_speed.clamp(0.0, f32::MAX);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest preventing a zero here by using some minimal value for the walk speed. Even if it's very small, like 0.000001 (I wouldn't suggest a smaller value than that because of floats).

Suggested change
controller.walk_speed = controller.walk_speed.clamp(0.0, f32::MAX);
controller.walk_speed = controller.walk_speed.clamp(MINIMUM_FREECAM_SPEED, f32::MAX);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this but I decided that being able to reach a speed of zero might be desired; sometimes, you might want to be sure that you cant change your position by accidentally pressing the wrong buttons. I'm open to have my opinion on this changed, though.

controller.run_speed = controller.walk_speed * 3.0;
}

// Handle key input
let mut axis_input = Vec3::ZERO;
Expand Down