Skip to content
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

Scale input to account for deadzones #17015

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

BenjaminBrienen
Copy link
Contributor

@BenjaminBrienen BenjaminBrienen commented Dec 29, 2024

Objective

Fixes #3450

Solution

Scale the input to account for the range

Testing

Updated unit tests

Migration Guide

GamepadButtonChangedEvent.value is now linearly rescaled to be from 0.0..=1.0 (instead of low..=high) and GamepadAxisChangedEvent.value is now linearly rescaled to be from -1.0..=0.0/0.0..=1.0 (accounting for the deadzone).

@BenjaminBrienen BenjaminBrienen self-assigned this Dec 29, 2024
@BenjaminBrienen BenjaminBrienen added A-Input Player input via keyboard, mouse, gamepad, and more D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Needs-Review Needs reviewer attention (from anyone!) to move forward C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Dec 29, 2024
@BenjaminBrienen
Copy link
Contributor Author

I can't work on fixing the unit tests until this is fixed:

error[E0599]: no method named `to_string` found for reference `&str` in the current scope
   --> crates\bevy_ecs\src\name.rs:252:24
    |
252 |         Ok(Name::new(v.to_string()))
    |                        ^^^^^^^^^ method not found in `&str`
    |
    = help: items from traits can only be used if the trait is in scope
help: trait `ToString` which provides `to_string` is implemented but not in scope; perhaps you want to import it
    |
3   + use crate::alloc::string::ToString;

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I agree that this is the right default behavior. Otherwise setting a deadzone makes it very hard / impossible to get small directional inputs.

This PR needs a bit of cleanup still. It would be particularly helpful to cleanup the tests: right now, the values just look like magic numbers. Maybe we should add some algebra + consts inside of the Some arms?

@alice-i-cecile
Copy link
Member

For the alloc error, I think you need to fix the feature flags in a new PR. @bushrat011899, FYI.

@BenjaminBrienen
Copy link
Contributor Author

BenjaminBrienen commented Dec 29, 2024

For the alloc error, I think you need to fix the feature flags in a new PR. @bushrat011899, FYI.

He's already got that fixed in the no_std bevy_input PR.

@bushrat011899
Copy link
Contributor

Can confirm I have this issue resolved in the no_std bevy_input PR, along with some other issues with how bevy_input feature gates bevy_reflect

@BenjaminBrienen BenjaminBrienen force-pushed the scaled-deadzones branch 2 times, most recently from 3eac744 to 91be8f2 Compare December 30, 2024 05:16
@alice-i-cecile alice-i-cecile added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Dec 30, 2024
Copy link
Contributor

It looks like your PR is a breaking change, but you didn't provide a migration guide.

Could you add some context on what users should update when this change get released in a new version of Bevy?
It will be used to help writing the migration guide for the version. Putting it after a ## Migration Guide will help it get automatically picked up by our tooling.

@alice-i-cecile
Copy link
Member

Let's do a quick migration guide noting this change (since it's otherwise surprising), but this LGTM now. Good work on the tests; I like the balance you struck.

@BenjaminBrienen
Copy link
Contributor Author

Sorry for the noise. I realized that I could make the implementation and test data cleaner by storing the raw values in the gamepad, which means the old values will be raw values. Everything is very optimal now, imo.

@BenjaminBrienen
Copy link
Contributor Author

Migration guide made me realize the doc comment should be improved. Both are done now.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I like the latest changes, and particularly appreciate the comments on the tests.

Copy link
Contributor

@Shatur Shatur left a comment

Choose a reason for hiding this comment

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

Looks good to me! Left a few small suggestions.

Personally, I think deadzone is a more high-level functionality and should be handled by input managers🤔 But since we already have this feature, it's definitely an improvement!
I handle deadzones in the same way in the bevy_enhanced_input library.

fn should_register_change(&self, new_value: f32, old_value: Option<f32>) -> bool {
if old_value.is_none() {
return true;
#[inline(always)]
Copy link
Contributor

@Shatur Shatur Dec 30, 2024

Choose a reason for hiding this comment

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

Never inline private methods. Same for other places. Here is a good article about it.

Suggested change
#[inline(always)]

crates/bevy_input/src/gamepad.rs Show resolved Hide resolved
}
}

/// A linear remapping of `value` from `old` to `new`
Copy link
Contributor

@Shatur Shatur Dec 30, 2024

Choose a reason for hiding this comment

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

Use . in the end for all doc comments in Rust:

Suggested change
/// A linear remapping of `value` from `old` to `new`
/// A linear remapping of `value` from `old` to `new`.

(-0.43, Some(-0.84), Some(-0.43)),
(-0.05, Some(-0.055), Some(0.0)),
(-0.95, Some(-0.945), Some(-1.0)),
// enough increase to change
Copy link
Contributor

@Shatur Shatur Dec 30, 2024

Choose a reason for hiding this comment

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

I would suggest to split this single test into multiple small tests named like enough_increase_to_change instead of comments. But it's up to you since it was like this before :)

@alice-i-cecile
Copy link
Member

Personally, I think deadzone is a more high-level functionality and should be handled by input managers

Agreed. In the medium term, I think this functionality should be removed from bevy_input and moved up a level. But until we have a first party input manager / action crate we're blocked there.

@BenjaminBrienen
Copy link
Contributor Author

BenjaminBrienen commented Dec 30, 2024

I think deadzones and scaling are good here because they address hardware limitations. Before then, the events are "raw". The part I would move into another crate is actually the clamping/rounding. Those are more about what "feels right". But then again, the same applies for the press/release/change thresholds, and I don't think those can be moved out.

@Shatur
Copy link
Contributor

Shatur commented Dec 30, 2024

I would consider bevy_input as a source for raw input and its processing being a higher-level abstraction. That's what I currently do with my crate (I use methods that return raw values and let user apply any modifiers on top of them).

We on the same page with @alice-i-cecile, this PR is good.
I just wanted to share my thoughts for possible future considerations.

@BenjaminBrienen BenjaminBrienen added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Input Player input via keyboard, mouse, gamepad, and more C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Deadzone clipping in AxisSettings and ButtonSettings results in a compression of values
4 participants