Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to use correctly the trigger  (Read 1779 times)

0 Members and 1 Guest are viewing this topic.

DiscoNic1P

  • Newbie
  • *
  • Posts: 3
    • View Profile
How to use correctly the trigger
« 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;

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: How to use correctly the trigger
« Reply #1 on: March 07, 2022, 06:37:00 pm »
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

DiscoNic1P

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: How to use correctly the trigger
« Reply #2 on: March 07, 2022, 06:38:49 pm »
So I should write if (sf::Joystick::getAxisPosition(0, Joystick::Axis::Z) == 1.f) inputs.fireMask = true;

kojack

  • Sr. Member
  • ****
  • Posts: 314
  • C++/C# game dev teacher.
    • View Profile
Re: How to use correctly the trigger
« Reply #3 on: March 08, 2022, 07:17:49 am »
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.