3

I'm using libgdx and box2d and I want to make my player's body jump. Nothing happens when I try to use some methods even if it's just for moving.

//gravity Vector2
Vector2 Gravity = new Vector2(0,-9.8);
//Box2d World
World world = New World(gravity);

//then I created my player body
public void createPlayer(){
    BodyDef def = new BodyDef();
    def.type = BodyDef.BodyType.DynamicBody;
    def.fixedRotation = true;
    def.position.set(80, 200);
    player = world.createBody(def);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(20, 20);
    player.createFixture(shape, 1f);
    shape.dispose();
}

//this is the jump method
public void jump(){
    if(Gdx.input.justTouched() == true) {
        player.applyForceToCenter(0, 900, false);
        System.out.println("touched");//to make sure the touch is working`
    }
}

Nothing happens and the player's body just falls until it collides to a static body, every time I touch the screen it only prints out the "touched" string.

UPDATE

Not sure if this is the reason why it's not working, I have two classes one for rendering and one for updating. On the update class I set the world.step() and not directly to the class where my body is.

//class for updating

public void update(float deltaTime){
    physics.world.step(1 / 60f, 6, 2);
    physics.jump(); //calling jump method from Physics class
}

Same thing for the rendering class, Box2dDebugRenderer is seperated where my bodies are

UPDATE

I fixed the problem, my Physics class which is connected to the Update and Render class had a reference to the create method of the main class, I don't understand because that's what I did for the constructor of my other classes.

public void create(){
    physics = new Physics(); //object for the Physics constructor.
}
CubeJockey
  • 2,209
  • 8
  • 24
  • 31
Kevin Bryan
  • 1,846
  • 2
  • 22
  • 45

2 Answers2

1

The problem is that you are working with the box2d scale which is in meter So your Box your creating is (20 meter x 20 meter) so a force of "300" is not enough to move it up to work with box2d you need to convert your values

divide all your box2d value by "WOLRD_TO_BOX" value scale

this post maybe helpful

Libgdx Box2D pixel to meter conversion?

Good luck

Update :

try change the

Gdx.input.justTouched() 

to

Gdx.input.isTouched()

because the JustTouched() method is called just once (not in a continue way)

Community
  • 1
  • 1
Netero
  • 3,761
  • 2
  • 29
  • 29
  • I tried to set the body to 5x5 meter then the force to 900 but the same thing happens, also can you set the weight of a body? – Kevin Bryan Oct 27 '15 at 15:45
  • you can put the density of body in fixtureDef: try something like : fixtureDef.density = 0.1f – Netero Oct 27 '15 at 15:48
  • see the update of my answer you are calling Gdx.input.justTouched() which is called just one time – Netero Oct 27 '15 at 15:55
1

Your jump() , World.step(...), debugRender.render(...) function should go on the render() method and the createPlayer() on should be on the create() method

 @Override
 public void create() {
        createPlayer()

 }

 @Override
 public void render() {
    //OpenGL settings
     Gdx.gl.glClearColor(0, 0, 0, 1);
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

     world.step(TIME_STEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
     jump();
     debugRendrer.render(world, camera.combined);
}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
Netero
  • 3,761
  • 2
  • 29
  • 29
  • Yes that's what I did, on my render class I have my own render method which goes to the render method on the main class, the same for my update method. – Kevin Bryan Oct 27 '15 at 18:49
  • Also this is the value for my step, physicsClass.world.step(1 / 60f, 6, 2); – Kevin Bryan Oct 27 '15 at 18:50
  • there is nothing wrong with the value that you are using, just make sure that the methods are in there right places – Netero Oct 27 '15 at 18:59
  • They are in the right places, I would have known if it's not cause every time i click the screen I printout a string – Kevin Bryan Oct 27 '15 at 19:02
  • one last thing to try : put the wake value to true player.applyForceToCenter(0, 300, true); and also reduce your density player.createFixture(shape, 0.1f); hope its work cause it all that all i got (:/ – Netero Oct 27 '15 at 19:33