StrikerX3/JXInput

some help for correctly using JXInput

dutpas opened this issue · 11 comments

Hi,

I made a video game for PC with Java language under Eclipse. This game works fine with keyboard.
Now, I want to connect a XBOX360 to my game for improving the game quality. To do that, I want to use Xinput and your solution seems to be very pratical.
I follow your instructions but I think that I have not understood them very well. And I am stuck for 2 days on this problem. I do that:

  • I downloaded the packages from https://github.com/StrikerX3/JXInput
  • I put, in the right place, only two files: the jxinput jar and the jxinput.dll (only). No native download ... OK?
  • I wrote small codes for using the JXinput API in my software (and solving the JXinput import problems). When I run it, on executing "XInputDevice[] devices = XInputDevice.getAllDevices();", the debugger raises this error "com.ivan.xinput.exceptions.XInputNotLoadedException: Native library failed to load
    at com.ivan.xinput.XInputDevice.checkLibraryReady(XInputDevice.java:298)"

Your readme file says that it is necessary to download also " Visual C++ Redistributable Packages for Visual Studio 2015". I do that. What is the interest to use it if I write on Java codes?
And, as I have not Maven, I have to modify the pom.xml file. Which one among about the 10 pom.xlml under Eclipse?

Why it is necessary to get Visual C++, maven, .. for allowing to connect a PC with XBOX 360 through XInput?

Thank you very much for your help.
Note: Sorry about my poor english knowledge

Hello,

You need to use Maven to build JXInput. The generated .jar file will contain the DLLs in the proper location so that the library can extract and load them at runtime. Do not try to export JXInput as a .jar using Eclipse's Export to JAR option as the resulting file will not have the correct structure and will be unusable with your projects.

If you haven't disabled or uninstalled m2eclipse (an Eclipse plugin that adds Maven support), you should be able to do this:

  1. If you already have JXInput loaded into Eclipse, delete it (but not from the hard disk!). Let's start from scratch
  2. Import the XInputDevice_Java folder as a Maven Project:
    1. Go to the File menu and select Import
    2. Look for the Maven folder. Within it, select Existing Maven Projects, then click Next
    3. Select the XInputDevice_Java folder (within the JXInput folder in your hard drive) for the Root Directory and click Finish
  3. When the project is done importing, right-click it and select Run As > Maven Build
  4. In the window that pops up, type install in the Goals field
  5. Click Run. The console output should display the build process. If everything goes right, you should get BUILD SUCCESS
  6. Right-click your game project and select Build Path > Configure Build Path
  7. Remove any jxinput.jar or dll files you may have added there
  8. On the right panel, go to the Projects tab and click Add...
  9. Select the JXInput project and click OK on both windows
  10. XInputDevice.isAvailable() should now return true

If you need to access the actual .jar, refresh the JXInput project after building it. There should be a target folder containing a bunch of folders and the resulting .jar file you'll want to bundle with your game. Note that you do not need to bundle the DLLs, as they are extracted automatically when you run the game.

If you say you have 10 pom.xml files, that means you probably have more than one Maven project loaded in Eclipse. You can distinguish between files by looking at their path -- they're listed as "pom.xml - <path in workspace>" in the Open Resource window (Ctrl+Shift+R), or you can just manually navigate to the projects. pom.xml files reside in the root folders of projects.

JXInput needs Visual C++ Redistributable Packages for Visual Studio 2015 installed because that's what was used to build the DLLs. Without it you may get an error saying that msvcr100.dll is missing, or something to that effect. JXInput makes use of the Java Native Interface to use XInput functionality needed to communicate with the Xbox controllers. The native layer was written in C++ using Visual Studio 2015, hence the requirement. Even though you're only writing Java code, this small portion of the code you cannot see does use C++ and the requirement applies to your project too.

You don't need Visual C++ or Visual Studio to use JXInput, unless you plan on making changes to the library itself. Your games can just use the .jar file without worrying about C++ or anything like that. Keep in mind that the game will still require the Visual C++ Redistributable Packages above (and so will your players).

Hope this helps!

Thank you very much for your information. I'll try to do that, as soon as possible

Pascal

