Default controller values?

In my JME3 project, I have a car that can be driven by an Xbox 360 controller. The Z Axis (left and right triggers) accelerate and brake the car. A -1 value is max acceleration, a 1 is max brake, and 0 is no acceleration or braking.



When the game launches, the car starts accelerating immediately, and I have not yet touched the controller. When I just tap the trigger, the car stops accelerating and behaves as normal.



To see if anything in my code caused this, I outputted the Z Axis value right after I initialized the controller. Here is my code:



[java]

if (settings.useJoysticks()){

for(int x = 0; x < Controllers.getControllerCount(); x++){

if (Controllers.getController(x).getName().equals(“XBOX 360 For Windows (Controller)”)){

controller = Controllers.getController(x);

break;

}

}



if (controller != null){

controller.setXAxisDeadZone(.1f);

System.out.println("Initial acceleration: " + controller.getZAxisValue());

}

}

[/java]



The value outputted was -1 (hard acceleration) every time. I tried

[java]Controllers.poll();[/java] and [java]controller.poll();[/java] before I outputted the Z-axis value, but neither had any effect. So either the Z-axis value is initialized to some value (here -1) before any input is detected, or I did not initialize the controller properly (which I doubt, because the controller otherwise works flawlessly).



Can anyone help me determine why this happens?

Maybe the value is -1 when its not pressed and 0 when it is pressed? Maybe the calibration of the controller is incorrect?

Try going into the gamepad control panel and see if you’re getting the same results there.

Momoko_Fan said:
Maybe the value is -1 when its not pressed and 0 when it is pressed? Maybe the calibration of the controller is incorrect?
Try going into the gamepad control panel and see if you're getting the same results there.


Here is the gamepad Control Panel when no buttons or axes are being pressed:



The Z-Axis bar is at 0; holding down the right trigger (acceleration) would make the bar go to the left toward -1, and holding down the left trigger (brake) would make the bar go to the right toward 1.

It seems that the Z-Axis value is "stuck" at -1 until it is pushed, and then it responds properly.

The way you would usually use the Controllers class is by calling poll() and then going through the events. Maybe once that is done you can access the Z Axis value.

Momoko_Fan said:
The way you would usually use the Controllers class is by calling poll() and then going through the events. Maybe once that is done you can access the Z Axis value.


How would this be done? The wiki page on input handling doesn't really elaborate on how controller input should be handled.

And should the polling and event checking go in the update method or the listener methods?

There’s support for joysticks/gamepads now in jME3 SVN, see the TestJoystick class.

You should now use this functionality, instead of implementing your own.

Momoko_Fan said:
(...) You should now use this functionality, instead of implementing your own.
And by all means supplement the wiki as you see fit :)

Thanks on letting me know about the new joystick functionality. I have implemented events for steering and braking/accelerating. I notice that if I am accelerating/braking, I cannot steer. If I stop accelerating/braking, then I can steer again.



The “if” statements in my analog listener allow for both steering and accelerating/braking functionality. This is odd because I thought events allowed more than one event to be fired at a time. Hopefully I am doing something incorrectly and it is not some limitation of joysticks. Here is my code:



Initializing the joystick:

[java]private void initJoystick(){

if (settings.useJoysticks()){

Joystick[] joysticks = inputManager.getJoysticks();



System.out.println("Joystick Detected (Not Necessarily Supported): " + joysticks[0].getName());

}



inputManager.addMapping(“Steer Left”, new JoyAxisTrigger(0, 1, true));

inputManager.addMapping(“Steer Right”, new JoyAxisTrigger(0, 1, false));

inputManager.addMapping(“Accelerate Vehicle”, new JoyAxisTrigger(0, 4, true));

inputManager.addMapping(“Brake Vehicle”, new JoyAxisTrigger(0, 4, false));

inputManager.addListener(analogListener, new String[]{“Steer Left”, “Steer Right”, “Accelerate Vehicle”, “Brake Vehicle”});

}[/java]



AnalogListener for joystick:

[java]private AnalogListener analogListener = new AnalogListener(){

public void onAnalog(String name, float isPressed, float tpf) {

if (name.equals(“Steer Left”))

car.getPlayer().steer(isPressed * .25f);

else{

if (name.equals(“Steer Right”))

car.getPlayer().steer(isPressed * -.25f);

else

car.getPlayer().steer(0);

}



if (name.equals(“Accelerate Vehicle”))

car.getPlayer().accelerate(isPressed * -6000f);

else

if (name.equals(“Brake Vehicle”))

car.getPlayer().accelerate(isPressed * 10000f);

}

};[/java]

The onAnalog() callback happens every time for every mapping. If you steer and then some other callback occurs, the code

[java]car.getPlayer().steer(0);[/java] will be executed (since none of the conditions are met) which will reset the steer value you set earlier. So just get rid of that line and it should work.