Player doesn't jumps in Box2D implementation

| | August 11, 2015

Well, i’m trying to learn andEngine. I was able to successfully implement the move functionality for the player. Now, i’m trying to make the player jump using Box2D. I referred few online tutorials to do this but i am not able to make the player jump. Here’s my code:

public class MyActivity extends BaseGameActivity {
@Override
    public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
            throws Exception {
         scene = new Scene();
        final float centerX = (CAMERA_WIDTH - back.getWidth()) / 2;
        final float centerY = (CAMERA_HEIGHT - this.back.getHeight()) / 2;

        Sprite backgr = new Sprite(centerX, centerY, back,
                this.getVertexBufferObjectManager());
         rcs1 = new Sprite(85, 250, rc1,
                this.getVertexBufferObjectManager());
         rcs2 = new Sprite(195, 240, rc2,
                this.getVertexBufferObjectManager());
         rcs3 = new Sprite(295, 250, rc3,
                this.getVertexBufferObjectManager());


        ans = new AnimatedSprite(86, 245, ttr,
                this.getVertexBufferObjectManager()) {
            @Override
            protected void onManagedUpdate(float pSecondsElapsed) {
                this.setPosition(pX, pY);
                super.onManagedUpdate(pSecondsElapsed);

            }
        };
        pw = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH),
                false);
        final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(2f,0f, 0.9f);
        body = PhysicsFactory.createBoxBody(pw, ans, BodyType.DynamicBody,
                PhysicsFactory.createFixtureDef(0, 0, 0));
        bodyr1 = PhysicsFactory.createBoxBody(pw, rcs1, BodyType.StaticBody,
                objectFixtureDef);
        bodyr2 = PhysicsFactory.createBoxBody(pw, rcs2, BodyType.StaticBody,
                objectFixtureDef);
        bodyr3 = PhysicsFactory.createBoxBody(pw, rcs3, BodyType.StaticBody,
                objectFixtureDef);

        pw.registerPhysicsConnector(new PhysicsConnector(ans, body, true, true));
        scene.attachChild(backgr);
        scene.attachChild(rcs1);
        scene.attachChild(rcs2);
        scene.attachChild(rcs3);
        scene.attachChild(ans);
        pw.setContactListener(this);
        scene.registerUpdateHandler(pw);
        pOnCreateSceneCallback.onCreateSceneFinished(scene);

    }
private void jump() {

            body.setLinearVelocity(new Vector2(body.getLinearVelocity().x, 12)); 
                }
}

Please Help!

One Response to “Player doesn't jumps in Box2D implementation”

  1. First, when are you calling the jump() method? Make sure it really gets called, maybe by using a log message. Second, instead of

    body.setLinearVelocity(new Vector2(body.getLinearVelocity().x, 12));
    

    you should do

    body.setApplyForce(new Vector2(0, 120));
    

    Setting a velocity directly is frowned upon and should be done only in a few exceptional cases such as adding a new body to the simulation that has some initial velocity. Otherwise use forces or linear impulses.

    Third, what does this code do?

        ans = new AnimatedSprite(86, 245, ttr,
                this.getVertexBufferObjectManager()) {
            @Override
            protected void onManagedUpdate(float pSecondsElapsed) {
                this.setPosition(pX, pY);
                super.onManagedUpdate(pSecondsElapsed);
    
            }
        };
    

    It looks to me that you want your animated sprite to be driven by the physics body (you register the connector) but then you are setting its position to some pX and pY. The physics body might be jumping, but the Sprite is drawn always at [pX, pY] due to this code.

Leave a Reply