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

Analog trigger values are only updated when the button is pressed #650

Open
Cedev opened this issue Oct 17, 2024 · 2 comments
Open

Analog trigger values are only updated when the button is pressed #650

Cedev opened this issue Oct 17, 2024 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@Cedev
Copy link

Cedev commented Oct 17, 2024

Version

54a361b

Operating system & version

Debian 12

What you did

Added a throttle button using the right analog trigger on a gamepad, and with a custom Axislike that reads the same thing from the CentralInputStore.

Into an input map for this Actionlike

#[derive(Actionlike, PartialEq, Eq, Clone, Copy, Hash, Debug, Reflect)]
pub enum PlayerAction {
    Throttle,
    #[actionlike(Axis)]
    CustomThrottle,
}

impl PlayerAction {
    pub fn default_input_map() -> InputMap<PlayerAction> {
        let mut input_map = InputMap::default();

        input_map.insert(Self::Throttle, GamepadButtonType::RightTrigger2);
        input_map.insert_axis(Self::CustomThrottle, GamepadButtonAxes::TRIGGERS);

        return input_map;
    }
}
return input_map;

Read the value from both the button and the custom axislike, while printing out all of the GamepadEvents that are received

let throttle = action_state.button_value(&PlayerAction::Throttle);
if throttle != 0.0 {println!("Throttle {:?}", throttle);}

let custom_throttle = action_state.value(&PlayerAction::CustomThrottle);
if custom_throttle != 0.0 {println!("CustomThrottle {:?}", custom_throttle);}

The custom Axislike reads from the CentralInputStore.

#[serde_typetag]
impl Axislike for GamepadButtonAxes {
    fn value(&self, input_store: &updating::CentralInputStore, gamepad: Gamepad) -> f32 {
        let up_value = match self.positive {
            Some(button) => input_store.button_value(&GamepadButton::new(gamepad, button)),
            None => 0.0
        };
        let down_value  = match self.negative {
            Some(button) => input_store.button_value(&GamepadButton::new(gamepad, button)),
            None => 0.0
        };
        return up_value - down_value;
    }
}

What you expected to happen

The throttle value to change over the entire range from 0.0 to 1.0 as the analog stick was depressed

What actually happened

Although GamepadEvents are emitted smoothly over depressing the analog stick, reading the value from the button only ever returns 1.0 (or 0.0) and reading the value from the CentralInputStore only returns high values over about 0.75 (or 0.0).

Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.050980393
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.0627451
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.07450981
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.08627451
...
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.3647059
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.38039216
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.4
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.42352942
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.44313726
...
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.7019608
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.7254902
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.74509805
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.7607843
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.78431374
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.79607844
Throttle 1.0
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.80784315
CustomThrottle 0.827451
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.827451
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8392157
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8509804
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8666667
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8784314
Throttle 1.0
CustomThrottle 0.9019608
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.8901961
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.9019608
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.9137255
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.93333334
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 0.9490196
Throttle 1.0
CustomThrottle 0.9490196
Button RightTrigger2 on gamepad Gamepad { id: 0 } is now at 1.0
Throttle 1.0
CustomThrottle 1.0
Throttle 1.0
CustomThrottle 1.0
...

Additional information

The UpdatableInput implementation for GamepadButton only records the axis values for buttons that are either pressed or just_released

impl UpdatableInput for GamepadButton {
type SourceData = GamepadButtonInput<'static>;
fn compute(
mut central_input_store: ResMut<CentralInputStore>,
source_data: StaticSystemParam<Self::SourceData>,
) {
for button in source_data.buttons.get_pressed() {
let value = source_data.axes.get(*button).unwrap_or(1.0);
central_input_store.update_buttonlike(*button, ButtonValue::new(true, value));
}
for button in source_data.buttons.get_just_released() {
let value = source_data.axes.get(*button).unwrap_or(0.0);
central_input_store.update_buttonlike(*button, ButtonValue::new(false, value));
}
}
}

I don't know why ActionState<>::button_value and ActionState<>::clamped_button_value only return 1.0 or 0.0 even though they are documented as returning analog values in examples/axis_input.rs:

// If you don't have a variable trigger, this will just return 0.0 when not pressed and 1.0
// when pressed.
let value = action_state.clamped_button_value(&Action::Throttle);

@Cedev Cedev added the bug Something isn't working label Oct 17, 2024
@alice-i-cecile
Copy link
Contributor

#649 was just merged: is this still an issue for you after those changes?

@alice-i-cecile alice-i-cecile added this to the 0.16 milestone Oct 17, 2024
@Cedev
Copy link
Author

Cedev commented Oct 17, 2024

After

~/.cargo/git/checkouts/leafwing-input-manager-85f1d55581648fda/54a361b$ git log
commit 54a361bde6f548e33b4a0491c03d2fa759f5c6cd (HEAD -> master)
Author: Shute <[email protected]>
Date:   Wed Oct 16 17:49:52 2024 +0100

    Add `value` for button-like inputs (#649)

Sorry, should have used the git hash. I guess I shouldn't trust Cargo.toml after cargo add --git.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants