Joystick in Java with JInput

Posted: September 2, 2012 in Uncategorized
Tags: , , , ,

I got Logitech Double Action gamepad from a friend and I wondered how can I use it in Java and I come acrose JInput. Then I made a test program, something like what you can find in Windows. You can download this program and its source code at the bottom of this post.

JInput Joystick Test

How to use JInput?

To use joystick in your java games or apps you can use JInput. First download latest release of it (current is jinput_nightly_20120831.zip). You can also download JInput javadoc to link it with NetBeans or you can look it online here.

First you need to move JInput files to certain places and link jar file to your project. I will describe how I did it in NetBeans, probably is almost the same with Eclipse.

  1. First unzip jinput zip package that you downloaded and move “jinput.jar” somewhere for later use, I usually copy jar libraries files into my java installation dir (C:\Program Files \Java\) and then link them to project from there.
  2. Then you need to move other files into root director of your project. JInputJoystickTest root folder
  3. Now go to NetBeans and click on folder named “Libraries” in your project with right mouse button and select “Add JAR/Folder…” then locate “jinput.jar” file. Now should jar file be visible in “Libraries” folder.

Adding jar file to a project in NetBeans

Now that you put all JInput files/libraries where they need to be, you can start using JInput. Note that files (jinput-dx8.dll, jinput-raw.dll, …) that you put into your root folder of a project are used when you run project inside the NetBeans. When you will build your project you will need to copy this files (jinput-dx8.dll, jinput-raw.dll, …) into folder where your game/app jar file will be (into “dist” folder, inside your project folder when you build a project). File “jinput.jar” will be copied automatically.

Using JInput in code

For easier and cleaner use I wrote one additional class called JInputJoystick. I will explain how to use this class, so click on the link, download it and copy it to your project source folder among your other classes/files. If you don’t want to use this class, you can look at it to see how to use JInput directly. You can also download JInputJoystick javadoc.


// First you need to create controller.
JInputJoystick joystick = new JInputJoystick(Controller.Type.STICK, Controller.Type.GAMEPAD);

When I created a controller, I put two types of a controller to the method (you can also put just one type), one is a stick type and other is a gamepad. I also put stick type in, because my Logitech Dual Action gamepad is recognized as a stick type. I also used Xbox MadCatz gamepad to test and it was recognized as a gamepad type. And there it is a little difference between those two types. Xbox controller have 6 axis, Logitech Dual Action controller have 4 axis. And thous axis, right and left joystick that you use with your thumbs, don’t have the same name.
So (I’m just guessing that) controllers with 4 axis are, in JInput, recognized as a stick type and controllers with 6 axis as a gamepad type. So it’s probably good to make your game/app for both, stick and gamepad type.

Names of axis:

  • Xbox MadCatz
    • left joystick: X Axis and Y Axis
    • right joystick: X Rotation and Y Rotation
    • third “joystick” that you use with your pointer fingers: Z Axis
  • Logitech Dual Action
    • left joystick: X Axis and Y Axis
    • right joystick: Z Axis and Z Rotation

After you create controller, is good to check if any controller was found.


// Check if the controller was found.
if( !joystick.isControllerConnected() ){
   System.out.println("No controller found!");
   // Do some stuff.
}

Now that you have created joystick you need to pull that joystick for current data. You need to do this always before using joystick components data. Put this on start of your loop.


// Get current state of joystick! And check, if joystick is disconnected.
if( !joystick.pollController() ) {
   System.out.println("Controller disconnected!");
   // Do some stuff.
}

Using joysticks:


// Left controller joystick
int xValuePercentageLeftJoystick = joystick.getXAxisPercentage();
int yValuePercentageLeftJoystick = joystick.getYAxisPercentage();

// Right controller joystick
int xValuePercentageRightJoystick, yValuePercentageRightJoystick;

// stick type controller
if(joystick.getControllerType() == Controller.Type.STICK)
{
   // Right controller joystick
   xValuePercentageRightJoystick = joystick.getZAxisPercentage();
   yValuePercentageRightJoystick = joystick.getZRotationPercentage();
}
// gamepad type controller
else
{
   // Right controller joystick
   xValuePercentageRightJoystick = joystick.getXRotationPercentage();
   yValuePercentageRightJoystick = joystick.getYRotationPercentage();

   // If Z Axis exists.
   if(joystick.componentExists(Component.Identifier.Axis.Z)){
      int zAxisValuePercentage = joystick.getZAxisPercentage();
   }
}

As mentioned before axis names of the left controller joystick are the same for stick and gamepad type, but names of axis for the right controller joystick are different, so we need to put an if else statement and get right joystick data with diferent methods.
There’s another, better/easier/cleaner way to do this. There are some methods that you can use to get data for right joystick regardless of controller type. Here’s the code with the same data as before but with different methods:


