Cross-platform Controller Code

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Andrei
Posts: 7
Joined: Wed Feb 10, 2010 3:26 am

Cross-platform Controller Code

Post by Andrei »

The alliteration was totally unintended, I swear!

I'm currently working on a game development project, and our team is planning to implement controller input. I've looked at the Joystick Example, but it's not exactly the clearest code, and doesn't demonstrate how to access multiple axis, for example.

We want to use the wired 360 gamepad, at least, but unfortunately we're forced into being cross-platform (one of the developers uses Linux only), and therefore we can't use XInput.

Are there any examples of using a 360 gamepad using just Irrlicht input methods, or should I try to map all the axis/buttons to it myself?

As a side question - what exactly is a POV hat, on controllers?

Edit: When loading the Controller as a Joystick, it comes up with only 10 buttons and 5 axis's. The Controller has 10 face buttons, 2 shoulder buttons, 2 triggers, and 2 control sticks - which should give 6 axis's and 12 buttons, right?
Andrei
Posts: 7
Joined: Wed Feb 10, 2010 3:26 am

Post by Andrei »

I looked through what was being returned manually, and I found the following mappings for the 360 gamepad for Irrlicht. They may help someone here.

Left Analog Stick:
L - Axis 0 +
R - Axis 0 -
U - Axis 1 +
D - Axis 1 -
Right Analog Stick:
L - Axis 4 +
R - Axis 4 -
U - Axis 3 +
D - Axis 3 -
Left Trigger: Axis 2 -
Right Trigger: Axis 2 +

D-Pad:
U - POV 0
R - POV 90
D - POV 180
L - POV 270

Back: Button 6
Start: Button 7
A: Button 0
B: Button 1
X: Button 2
Y: Button 3
LB: Button 4
RB: Button 5
[Center Button: Unsupported?]

This is what is returned on Windows. I'm still wondering what it would be like on Linux - where the actual axis's present (0-4) plus the POV hats (2 more) will overflow the NUMBER_OF_AXES, which is 6.
DavidJE13
Posts: 165
Joined: Tue Jan 09, 2007 7:17 pm

Post by DavidJE13 »

Each OS will have a different button mapping, this is an annoyance you have to live with, because of the various drivers. For example, while Andrei is right that the centre button is inaccessible to Windows, both Linux and OSX report it. They will also report the buttons & axes in a different order. A pain, yes.
A POV hat is a point-of-view hat. It's commonly called the D-pad, though the D-pad is often sent as 4 buttons instead of a POV value, and on linux it's sent as 2 axes analog stick style. Irrlicht on OSX doesn't support POVs yet, unless they report themselves as buttons.

The example *does* show how to access multiple axes; look at the lines

Code: Select all

(f32)joystickData.Axis[SEvent::SJoystickEvent::AXIS_X] / 32767.f;
and

Code: Select all

(f32)joystickData.Axis[SEvent::SJoystickEvent::AXIS_Y] / -32767.f;
I was working on a class to do all the mapping automatically, as well as adjust dynamically to added, removed & replaced joysticks, but this hit a hurdle and is temporarily stopped. It's not too difficult to do your own mapping though, just do a few tests.


Also, on Windows only (i.e. not Linux or OSX) both of the triggers are represented by 1 axis - it is centred normally, and goes negative when one is pressed and positive when the other is. Obviously this means that it is impossible to determine if both are pressed simultaneously - a serious problem with the default windows drivers. (some Windows games get around this, not sure how)
I think the controller only has 8 face buttons in Windows - the D-pad is reported as the POV, leaving A, B, X, Y, stick 1, stick 2, START and BACK (or SELECT or whatever), totalling 8. Plus RB and LB gives you the 10 reported.

(btw, it's axes not axises or axis's!)
Andrei
Posts: 7
Joined: Wed Feb 10, 2010 3:26 am

Post by Andrei »

First off, thanks for the correct plural - I always forget that rule.

Secondly, thanks for the note that the button mappings are different for every OS - that probably would've been an issue later down the road.

I guess the best way to go is to create a custom control input and have each function able to be remapped.
Brainsaw
Posts: 1177
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

I recommend using my irrConfigController (see CodeSnippets forum) where the user can assign which button/axis is used for which control, though I haven't yet tried it under Linux (as I don't have that installed) it still should work as it's pure Irrlicht code.
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
Andrei
Posts: 7
Joined: Wed Feb 10, 2010 3:26 am

Post by Andrei »

Brainsaw wrote:I recommend using my irrConfigController (see CodeSnippets forum) where the user can assign which button/axis is used for which control, though I haven't yet tried it under Linux (as I don't have that installed) it still should work as it's pure Irrlicht code.
I actually saw your code shortly after posting this, and read through it to figure out how to access joysticks. It was good for what it was, but I decided to implement the custom controllers myself, as I'm trying to avoid using irrlicht's gui methods.

The way I implemented it was a much more lightweight version of yours - I had a class Trigger that evaluated either to true or false, and whenever input was processed, each function was checked against the trigger. Since I didn't have to worry about GUI commands and conflicts, the code ended up being under 50 lines long.
Brainsaw
Posts: 1177
Joined: Wed Jan 07, 2004 12:57 pm
Location: Bavaria

Post by Brainsaw »

Nice to see my code helped you nevertheless ;)
Dustbin::Games on the web: https://www.dustbin-online.de/

Dustbin::Games on facebook: https://www.facebook.com/dustbingames/
Dustbin::Games on twitter: https://twitter.com/dustbingames
Post Reply