Reading input from an XBOX 360 pad using glfw pt 1

Published by GaijinWolf under Programming.

In past projects when I needed to use a Xbox 360 pad for input I’d use DirectInput and XInput to do the magic. However this usually means having to use a lot of api calls and directX stuff to make this work. With the use of the OpenGL windows library GLFW it is super easy to use xbox pads for reading input.

Testing if a pad is connected

Testing if a pad is connected is so simple. Simply use the line

and you get a result stating if it’s connected ,1 for yes, 0 for no.Easily abusable as a bool value. The constant GLFW_PLAYER_1 is to check if player 1′s pad is connected, GLFW_PLAYER_2 for player 2 etc

Recording Axes’ Values

According to glfw, there are 5 axes for the pad. The x and y axes for the left and right analogue stick and also, curiously, the analogue shoulder triggers count as the ‘last’ axis. All of the axes record values ranging from -1.0 to +1.0 as you expect, i.e left on the x axis is the negative direction, right is the positive direction.With the shoulder triggers depressing the left one all the way is -1.0, and conversely depressing the right one all the way is 1.0. If you want to check if both are pressed there are tricks you can use, but we will cover that another time.

You use a handy function to record all 5 values into an array of 5 floats for reference in the next game loop like so:

Recording Button Values

Glfw records 10 possible button values from the Xbox 360 pad: A,B,X,Y,RB,LB,Start,Back, and the clicks of the left and right analogue sticks. Unfortunately I don’t know how you would record the D-PAD values, which is a major drawback.These values are recorded into an array of 10 unsigned chars, with a value of for pressed, for not pressed

The Sound of White(Analogue) noise!

Unfortunately both analogue sticks create some signal noise, even if it’s extremely small! If you’ve programmed pads before, of course you know this, but this wreaks havoc if you don’t know what’s going on. To combat this it’s always good to put in a ‘threshold’. If any analogue values are below this then change them to 0.0 to produce the intended behaviour, I find 0.15 and -0.15 to be a good range to clamp to 0.0, but you can tweak the value to your own taste e.g.

Putting it all together

As an example I have a class below you can freely use in any project of your choice, just please mention where it came from in a comment. Additionally if anyone has any extensions or suggestions to add to this please comment!

joypad.h

joypad.cpp

Closing Comments

There are a plethora of alternative ways to use some of the above functions, for example you might want to change the class so it fires an event to an input event handler to say a button has been pressed or joystick moved, and ‘map’ the event to an action. This way you can have easily configurable controls changeable by the user.

There is another important thing to keep in mind, and that is ‘dead zones’ for specific axes. What this means I will elaborate on in part 2, and update the code accordingly. Take care, y’all

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>