All libGDX input statements are returning TRUE at once

Posted by MowDownJoe on Game Development See other posts from Game Development or by MowDownJoe
Published on 2013-10-16T20:21:27Z Indexed on 2013/10/17 16:27 UTC
Read the original article Hit count: 442

Filed under:
|
|

I'm fooling around with Box2D and libGDX and running into a peculiar problem with polling for input. Here's the code for the Screen's render() loop:

@Override
public void render(float delta) {
    Gdx.gl20.glClearColor(0, 0, .2f, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
    camera.update();
    game.batch.setProjectionMatrix(camera.combined);
    debugRenderer.render(world, camera.combined);
    if(Gdx.input.isButtonPressed(Keys.LEFT)){
        Gdx.app.log("Input", "Left is being pressed.");
        pushyThingyBody.applyForceToCenter(-10f, 0);
    }
    if(Gdx.input.isButtonPressed(Keys.RIGHT)){
        Gdx.app.log("Input", "Right is being pressed.");
        pushyThingyBody.applyForceToCenter(10f, 0);
    }
    world.step((1f/45f), 6, 2);
}

And the constructor is largely just setting up the World, Box2DDebugRenderer, and all the Bodies in the world:

public SandBox(PhysicsSandboxGame game) {
    this.game = game;
    camera = new OrthographicCamera(800, 480);
    camera.setToOrtho(false);

    world = new World(new Vector2(0, -9.8f), true);
    debugRenderer = new Box2DDebugRenderer();

    BodyDef bodyDef = new BodyDef();
    bodyDef.type = BodyType.DynamicBody;
    bodyDef.position.set(100, 300);

    body = world.createBody(bodyDef);

    CircleShape circle = new CircleShape();
    circle.setRadius(6f);

    FixtureDef fixtureDef = new FixtureDef();
    fixtureDef.shape = circle;
    fixtureDef.density = .5f;
    fixtureDef.friction = .4f;
    fixtureDef.restitution = .6f;

    fixture = body.createFixture(fixtureDef);
    circle.dispose();

    BodyDef groundBodyDef = new BodyDef();
    groundBodyDef.position.set(new Vector2(0, 10));
    groundBody = world.createBody(groundBodyDef);
    PolygonShape groundBox = new PolygonShape();
    groundBox.setAsBox(camera.viewportWidth, 10f);
    groundBody.createFixture(groundBox, 0f);
    groundBox.dispose();

    BodyDef pushyThingyBodyDef = new BodyDef();
    pushyThingyBodyDef.type = BodyType.DynamicBody;
    pushyThingyBodyDef.position.set(new Vector2(400, 30));
    pushyThingyBody = world.createBody(pushyThingyBodyDef);
    PolygonShape pushyThingyShape = new PolygonShape();
    pushyThingyShape.setAsBox(40f, 10f);
    FixtureDef pushyThingyFixtureDef = new FixtureDef();
    pushyThingyFixtureDef.shape = pushyThingyShape;
    pushyThingyFixtureDef.density = .4f;
    pushyThingyFixtureDef.friction = .1f;
    pushyThingyFixtureDef.restitution = .5f;
    pushyFixture = pushyThingyBody.createFixture(pushyThingyFixtureDef);
    pushyThingyShape.dispose();
}

Testing this on the desktop. Basically, whenever I hit the appropriate keys, neither of the if statements in the loop return true. However, when I click in the window, both statements return true, resulting in a 0 net force on the body. Why is this?

© Game Development or respective owner

Related posts about java

Related posts about libgdx