beni
beni

Reputation: 141

LibGDX applyLinearImpulse trying to implement Gravity....Confused

Street Fighter 2d clone I'm trying to get the player Ken to come back down after jumping. He just goes higher and higher but I want it to be like gravity so he's pulled back down. Then I will set a ground level where he cant pass through.

I've researched the applyLinearImpulse method to be called on the body which is initialised beforre asa I kept getting null pointer exception it seems that now it don't crash but Ken just gets drawn further up the Y Axis. Its left me very confused.

Any advice links greatly appreciated.

Ken class

public class Ken extends Player {

    private static final int FRAME_COLS = 6, FRAME_ROWS = 1;
    private static final int COLUMNS_KICK  = 6;
    private static final int COLUMNS_LEFT = 8;
    private static final int COLUMNS_RIGHT = 8;
    private static final int COLUMNS_JUMP = 10;
    private static final int COLUMNS_PUNCH = 6;
    private static final int COLUMNS_FRONTFLIP = 8;
    private static final int COLUMNS_BACKFLIP = 8;
    public static final int FRAME_FRONTFLIP = 1;
    public static final int FRAME_BACKLIP = 1;

    float x, y;
    Animation<TextureRegion> walkAnimation;
    Animation<TextureRegion> kickAnimation;
    Animation<TextureRegion> punchAnimation;
    Animation<TextureRegion> leftAnimation;
    Animation<TextureRegion> rightAnimation;
    Animation<TextureRegion> jumpAnimation;
    Animation<TextureRegion> frontFlipAnimation;
    Animation<TextureRegion> backFlipAnimation;
    Texture walkSheet;
    Texture kickSheet;
    Texture punchSheet;
    Texture leftSheet;
    Texture rightSheet;
    Texture jumpSheet;
    Texture frontFlipSheet;
    Texture backFlipSheet;

    public Body body;

    public World world;
    boolean alive = true;

    private final static int STARTING_X = 50;
    private final static int STARTING_Y = 30;
    TextureRegion reg;
    float stateTime;

    public Ken(GameScreen screen){
        this.world = screen.getWorld();
        defineKen();
        createIdleAnimation();
        kickAnimation();
        punchAnimation();
        lefttAnimation();
        righttAnimation();
        jumpAnimation();

        frontFlipAnimation();

        backFlipAnimation();


        this.setPosition(STARTING_X, STARTING_Y);
    }

    public void createIdleAnimation() {
        walkSheet = new Texture(Gdx.files.internal("ken/idle.png"));

        TextureRegion[][] tmp = TextureRegion.split(walkSheet,
                walkSheet.getWidth() / FRAME_COLS,
                walkSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }

        walkAnimation = new Animation<TextureRegion>(0.1f, walkFrames);
        stateTime = 0f;
        reg=walkAnimation.getKeyFrame(0);
    }


    public void kickAnimation(){

        kickSheet = new Texture(Gdx.files.internal("ken/kick_low.png"));

        TextureRegion [][] tmp = TextureRegion.split(kickSheet, kickSheet.getWidth() / COLUMNS_KICK,
                kickSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] kickFrames = new TextureRegion[COLUMNS_KICK * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                kickFrames[index++] = tmp[i][j];
            }
        }