// Left controller joystick
int xValuePercentageLeftJoystick = joystick.getX_LeftJoystick_Percentage();
int yValuePercentageLeftJoystick = joystick.getY_LeftJoystick_Percentage();

// Right controller joystick
int xValuePercentageRightJoystick = joystick.getX_RightJoystick_Percentage();
int yValuePercentageRightJoystick = joystick.getY_RightJoystick_Percentage();

// If controller is a gamepad type.
if(joystick.getControllerType() == Controller.Type.GAMEPAD)
{ // Must check if controller is a gamepad, because stick type controller also have Z axis but it's for right controller joystick.
   // If Z Axis exists.
   if(joystick.componentExists(Component.Identifier.Axis.Z)){
      int zAxisValuePercentage = joystick.getZAxisPercentage();
   }
}

I this examples I used methods that returns joystick values in percentages, int numbers from 0 to 100. You can also use methods that returns float numbers from -1.0 to 1.0.
I should also tell that when I tested Xbox MadCatz gamepad controller, I got -1.52… value for Z axis in idle mode (when I didn’t move “joystick” that you use with your pointer fingers).

Using Hat switch:


float hatSwitchPosition = joystick.getHatSwitchPosition();

if(Float.compare(hatSwitchDirection, Component.POV.OFF) == 0){
  // Hat switch is not pressed. The same as Component.POV.CENTER
}else if(Float.compare(hatSwitchPosition, Component.POV.UP) == 0){
   // Do stuff when UP is pressed.
}else if(Float.compare(hatSwitchPosition, Component.POV.DOWN) == 0){
   // Do stuff when DOWN is pressed.
}else if(Float.compare(hatSwitchPosition, Component.POV.LEFT) == 0){
   // Do stuff when LEFT is pressed.
}else if(Float.compare(hatSwitchPosition, Component.POV.RIGHT) == 0){
   // Do stuff when RIGHT is pressed.
}else if(Float.compare(hatSwitchPosition, Component.POV.UP_LEFT) == 0){
   // Do stuff when UP and LEFT is pressed.
}else if(Float.compare(hatSwitchPosition, Component.POV.UP_RIGHT) == 0){
   // Do stuff when UP and RIGHT is pressed.
}else if(Float.compare(hatSwitchPosition, Component.POV.DOWN_LEFT) == 0){
   // Do stuff when DOWN and LEFT is pressed.
}else if(Float.compare(hatSwitchPosition, Component.POV.DOWN_RIGHT) == 0){
   // Do stuff when DOWN and RIGHT is pressed.
}

Using joystick buttons:


// Number of buttons.
int numberOfButtons = joystick.getNumberOfButtons();

// Button one on the controller.
boolean joystickButton_1 = joystick.getButtonValue(0);

If variable “joystickButton_1″ is true then button one is pressed.

Closure

There are also some other methods that you can use, so look at javadoc of this JInputJoystick class and also look at JInput javadoc so that you will know what else can you use.

In JInputJoystickTest_source_code.zip package you can find class called JInputJoystickTest.java that you can use to see all controllers connected to your computer and its components. This class content was written by java-gaming.org forum user Endolf, you can find his posts about JInput on http://www.java-gaming.org/index.php/topic,16866.0, his post were my basis to work with JInput, so thanks you Endolf.

Downloads

To download, click on a link and then click File >> Download.

JInputJoystick.java – Additional class for easier and cleaner use of JInput.
JInputJoystick_Javadoc.html – Java doc for JInputJoystick.java class.

JInputJoystickTest.zip – Program for testing joystick.
JInputJoystickTest_source_code.zip – Program source code.

Folder containing all the above files.

About these ads
Comments
  1. carlos says:

    I have a mat pad , but doesnt work :( do you know how work _?

  2. TheUzo007 says:

    Try to use JInputJoystickTest.java class to see all controllers and its components, try to implement components one by one.

  3. carlos says:

    How can I create an event with joystick? here’s the code where I want to create the event. Any chance you could help me? http://pastebin.com/embed.php?i=8QpkLfV5

  4. TheUzo007 says:

    I think that you can’t set any listeners for events except for the controllerRemoved and controllerAdded.
    You need to check for states of components (buttons, joysticks) that you are interested into in a while loop.
    Look at JoystickTest.java into method stickOrGamepadTypeJoystick_Test_Better, in this method is while loop. In while loop first poll controller for current state/data and then use/check controller components states and make actions according to the state of buttons, joysticks.

  5. carlos says:

    thank´s !!!

  6. rsdnt says:

    excellent library! really help me out to control a rc car over wifi with my xbox360 controller! thank you so much

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s