SFML community forums

Help => Window => Topic started by: Antshockey on April 25, 2021, 03:56:57 pm

Title: XboxOne GamePad Triggers
Post by: Antshockey on April 25, 2021, 03:56:57 pm
Hi Everyone,

I've been working on implementing XboxOne GamePad support into my Windows game.

Unfortunately I've discovered an issue with getting the values of the triggers.

Essentially both triggers are mapped to the Z axis, one being positive and the other negative. This means that it is impossible to tell how far in each trigger is pulled, or the difference between both triggers held down fully or completely untouched as both of those states give a Z value of 0.

Has anyone else had this issue and is there a solution?
Title: Re: XboxOne GamePad Triggers
Post by: kojack on April 25, 2021, 07:16:06 pm
This was an intentional design decision by Microsoft when they made the Direct Input drivers for xbox controllers. I've seen them say that it is because it makes it easier to do car games with accelerator/brake on the two triggers. Any direct input based input lbirary (like sfml) will have this issue.

The way to bypass it is to use XInput instead. It exposes all features of the xbox controller as well as keeping the triggers separate. Luckily it's also very easy to use.
There's no setup or initialisation. include the xinput.h header, link with the xinput library, then every update in the game do:
XINPUT_STATE state;
DWORD result = XInputGetState(0, &state);
(The 0 is the index (0-3) of the controller you want)
The triggers can then be read using state.GamePad.bLeftTrigger and state.GamePad.bRightTrigger (values 0-255).
Using vibration is just as easy. (make state variable, set 2 motor values, call set state)
Title: Re: XboxOne GamePad Triggers
Post by: Antshockey on April 25, 2021, 07:46:24 pm
I thought it may have been something along those lines.

I'm currently using the C# bindings so I may have to pull in a different input library for the pad.

Thanks for the help
Title: Re: XboxOne GamePad Triggers
Post by: kojack on April 25, 2021, 07:59:19 pm
I haven't tried it myself, but there's https://github.com/speps/XInputDotNet
It says it lets any .Net app access xinput.

Looking at their demo source, it would be used like:
GamePadState state = GamePad.GetState(PlayerIndex.One);
Then look in state.Triggers.Left and state.Triggers.Right for the trigger values.