----- Mail original -----
De: "Ivan Roberto de Oliveira" notifications@github.com
À: "StrikerX3/JXInput" JXInput@noreply.github.com
Cc: "dutpas" dutpas@free.fr, "Author" author@noreply.github.com
Envoyé: Lundi 13 Juin 2016 05:08:23
Objet: Re: [StrikerX3/JXInput] some help for correctly using JXInput (#11)

Hello,

You need to use Maven to build JXInput. The generated .jar file will contain the DLLs in the proper location so that the library can extract and load them at runtime. Do not try to export JXInput as a .jar using Eclipse's Export to JAR option as the resulting file will not have the correct structure and will be unusable with your projects.

If you haven't disabled or uninstalled m2eclipse (an Eclipse plugin that adds Maven support), you should be able to do this:

1. If you already have JXInput loaded into Eclipse, delete it (but not from the hard disk!). Let's start from scratch 
2. Import the XInputDevice_Java folder as a Maven Project: 
    1. Go to the File menu and select Import 
    2. Look for the Maven folder. Within it, select Existing Maven Projects, then click Next 
    3. Select the XInputDevice_Java folder (within the JXInput folder in your hard drive) for the Root Directory and click Finish 
3. When the project is done importing, right-click it and select Run As > Maven Build 
4. In the window that pops up, type install in the Goals field 
5. Click Run. The console output should display the build process. If everything goes right, you should get BUILD SUCCESS 
6. Right-click your game project and select Build Path > Configure Build Path 
7. Remove any jxinput.jar or dll files you may have added there 
8. On the right panel, go to the Projects tab and click Add... 
9. Select the JXInput project and click OK on both windows 
10. XInputDevice.isAvailable() should now return true 

If you need to access the actual .jar, refresh the JXInput project after building it. There should be a target folder containing a bunch of folders and the resulting .jar file you'll want to bundle with your game. Note that you do not need to bundle the DLLs, as they are extracted automatically when you run the game.

If you say you have 10 pom.xml files, that means you probably have more than one Maven project loaded in Eclipse. You can distinguish between files by looking at their path -- they're listed as "pom.xml - " in the Open Resource window (Ctrl+Shift+R), or you can just manually navigate to the projects. pom.xml files reside in the root folders of projects.

JXInput needs Visual C++ Redistributable Packages for Visual Studio 2015 installed because that's what was used to build the DLLs. Without it you may get an error saying that msvcr100.dll is missing, or something to that effect. JXInput makes use of the Java Native Interface to use XInput functionality needed to communicate with the Xbox controllers. The native layer was written in C++ using Visual Studio 2015, hence the requirement. Even though you're only writing Java code, this small portion of the code you cannot see does use C++ and the requirement applies to your project too.

You don't need Visual C++ or Visual Studio to use JXInput, unless you plan on making changes to the library itself. Your games can just use the .jar file without worrying about C++ or anything like that. Keep in mind that the game will still require the Visual C++ Redistributable Packages above (and so will your players).

Hope this helps!


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub , or mute the thread .

Hi Ivan,

that's work fine. I got the console output:

"XInput 1.3 is available on this platform.
XInput 1.4 is available on this platform."

when I run:
XInputDevice[] devices = XInputDevice.getAllDevices();

        XInputDevice device = XInputDevice.getDeviceFor(0); // or devices[0]

        if (XInputDevice.isAvailable()) {
            System.out.println("XInput 1.3 is available on this platform.");
        }

        if (XInputDevice14.isAvailable()) {
            System.out.println("XInput 1.4 is available on this platform.");
        }

I can begin to connect a XBOX 360 joystick to my game.


Now, it is my turn to understand how to use these following notions (that work fine at the first test)
XInputComponents components = device.getComponents();
XInputButtons buttons= components.getButtons();
boolean bRight = buttons.right; // etc ....
XInputAxes axes = components.getAxes();
double xPos = axes.lx; // ou rawx ....
double yPos = axes.ly;
int dp = axes.dpad;

Do you know a good site explaining well these notions?
Because, I always get the following STEADY values:
dp: -1
xPos: -0.30999755859375
yPos: 0.638916015625

Regards and, still, thank you very much for your help
Pascal

----- Mail original -----
De: "Ivan Roberto de Oliveira" notifications@github.com
À: "StrikerX3/JXInput" JXInput@noreply.github.com
Cc: "dutpas" dutpas@free.fr, "Author" author@noreply.github.com
Envoyé: Lundi 13 Juin 2016 05:08:23
Objet: Re: [StrikerX3/JXInput] some help for correctly using JXInput (#11)

Hello,

You need to use Maven to build JXInput. The generated .jar file will contain the DLLs in the proper location so that the library can extract and load them at runtime. Do not try to export JXInput as a .jar using Eclipse's Export to JAR option as the resulting file will not have the correct structure and will be unusable with your projects.

If you haven't disabled or uninstalled m2eclipse (an Eclipse plugin that adds Maven support), you should be able to do this:

1. If you already have JXInput loaded into Eclipse, delete it (but not from the hard disk!). Let's start from scratch 
2. Import the XInputDevice_Java folder as a Maven Project: 
    1. Go to the File menu and select Import 
    2. Look for the Maven folder. Within it, select Existing Maven Projects, then click Next 
    3. Select the XInputDevice_Java folder (within the JXInput folder in your hard drive) for the Root Directory and click Finish 
3. When the project is done importing, right-click it and select Run As > Maven Build 
4. In the window that pops up, type install in the Goals field 
5. Click Run. The console output should display the build process. If everything goes right, you should get BUILD SUCCESS 
6. Right-click your game project and select Build Path > Configure Build Path 
7. Remove any jxinput.jar or dll files you may have added there 
8. On the right panel, go to the Projects tab and click Add... 
9. Select the JXInput project and click OK on both windows 
10. XInputDevice.isAvailable() should now return true 

If you need to access the actual .jar, refresh the JXInput project after building it. There should be a target folder containing a bunch of folders and the resulting .jar file you'll want to bundle with your game. Note that you do not need to bundle the DLLs, as they are extracted automatically when you run the game.

If you say you have 10 pom.xml files, that means you probably have more than one Maven project loaded in Eclipse. You can distinguish between files by looking at their path -- they're listed as "pom.xml - " in the Open Resource window (Ctrl+Shift+R), or you can just manually navigate to the projects. pom.xml files reside in the root folders of projects.

JXInput needs Visual C++ Redistributable Packages for Visual Studio 2015 installed because that's what was used to build the DLLs. Without it you may get an error saying that msvcr100.dll is missing, or something to that effect. JXInput makes use of the Java Native Interface to use XInput functionality needed to communicate with the Xbox controllers. The native layer was written in C++ using Visual Studio 2015, hence the requirement. Even though you're only writing Java code, this small portion of the code you cannot see does use C++ and the requirement applies to your project too.

You don't need Visual C++ or Visual Studio to use JXInput, unless you plan on making changes to the library itself. Your games can just use the .jar file without worrying about C++ or anything like that. Keep in mind that the game will still require the Visual C++ Redistributable Packages above (and so will your players).

Hope this helps!


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub , or mute the thread .

You need to do device.poll() once every frame to read the controller input data, then you can use the values as you mentioned above. Polling must be performed per controller, so what you want to do is something like this in your game loop:

for (XInputDevice device : XInputDevice.getAllDevices() {
    if (device.poll()) {
        // controller is connected, implement your input handling logic here, e.g.:
        XInputComponents components = device.getComponents();
        XInputButtons buttons = components.getButtons();
        boolean bRight = buttons.right; // etc ....
        XInputAxes axes = components.getAxes();
        double xPos = axes.lx; // ou rawx ....
        double yPos = axes.ly;
        int dp = axes.dpad;

        // use device.getPlayerNum() if you need to know which player this device is associated with
    } else {
        // controller is not connected
        // in this situation games typically ask the player to reconnect the controller and pause if possible
    }
}

Components are just a logical grouping of all the input elements of an XInput controller. These include buttons (A, B, X, Y, start, back and others) and axes (sticks, triggers and the directional pad). If you need more information you can head over to the XInput documentation. Although the code examples are C++, the fundamentals are valid for JXInput.

Thank's a lot.

I can go ahead, now

Pascal

----- Mail original -----
De: "Ivan Roberto de Oliveira" notifications@github.com
À: "StrikerX3/JXInput" JXInput@noreply.github.com
Cc: "dutpas" dutpas@free.fr, "Author" author@noreply.github.com
Envoyé: Lundi 13 Juin 2016 18:42:02
Objet: Re: [StrikerX3/JXInput] some help for correctly using JXInput (#11)

You need to do device.poll() once every frame to read the controller input data, then you can use the values as you mentioned above. Polling must be performed per controller, so what you want to do is something like this in your game loop:
for ( XInputDevice device : XInputDevice . getAllDevices() { if (device . poll()) { // controller is connected, implement your input handling logic here, e.g.: XInputComponents components = device . getComponents(); XInputButtons buttons = components . getButtons(); boolean bRight = buttons . right; // etc .... XInputAxes axes = components . getAxes(); double xPos = axes . lx; // ou rawx .... double yPos = axes . ly; int dp = axes . dpad; // use device.getPlayerNum() if you need to know which player this device is associated with } else { // controller is not connected // in this situation games typically ask the player to reconnect the controller and pause if possible }
}

Components are just a logical grouping of all the input elements of an XInput controller. These include buttons (A, B, X, Y, start, back and others) and axes (sticks, triggers and the directional pad). If you need more information you can head over to the XInput documentation . Although the code examples are C++, the fundamentals are valid for JXInput.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub , or mute the thread .

Great! I'm glad you could get it to work.

XInputDevice14 should be used only if you need the XInput 1.4 functionality, such as reading battery state or keyboard input. It also requires Windows 8 or higher.

Thanks for pointing out the issue with the dpadFromButtons method. I'll push a fix in a moment.

Hello,

I am very glad to use your package on my XBOX360. At this time, my development concerns a video game on PC. And, I intend to commercialize it in few months, if it is good enough. In this case, I'll contact you again for remunerating your help. About your driver, if you want to know how to correctly manage a stick for an object rotation (in other words, with accurate motion), I'll give you back it: after some attempts, I have found a good algorithm

But, my major aim is to commercialize this game on smartphones. To do that, I am adapting my PC game and, now, I am looking for a Java package that manages the phone accelerometer (like in Asphalt Nitro game). As you produce some drivers like JXinput, perhaps have you also produced a accelerometer as driver? Otherwise, do you know where I could find it?

Regards
Pascal

----- Mail original -----
De: "Ivan Roberto de Oliveira" notifications@github.com
À: "StrikerX3/JXInput" JXInput@noreply.github.com
Cc: "dutpas" dutpas@free.fr, "Author" author@noreply.github.com
Envoyé: Mercredi 22 Juin 2016 14:07:00
Objet: Re: [StrikerX3/JXInput] some help for correctly using JXInput (#11)

XInputDevice14 should be used only if you need the XInput 1.4 functionality, such as reading battery state or keyboard input. It also requires Windows 8 or higher.

Thanks for pointing out the issue with the dpadFromButtons method. I'll push a fix in a moment.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub , or mute the thread .

Hello again Pascal, glad to hear back from you. I wish you success on your commercial release and please do send me a link to the game when it's released!
We can further discuss private matters through email. I'll send you an email from my personal account in a moment.
I'm curious about your rotation algorithm; you can create a gist or reply to my email if you wish to keep it private.

As for reading the acceleromoter on smartphones, I can guide you towards the standard APIs used on those platforms:

I believe most if not all game frameworks and engines that target smartphones will have some form of API for reading sensors, and they should not deviate too much from what you see above.

Hello Ivan,

Glad to read your information.

About the rotation algorithm, I'll post it in a gist, in few days

Regards
Pascal

----- Mail d'origine -----
De: Ivan Roberto de Oliveira notifications@github.com
À: StrikerX3/JXInput JXInput@noreply.github.com
Cc: dutpas dutpas@free.fr, Author author@noreply.github.com
Envoyé: Sat, 13 Aug 2016 01:19:29 +0200 (CEST)
Objet: Re: [StrikerX3/JXInput] some help for correctly using JXInput (#11)

Hello again Pascal, glad to hear back from you. I wish you success on your commercial release and please do send me a link to the game when it's released!
We can further discuss private matters through email. I'll send you an email from my personal account in a moment.
I'm curious about your rotation algorithm; you can create a gist or reply to my email if you wish to keep it private.

As for reading the acceleromoter on smartphones, I can guide you towards the standard APIs used on those platforms:

I believe most if not all game frameworks and engines that target smartphones will have some form of API for reading sensors, and they should not deviate too much from what you see above.

You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub:
#11 (comment)