Search code examples
c++visual-studioallegro5

Allegro 5: Check if Joystick is "neutral"


I am implementing joystick controls in my Allegro 5 game in Visual Studio 2017. I have tried doing this two ways: by having my event queue listen for joystick events in my main loop, and by having an ALLEGRO_JOYSTICK_STATE update once per frame in my main loop with the joystick's current state. However, when looking at the pos value of the joystick, I find that in both cases it does not actually return the x and y positions to 0.0 when the stick returns to neutral, but instead to very low values like 0.01 or 0.02. For the most part this doesn't cause problems, but occasionally my player will continue to move very slowly even when the joystick is in neutral because the pos values are too high.

Is there a reliable way to check whether or not the joystick is being pushed? I initially thought of "resetting" the position to 0.0, 0.0 every frame and then only changing it if joystick movement was detected, but the event queue doesn't actually detect movement every frame even when the joystick was held down, causing the player to "stutter" while moving. Since the "neutral" values are very low, I also thought of checking if they were below some value and considering the stick "neutral" if they were, but this seems very unreliable given that I see different numbers every time I let go of the joystick and I'm not sure how high they can go.


Solution

  • The values vary from -1.0 to 1.0. You may want to have a "deadzone" where any value less than, say, 0.05 is treated as 0. Eventually you might let players configure this.

    Using events is preferable to polling as you avoid the risk of missing events between polls.