Hey guys,
Im working on a robotics project and am attempting to have support for controllers via Jinput, however, I am having difficulty getting the polling to work. I am using an Xbox One controller which I have confirmed works the following ways: Played steam games with it, works as expected for all tests provided by jinput, shows up in when I run my code as a connected device. However, when it comes to getting poll data, the values never seem to change. From what I can tell with the debugger, the components all display the “has_pulled” value to be false. I have followed several tutorials (including the one on this forum) and cannot seem to get it working. My values all stay at the same default value of 0.0 or -something for thumbsticks.
Here is my code:
public class ControllerTest {
public static void main(String[] args){
//System.out.println("Hello World");
Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
Controller gamepad = null;
Component[] components = null;
EventQueue eventQueue;
// Run through the list of available input devices and get the gamepad
for(int i = 0; i < ca.length; i ++){
if(ca[i].getType().equals(Controller.Type.GAMEPAD)){ // Snag the first instance of a gamepad
gamepad = ca[i];
}
}
// Print the name of the controller and its type
if(gamepad != null){
System.out.println(gamepad.getName() + ": " + gamepad.getType());
components = gamepad.getComponents();
System.out.println("COMPONENTS:");
for(int i = 0; i < components.length; i ++){
StringBuffer buffer = new StringBuffer();
buffer.append("Component #" + i + ": "); // Get the Component #
buffer.append("\t" + components[i].getName()); // Get the component name
// Check if the component is analog or relative
// Relative relies on its past position for values
// Analog is a current position
// Analog absolute is a toggle (think your a,b,y,x button)
if(components[i].isRelative()){
buffer.append(" [Relative]");
}
else if(components[i].isAnalog()){
buffer.append(" [Analog]");
}
else{
buffer.append(" [Analog Absolute]");
}
System.out.println(buffer.toString());
}
}
else{
System.out.println("No gamepad connected");
}
float prevPollData = 100f;
float curPollData;
String compName;
while(true){
gamepad.poll();
for(int i = 0; i < components.length; i ++){
curPollData = components[i].getPollData();
compName = components[i].getName();
System.out.println(compName + ": " + curPollData);
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
I am quite baffled as to why this works every way but when I use my code, and by all accounts, I believe I am following the correct method for polling and retrieving information. Any help would be greatly appreciated.