Jinput init's controllers with up and left as true

787 Views Asked by At

I tried using it on this simple code, and JInput makes the controller automatically go up and left! IT seeems that input.isControllerUp(Input.ANY_CONTROLLER) starts out as true! How can I fix this?

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;


public class Test1 extends BasicGame{
    float x = 300;
    float y = 300;

    public Test1()
    {
        super("Controller Test 1");
    }

    @Override
    public void init(GameContainer gc) 
            throws SlickException {
    }

    public void updateControler(GameContainer gc){
        Input input = gc.getInput();


            System.out.println("input.isControllerUp(Input.ANY_CONTROLLER)" + input.isControllerUp(Input.ANY_CONTROLLER));

            if(input.isKeyDown(Input.KEY_UP) || input.isControllerUp(Input.ANY_CONTROLLER)) {
                y--;
            }

            else if(input.isKeyDown(Input.KEY_DOWN) || input.isControllerDown(Input.ANY_CONTROLLER)) {
                y++;
            }

            else if(input.isKeyDown(Input.KEY_LEFT) || input.isControllerLeft(Input.ANY_CONTROLLER)) {
                x--;
            }

            else if(input.isKeyDown(Input.KEY_RIGHT) || input.isControllerRight(Input.ANY_CONTROLLER)) {
                x++;
            }
    }

    @Override
    public void update(GameContainer gc, int delta) 
            throws SlickException     
    {;
        updateControler(gc);
    }


    public void render(GameContainer gc, Graphics g) 
            throws SlickException 
    {   
        g.setColor(Color.red);
        g.fillRect(x,y,50,50);
    }

    public static void main(String[] args) 
            throws SlickException
    {
         AppGameContainer app = 
            new AppGameContainer(new Test1());

         app.setDisplayMode(800, 600, false);
         app.start();
    }
}
2

There are 2 best solutions below

0
On

After researching this a bit since I was having the same problem, I found the following link explaining what the issue is: http://www.java-gaming.org/topics/jinput-is-awsome-i-want-it-s-babies-but-a-slight-issue/21594/msg/176751/view.html

Basically, some controllers just start with some of the axes at something besides zero. The workaround mentioned is to set a flag when the game starts and check the values of the axes. Until any of the axes have been moved or buttons have been pressed, ignore all controller input. After any axes or button states change from their original values, clear the flag and start accepting input.

0
On

Input is not a class of JInput, it comes from the slick library. I am using JInput directly and I have no similar problems.