SFML community forums
Help => General => Topic started by: DiscoNic1P on March 07, 2022, 05:17:08 pm
-
I want to use my right trigger from my xbox controller when the player want to shoot a bull. But I don't understand why this line of code is always false. Can someone tell what part is wrong?
if (sf::Joystick::isButtonPressed(0, Joystick::Axis::Z)) inputs.fireMask = true;
-
https://www.sfml-dev.org/tutorials/2.5/window-inputs.php#joystick
Joysticks and triggers aren't buttons, they are axis, use sf::Joystick::getAxisPosition and you can consider it "pressed" when it crosses a threshold
-
So I should write if (sf::Joystick::getAxisPosition(0, Joystick::Axis::Z) == 1.f) inputs.fireMask = true;
-
Joystick axes are in the range -100 to 100. However you might not reach exactly that. For example, my Xbox one controller gets about 99.609 max. So it's safer to test against a threshold (like axisvalue > 90) to make sure it covers most controllers.
Another fun thing to consider: The left trigger is positive, the right trigger is negative. So for the right trigger you'd want axisvalue < -90 and for left trigger you'd have axisvalue > 90.
This was intentional by microsoft. They merged both triggers into one, it's impossible to tell if both triggers are fully pulled in at the same time. But if you use the XInput library (only supports Xbox stuff) then it gives you both triggers separately.