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.
}