Game Development Reference
In-Depth Information
How to do it...
To add a game controller or joystick input, perform the following steps:
1. First of all, any controllers that the system recognizes will be available through
inputManager.getJoysticks() . We'll create a new method called as-
signJoysticks() where we apply this.
2. These controllers might turn up differently, in no particular order. It also seems as
if they can sometimes show duplicate axes or some axes as separate controls. How
can we handle this? The safest way might just be to have a for loop, parsing all
controllers and trying to map them to the controls as follows:
Joystick[] joysticks = inputManager.getJoysticks();
if (joysticks != null){
for( Joystick j : joysticks ) {
for(JoystickAxis axis : j.getAxes()){
3. A difference between keyboard and mouse mapping is that we don't actually need
to add new mappings to InputManager . Instead, we tell the joystick what ac-
tions to emit. In this case, it's the x axis on the left stick that is assigned the strafing
action as follows:
axis.assignAxis(InputMapping.StrafeRight.name(),
InputMapping.StrafeLeft.name());
Note
The x and y axes are often simple to map, usually on the left stick on the controller.
The right one might not be as obvious. In this example, it's mapped to the rotation-
X and rotation-Y axes, but might be mapped to the z axis, or rotation-Z as well.
4. In the same way, we can assign buttons to emit specific actions:
button.assignButton("Fire");
Search WWH ::




Custom Search