javalibgdxouya

Implementing a Gamepad Controller in LIBGDX?


I'm at a loss as to how to implement the LIBGDX controller support. So here are the details.

Let's say I'm following the tutorial here: http://obviam.net/index.php/getting-started-in-android-game-development-with-libgdx-create-a-working-prototype-in-a-day-tutorial-part-1/

The Git for the tutorial at Part 4 (where i'm trying to implement a gamepad) is here: https://github.com/obviam/star-assault/tree/part4

I've made it to the final step and now it's running and I can interact with the character using the keyboard. I want to now make the character controllable via a controller either with the OUYA or through USB.

I have read the following: http://www.badlogicgames.com/wordpress/?p=2724 I have added the jar files to the main project and have Ordered and Exported. I have now modified my GameScreen class to look as follows:

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.controllers.Controller;
import com.badlogic.gdx.controllers.PovDirection;
import com.badlogic.gdx.controllers.mappings.Ouya;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.math.Vector3;


public class GameScreen implements Screen, InputProcessor, ControllerListener {

private World           world;
private WorldRenderer   renderer;
private CharacterController charactercontroller;

private int width, height;

@Override
public void show() {
    world = new World();
    renderer = new WorldRenderer(world, false);
    charactercontroller = new CharacterController(world);
    Gdx.input.setInputProcessor(this);
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    charactercontroller.update(delta);
    renderer.render();
}

@Override
public void resize(int width, int height) {
    renderer.setSize(width, height);
    this.width = width;
    this.height = height;
}

@Override
public void hide() {
    Gdx.input.setInputProcessor(null);
}

@Override
public void pause() {
    // TODO Auto-generated method stub
}

@Override
public void resume() {
    // TODO Auto-generated method stub
}

@Override
public void dispose() {
    Gdx.input.setInputProcessor(null);
}

// * InputProcessor methods ***************************//

@Override
public boolean keyDown(int keycode) {
    if (keycode == Keys.LEFT)
        charactercontroller.leftPressed();
    if (keycode == Keys.RIGHT)
        charactercntroller.rightPressed();
    if (keycode == Keys.UP)
        charactercontroller.jumpPressed();
    return true;
}

@Override
public boolean keyUp(int keycode) {
    if (keycode == Keys.LEFT)
        charactercontroller.leftReleased();
    if (keycode == Keys.RIGHT)
        charactercontroller.rightReleased();
    if (keycode == Keys.UP)
        charactercontroller.jumpReleased();
    return true;
}

@Override
public boolean keyTyped(char character) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean touchDown(int x, int y, int pointer, int button) {
    if (!Gdx.app.getType().equals(ApplicationType.Android))
        return false;
    if (x < width / 2 && y > height / 2) {
        charactercontroller.leftPressed();
    }
    if (x > width / 2 && y > height / 2) {
        charactercontroller.rightPressed();
    }
    return true;
}

@Override
public boolean touchUp(int x, int y, int pointer, int button) {
    if (!Gdx.app.getType().equals(ApplicationType.Android))
        return false;
    if (x < width / 2 && y > height / 2) {
        charactercontroller.leftReleased();
    }
    if (x > width / 2 && y > height / 2) {
        charactercontroller.rightReleased();
    }
    return true;
}

@Override
public boolean touchDragged(int x, int y, int pointer) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean mouseMoved(int screenX, int screenY) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean scrolled(int amount) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public void connected(Controller controller) {
    // TODO Auto-generated method stub

}

@Override
public void disconnected(Controller controller) {
    // TODO Auto-generated method stub

}

@Override
public boolean buttonDown(Controller controller, int buttonCode) {
    if (buttonCode == Ouya.BUTTON_O) {
                charactercontroller.jumpPressed();
            }
    return false;
}

@Override
public boolean buttonUp(Controller controller, int buttonCode) {
    if (buttonCode == Ouya.BUTTON_O) {
                charactercontroller.jumpReleased();
            }
    return false;
}

@Override
public boolean axisMoved(Controller controller, int axisCode, float value) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean povMoved(Controller controller, int povCode,
        PovDirection value) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean xSliderMoved(Controller controller, int sliderCode,
        boolean value) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean ySliderMoved(Controller controller, int sliderCode,
        boolean value) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean accelerometerMoved(Controller controller,
        int accelerometerCode, Vector3 value) {
    // TODO Auto-generated method stub
    return false;
}

}

So based on my understand of how this works, right now when I load this on the OUYA the character should Jump when the "O" button is pressed. But nothing happens. I think I'm missing something. Any glaring mistakes in implementation?

Where I'm at with Java is I've been web developing for a number of years and have just recently returned to Java again to play around with games and interactive stuff on micro consoles. I figure starting off with a simple tutorial and experimenting with building on it would be a good idea, right?

Look forward to getting some awesome help!

Thanks,

Phil


Solution

  • Try adding:

    Controllers.addListener(this);
    

    right before:

    Gdx.input.setInputProcessor(this);
    

    setInputProcessor only registers listeners for the built-in input events (keys, touch events, etc). The controllers are an external library, and it needs to be initialized and loaded separately.

    You will need to import com.badlogic.gdx.controllers.Controllers, too.