2
\$\begingroup\$

I am making a 2D platformer in LibGDX with both Ashley and Box2D.

I have it so, that the PlayerControllerSystem watches for the jump key being pressed:

// Jumping
if (Gdx.input.isKeyPressed(Keybindings.KEY_JUMP)) {
    entity.add(new JumpingComponent());
}

This works like a charm. Every time I press the jump key the PlayerJumpingSystem fires up:

/**
* System used for the player jumping mechanism
* @author Tim
*/
public class PlayerJumpingSystem extends IteratingSystem {

    @SuppressWarnings("unused")
    private final static String TAG = PlayerJumpingSystem.class.getName();

    // Variable declaration
    @SuppressWarnings("unchecked")
    private static final Family family = Family.all(
            PositionComponent.class,
            VelocityComponent.class,
            PhysicsComponent.class,
            PlayerComponent.class,
            JumpingComponent.class).get();

    public PlayerJumpingSystem () {
        super(family);
    }

    @Override
    protected void processEntity (Entity entity, float deltaTime) {
        PositionComponent position = Mappers.position.get(entity);
        VelocityComponent velocity = Mappers.velocity.get(entity);
        PhysicsComponent physics = Mappers.physics.get(entity);

        if (velocity.y > 0) {
            // We are ascending
            physics.body.applyLinearImpulse(0, -Constants.JUMP_FORCE, position.x, position.y, true);
        }

        if (velocity.y < 0) {
            // We are descending
            physics.body.applyLinearImpulse(0, Constants.JUMP_FORCE, position.x, position.y, true);
        }

        else if (velocity.y == 0) {
            entity.remove(JumpingComponent.class);
            Gdx.app.log(TAG, "JumpingComponent removed");
        }
    }

}

It gives very weird jumping behaviour. Does anyone know how to implement jumping with Box2D in a good way? Thanks in advance!

\$\endgroup\$
1
\$\begingroup\$

you can do like this:

In your hud class

public jumpButtonPressed = false;
buttonJump.addListener(new InputListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
        jumpButtonPressed = true;
        return true;
    }
    public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
        jumpButtonPressed = false;
    }
});

In controll buttons

public void updateInput(float dt) {
    if ((Gdx.input.isKeyJustPressed(Input.Keys.UP) || Gdx.input.isKeyJustPressed(Input.Keys.W)) || hud.jumpButtonPressed == true) {
        hud.jumpButtonPressed = false;
        if (player.getState() != player.State.JUMPING) {

            player.b2body.setLinearVelocity(player.b2body.getLinearVelocity().x,0);
            player.b2body.applyForceToCenter(0, 160f, true);

        } 
    }
}

In player class

public State getState(){

if(b2body.getLinearVelocity().y >0 || (b2body.getLinearVelocity().y < 0 && estadoAnterior == State.JUMPING))
    return State.JUMPING;
else if(b2body.getLinearVelocity().y<0)
    return State.FALLING;
else
    return State.STOPSTATE;
}

in my case, if the player is falling and press jump button he will jump, but you can create more states like you want

\$\endgroup\$
  • \$\begingroup\$ Thank you so much for your great and exhaustive answer! You helped me a lot. \$\endgroup\$ – Tim Visser May 14 '16 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.