Main Menu

Gamepad?

Started by ToddMcF2002, May 05, 2007, 10:07:52 PM

Previous topic - Next topic

ToddMcF2002

Anyone try to map a USB gamepad in OpenGL mode?  I have not tried it but I've been animating a character for my son to play with and he'd be better off on the Saitek than the keyboard.

I was thinking about making some kids games but without gamepad support it will be sorta tough.  Ideas?




ToddMcF2002

OK, I knew about the lwjgl Mouse support and should have checked for USB controller.  I guess that's where my homework lies...


Remo

I've worked with jInput and it works. Here's the source code from my asteroids project. Hope it helps you getting started!

Here's jinput homepage also: https://jinput.dev.java.net/


package asteroids;
import net.java.games.input.*;
/**
*
* @author Remo
*/
public class JoyManager {
    private static final int NUM_BUTOTONS = 6;
    private Controller controller;
    private Component[] comps;
    private int xAxisIdx, yAxisIdx, zAxisIdx, rzAxisIdx; // for the x- and y- axes
    private int buttonsIdx[];
   
    /** Creates a new instance of JoyManager */
    public JoyManager() {
        xAxisIdx = -1;
        yAxisIdx = -1;
        zAxisIdx = -1;
        rzAxisIdx = -1;
        buttonsIdx = new int[NUM_BUTOTONS];
        for (int i = 0; i < buttonsIdx.length; i++) {
            buttonsIdx[i] = -1;
        }
        Controller[] cs = getContollers();
        controller = findJoystick(cs);
        System.out.println("Using "+ controller.getName() + ":" + controller.getType());
       
        comps = controller.getComponents();
        getComponentsIndicies(comps);
    }
   
    private Controller findJoystick(Controller[] cs) {
        Controller.Type type;
        int index = 0;
        while(index < cs.length) {
            type = cs[index].getType();
            if (type == Controller.Type.STICK) break;
            index++;
        }
        if (index == cs.length) {
            System.out.println("No Joystick found!!! Closing Asteroids.");
            System.exit(0);
        }
        return cs[index];
    }

    private Controller[] getContollers() {
        ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
        Controller[] controllers = ce.getControllers();
        return controllers;
    }

    private void getComponentsIndicies(Component[] comps) {
        if (comps.length <= 0) {
            System.out.println("No components found for Joystick");
            System.out.println("Aborting asteroids.");
            System.exit(0);
        }
        int numButtons = 0;
        Component c;
        for (int i = 0; i < comps.length; i++) {
            c= comps[i];
            if (c.getIdentifier() == Component.Identifier.Axis.X) {
                xAxisIdx = i;
                System.out.println("Found X Axis. Component number: " + i);
            }
            if (c.getIdentifier() == Component.Identifier.Axis.Y) {
                yAxisIdx = i;
                System.out.println("Found Y Axis. Component number: " + i);
            }
            if (c.getIdentifier() == Component.Identifier.Axis.Z) {
                zAxisIdx = i;
                System.out.println("Found Z Axis. Component number: " + i);
            }
            if (c.getIdentifier() == Component.Identifier.Axis.RZ) {
                rzAxisIdx = i;
                System.out.println("Found RZ Axis. Component number: " + i);
            }
            if (isButton(c)) {
                if (numButtons == NUM_BUTOTONS) {
                    System.out.println("Found too many buttons. Ignoring the rest.");
                }
                else {
                    buttonsIdx[numButtons] = i;
                    numButtons++;
                    System.out.println("Found Button. Component number: " + i);
                }
            }
            else {
                System.out.println("Unknown component. Ignoring.");
            }
        }
        if (xAxisIdx==-1 || yAxisIdx == -1 || buttonsIdx[0] ==-1) {
            System.out.println("Could not find enough components to launch Asteroids. Aborting");
            System.exit(0);
        }
        if (zAxisIdx == -1) System.out.println("Could not find Z Axis.");
        if (rzAxisIdx == -1) System.out.println("Could not find RZ Axis.");
        if (numButtons < NUM_BUTOTONS) System.out.println("Not all buttons were found.");
    }
   
    private boolean isButton (Component c) {
        if (!c.isAnalog() && !c.isRelative() && c.isNormalized()){
            String className = c.getIdentifier().getClass().getName();
            if (className.endsWith("Button")) return true;
        }
        return false;
    }
   
    public void poll() {
        controller.poll();
    }
    public float getXAxis() {
        return comps[xAxisIdx].getPollData();
    }
    public float getYAxis() {
        return comps[yAxisIdx].getPollData();
    }
    public float getZAxis() {
        if (zAxisIdx == -1) return 0f;
        return comps[zAxisIdx].getPollData();
    }
    public float getRZAxis() {
        if (rzAxisIdx == -1) return 0f;
        return comps[rzAxisIdx].getPollData();
    }
    public boolean fireButtonPressed() {
        return ((comps[buttonsIdx[0]].getPollData() == 0.0f) ? false : true);
    }
   
}


And here's how I used it in the game:


package asteroids;
import com.threed.jpct.*;

/**
*
* @author Remo
*/
public class CameraManager {
    JoyManager joystick;
    public float speedx = 0f;
    public float speedy = 0f;
    public float sens;
    public SimpleVector direction;
    private Camera camera;
    private Camera skycam;
   
    /** Creates a new instance of CameraManager */
    public CameraManager(Camera camera, Camera sky) {
        joystick = new JoyManager();
        this.camera = camera;
        skycam = sky;
        sens = .001f;
    }
   
    public void moveSpaceship() {
        //skycam.setPosition(new SimpleVector(0f,0f,0f));
        speedx+= joystick.getYAxis()*-sens;
        speedy+= joystick.getXAxis()*sens;
        if (speedx > .1f) speedx = .1f;
        if (speedy > .1f) speedy = .1f;
        camera.rotateCameraX(speedx);
        skycam.rotateCameraX(speedx);
        camera.rotateCameraY(speedy);
        skycam.rotateCameraY(speedy);
        SimpleVector oldCamDir = camera.getDirection();
        camera.moveCamera(oldCamDir, joystick.getZAxis() *-10);
    }
   
    public void pollJoystick(){
        joystick.poll();
    }
   
    public boolean fireButtonPressed(){
        return joystick.fireButtonPressed();
    }
   
    public void incSens() {
        sens += .001f;
        System.out.println("Sensibility multiplier:" +sens);
    }
    public void decSens() {
        sens -= .001f;
        if(sens < 0) sens = 0f;
        System.out.println("Sensibility multiplier:" +sens);
    }
}



ToddMcF2002

Thanks guys that helps alot!

ToddMcF2002

So I finished implementing the Gamepad last night and I ended up using the LWJGL wrapper.  I noticed that I have to put JInput on my classpath so I suppose I could have used the attached code from Asteroids.  I like the getComponentsIndecies() call especially.  Maybe I'll switch to JInput directly later.  Thanks for the code.

I must say I was pleasantly suprised about how easy this was to implement - compared to say OpenGL custom cursors.  I still get the shakes just thinking about trying to replace cursors.  :o