        kickAnimation = new Animation<TextureRegion>(8f, kickFrames);
        stateTime = 6f;
        reg = kickAnimation.getKeyFrame(1);

    }

    public void lefttAnimation(){

        leftSheet = new Texture(Gdx.files.internal("ken/parry_b.png"));

        TextureRegion [][] tmp = TextureRegion.split(leftSheet, leftSheet.getWidth() / COLUMNS_LEFT,
                leftSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] leftFrames = new TextureRegion[COLUMNS_LEFT * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < COLUMNS_LEFT; j++) {
                leftFrames[index++] = tmp[i][j];
            }
        }

        leftAnimation = new Animation<TextureRegion>(0.1f, leftFrames);
        stateTime = 0f;
        reg = punchAnimation.getKeyFrame(0);

    }

    public void righttAnimation(){

        rightSheet = new Texture(Gdx.files.internal("ken/parry_f.png"));

        TextureRegion [][] tmp = TextureRegion.split(rightSheet, rightSheet.getWidth() / COLUMNS_RIGHT,
                rightSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] rightFrames = new TextureRegion[COLUMNS_RIGHT * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < COLUMNS_RIGHT; j++) {
                rightFrames[index++] = tmp[i][j];
            }
        }

        rightAnimation = new Animation<TextureRegion>(0.1f, rightFrames);
        stateTime = 0f;
        reg = rightAnimation.getKeyFrame(0);

    }


    public void punchAnimation(){

        punchSheet = new Texture(Gdx.files.internal("ken/punch.png"));

        TextureRegion [][] tmp = TextureRegion.split(punchSheet, punchSheet.getWidth() / COLUMNS_PUNCH,
                punchSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] punchFrames = new TextureRegion[COLUMNS_PUNCH * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < COLUMNS_PUNCH; j++) {
                punchFrames[index++] = tmp[i][j];
            }
        }

        punchAnimation = new Animation<TextureRegion>(0.1f, punchFrames);
        stateTime = 0f;
        reg = punchAnimation.getKeyFrame(0);

    }



    public void jumpAnimation(){

        jumpSheet = new Texture(Gdx.files.internal("ken/jump.png"));

        TextureRegion [][] tmp = TextureRegion.split(jumpSheet, jumpSheet.getWidth() / COLUMNS_JUMP,
                jumpSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] jumpFrames = new TextureRegion[COLUMNS_JUMP * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < COLUMNS_JUMP; j++) {
                jumpFrames[index++] = tmp[i][j];
            }
        }



        jumpAnimation = new Animation<TextureRegion>(0.1f, jumpFrames);
        stateTime = 0f;


        reg = jumpAnimation.getKeyFrame(0);



    }



    public void frontFlipAnimation(){

        frontFlipSheet = new Texture(Gdx.files.internal("ken/front_flip.png"));

        TextureRegion [][] tmp = TextureRegion.split(frontFlipSheet, frontFlipSheet.getWidth() / COLUMNS_FRONTFLIP,
                frontFlipSheet.getHeight() / FRAME_ROWS);

        TextureRegion[] frontFlipFrames = new TextureRegion[COLUMNS_FRONTFLIP * FRAME_FRONTFLIP];
        int index = 0;
        for (int i = 0; i < FRAME_FRONTFLIP; i++) {
            for (int j = 0; j < COLUMNS_FRONTFLIP; j++) {
                frontFlipFrames[index++] = tmp[i][j];
            }
        }

        frontFlipAnimation = new Animation<TextureRegion>(0.1f, frontFlipFrames);
        stateTime = 0f;
        reg = frontFlipAnimation.getKeyFrame(0);

    }


    public void backFlipAnimation(){

        backFlipSheet = new Texture(Gdx.files.internal("ken/back_flip.png"));

        TextureRegion [][] tmp = TextureRegion.split(backFlipSheet, backFlipSheet.getWidth() / COLUMNS_BACKFLIP,
                backFlipSheet.getHeight() / FRAME_BACKLIP);

        TextureRegion[] backFlipFrames = new TextureRegion[COLUMNS_BACKFLIP * FRAME_BACKLIP];
        int index = 0;
        for (int i = 0; i < FRAME_BACKLIP; i++) {
            for (int j = 0; j < COLUMNS_BACKFLIP; j++) {
                backFlipFrames[index++] = tmp[i][j];
            }
        }

        backFlipAnimation = new Animation<TextureRegion>(0.1f, backFlipFrames);
        stateTime = 0f;
        reg = backFlipAnimation.getKeyFrame(0);

    }



    @Override
    public void act(float delta) {
        super.act(delta);
        stateTime += delta;





        stateTime += delta;
        reg = walkAnimation.getKeyFrame(stateTime,true);

        if(Gdx.input.isKeyPressed(Input.Keys.A)){
            reg = kickAnimation.getKeyFrame(stateTime, false);
            this.addAction(Actions.moveTo(getX() +2, getY(), 1 / 10F));

        }

        if(Gdx.input.isKeyPressed(Input.Keys.S)){
            reg = punchAnimation.getKeyFrame(stateTime, false);
        }

        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            reg = leftAnimation.getKeyFrame(stateTime, true);
            this.addAction(Actions.moveTo(getX() - 10, getY(), 1 / 10f ));
        }

        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            reg = rightAnimation.getKeyFrame(stateTime, true);
            this.addAction(Actions.moveTo(getX() + 10, getY(), 1 /10f));
        }

        if(Gdx.input.isKeyPressed(Input.Keys.UP)){
            body.applyLinearImpulse(new Vector2(0, 20), body.getWorldCenter(), true);
            reg = jumpAnimation.getKeyFrame(stateTime, false);
            this.addAction(Actions.moveTo(getX(), getY() + 10, 1/ 10f));


        }

        if(Gdx.input.isKeyPressed(Input.Keys.D)){
            reg = frontFlipAnimation.getKeyFrame(stateTime, true);
            this.addAction(Actions.moveTo(getX() + 5, getY(), 1 / 10f));
        }

        if(Gdx.input.isKeyPressed(Input.Keys.W)){
            reg = backFlipAnimation.getKeyFrame(stateTime, true);
            this.addAction(Actions.moveTo(getX() - 5, getY(), 1 / 10F));
        }
    }





    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        Color color = getColor();
        batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
        batch.draw(reg,getX(),getY(),getWidth()/2,getHeight()/2,getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
    }

    private void defineKen(){
        BodyDef bdef = new BodyDef();
        bdef.position.set(32 / 100, 32 / 100);
        bdef.type = BodyDef.BodyType.DynamicBody;

        body = world.createBody(bdef);

        FixtureDef fdef = new FixtureDef();
        CircleShape shape = new CircleShape();
        shape.setRadius(7 / 100);


        fdef.shape = shape;
        body.createFixture(fdef).setUserData(this);
        body.createFixture(fdef).setUserData(this);
    }




}

No gravity........

Repo

Thanks alot

Upvotes: 0

Views: 49

Answers (1)

Morchul
Morchul

Reputation: 2037

You work with Box2d and Box2d is a 2d physics engine so it also integrated gravity.

First, when you create your World you can define the gravity force:

world = new World(new Vector2(0,-15f), true);

This will pull your Player down.

Then you must update the world every frame. So call in the render() method:

world.step(delta, 6, 2);

Now the Body position will be calculated by the world and gravity apply to the body.

Important now is that you first have a Static body as Ground otherwise, the body will fall down infinitely. Secondly, you must change the place where you draw the Image of Ken to the place of the Body so in act() method update the position:

setX(body.getPosition().x);
setY(body.getPosition().y);

Maybe you look for some Box2d tutorials to have a better understanding: https://www.gamedevelopment.blog/full-libgdx-game-tutorial-box2d/

Upvotes: 1

Related Questions