0

I'm trying to make a little game with the java game engine called libGDX. I already got the player but he can't jump and I dont know why.

This is my GameScreen class:

public class GameScreen extends ScreenManager{
    //For the view of the game and the rendering
    private SpriteBatch batch;
    private OrthographicCamera cam;

    //DEBUG
    private Box2DDebugRenderer b2dr;

    //World, Player and so on
    private GameWorld world;
    private Player player;
    private Ground ground;


    public static float w, h;

    public GameScreen(Game game) {
        super(game);
        //vars
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();

        //view and rendering
        batch = new SpriteBatch();
        cam = new OrthographicCamera();
        cam.setToOrtho(false, w/2, h/2);

        //debug
        b2dr = new Box2DDebugRenderer();

        //world, bodies ...
        player = new Player(world);
        ground = new Ground(world);

    }

    @Override
    public void pause() {


    }

    @Override
    public void show() {


    }

    @Override
    public void render(float delta) {
        //clearing the screen
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        //updating
        update(Gdx.graphics.getDeltaTime());

        //render
        batch.setProjectionMatrix(cam.combined);

        //debug
        b2dr.render(world.getWorld(), cam.combined);    


    }

    @Override
    public void resize(int width, int height) {


    }

    @Override
    public void hide() {


    }


    @Override
    public void dispose() {


    }

    @Override
    public void onKlick(float delta) {


    }

    public void update(float delta){
        world.update(delta);
        player.keyInput();
        System.out.println(player.getBody().getPosition().x);
        System.out.println(player.getBody().getPosition().y);
    }



}

and this is my Player class:

public class Player {
    public static Body body;
    public static BodyDef def;
    private FixtureDef fd;

    //set form
    private PolygonShape shape;

    private GameScreen gs;

    public Player(GameWorld world){
        def = new BodyDef();
        def.fixedRotation = true;
        def.position.set(gs.w / 4, gs.h / 4);
        def.type = BodyType.DynamicBody;

        body = world.getWorld().createBody(def);

        shape = new PolygonShape();
        shape.setAsBox(32 / 2, 32 / 2);

        fd = new FixtureDef();
        fd.shape = shape;
        fd.density = 30;

        body.createFixture(fd);
        shape.dispose();
    }

    public static Body getBody() {
        return body;
    }

    public static BodyDef getDef() {
        return def;
    }

    public static void keyInput(){
        if(Gdx.input.isKeyPressed(Input.Keys.UP)){
            body.applyForceToCenter(0, 900, false);
            System.out.println("PRESSED");
        }
    }



}

this finally is my GameWorld class:

public class GameWorld {
    public static World world;

    public GameWorld(){
        world = new World(new Vector2(0f, -10f), false);

    }

    public void update(float delta){
        world.step(1 / 60f, 5, 2);
        System.out.println("WorldUPDATE");
    }

    public static World getWorld() {
        return world;
    }



}

I don't know why the body can't jump please help me if you can (: Thank you in advance.

1 Answers1

0

Force pushes the body each timestep, while gravity pushes it back down again. There is a function applyLinearImpulse() which pushes it like once with all its "force" before gravity interferention.

When you are using applyForceToCenter() body should be pushed up, but its very slow process compared to impulse.

Dawid Fieluba
  • 1,271
  • 14
  • 34