LibGDX Controller Support

Anything libgdx related goes here!

LibGDX Controller Support

Postby AaronLandon » Mon Jan 27, 2020 11:30 am

I'm getting ready to release a game on Steam in a few weeks. For the most part I've had almost zero problems with controllers in LibGDX until I started adding support for XBox. Unlike all other controllers (PS4, Nintendo Switch, Logitech, Z-DV, Steam Controller, etc), everything just magically worked, I was able to easily creating mappings and such. However, XBox One controller required me to first download a driver (Mac) from their website, otherwise LWJGL3 controller backend would throw errors. However, even upon solving this, I've noticed that the LibGDX controller do not line up with what I'm observing. I have of course since solved this by creating my own custom mapping. However, I noticed that for XBox, there seemed to be platform dependent key codes, which makes mapping more complicated. I'm OK manually mapping these myself but am concerned since the keycodes I've mapped are different that what the LibGDX libs show. My worry is specifically that people will have problems using their XBox controllers with my game. This has already been an issue with two beta testers.

Re: LibGDX Controller Support

Postby shatterblast » Mon Jan 27, 2020 4:00 pm

If I remember right, I think that XBox controllers work with Microsoft stuff a little more closely than with other operating systems. For example, XBox controllers last I checked do not work with Linux. DirectX originally derived from OpenGL, but Microsoft drivers handle differently in this case.

MS Windows will actually assign random values to the keys, so assigning them yourself can prove to be a problem. It's possible for two different players to receive two different input mappings for this reason.

The standard fix includes bringing up the MS Windows controller setup and let the player save an input mapping for your game itself if necessary. That will cure the problem in most cases.

I don't remember the command to do so, but I believe you would need to make a system call to Windows itself. You could probably think of it as the player needing to set their controller before your actual game starts. For this reason, you might want an intro screen like the old style, before your game itself even loads. This is kind of a common necessity for OpenGL games supporting controllers on Windows. This is why many tend to favor the mouse and keyboard instead.

On the good side, you shouldn't have to worry about altering your default mappings. Just make sure they stay consistent for the same player after each re-boot of his or her device.

You would probably want to write a test in code that checks whether the mappings stay the same each time. Again, they can be unique for each player and each system ran by MS Windows.

Also, I believe MS Windows will allow your game to check for the presence of a file that has a controller configuration with the name of your game or whatever.
Posts: 653
Joined: Sun Jul 06, 2014 1:14 pm

Re: LibGDX Controller Support

Postby RafaSKB » Mon Feb 17, 2020 2:09 pm

Hi! Just for future reference, there are some libraries that deal with these crazy mappings for you, providing a safe and consistent way of dealing with controllers.

They use a universal mapping, so you only have to map your actions once, and they will work just fine with hundreds of controllers of different consoles, brands, and types.

One of them is gdx-controllerutils:
And the other is sdl2gdx:

Both are excellent :)
Awesome libGDX - A curated list of resources and tutorials to help developers make awesome games.

Grashers - Trash-themed roguelike set in space.
Posts: 144
Joined: Sat Aug 01, 2015 5:14 am
Location: São Paulo, Brazil

Return to Libgdx

Who is online

Users browsing this forum: Google [Bot], MSN [Bot] and 1 guest