-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
FreeCam: change walk_speed scaling to avoid sticking to zero/negative speeds
#21486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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 { | ||||||||||||||
| // 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); | ||||||||||||||
|
||||||||||||||
| // `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; | ||||||||||||||
|
||||||||||||||
| controller.walk_speed += factor * amount; | ||||||||||||||
|
||||||||||||||
| 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):
| 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.
There was a problem hiding this comment.
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.
Outdated
There was a problem hiding this comment.
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).
| controller.walk_speed = controller.walk_speed.clamp(0.0, f32::MAX); | |
| controller.walk_speed = controller.walk_speed.clamp(MINIMUM_FREECAM_SPEED, f32::MAX); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.