2
votes

I am new to box2d and libgdx game development framework.

I have created a world and a circle shape.

I am in Trouble with Gravity. The Circle I created in libgdx box2d is not falling under gravity. plz help me i stuck at this problem .

public void render(float delta) {
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 

    box2dDebugRenderer.render(world, orthographicCamera.projection);

    world.step(TIME_STEMP, VELOCITY_ITERAIONS, POSITION_ITERATION);
    System.out.println(b.getPosition().y);
}

@Override
public void resize(int width, int height) {
    orthographicCamera.setToOrtho(false, width/10, height/10);
    orthographicCamera.update();
}

@Override
public void show() {
    world = new World(new Vector2(0, -9.81f), true);
    box2dDebugRenderer = new Box2DDebugRenderer();
    orthographicCamera = new OrthographicCamera();


    BodyDef balldef = new BodyDef();
    balldef.type = BodyType.DynamicBody;
    balldef.position.set(0, 1);



    CircleShape ballshape = new CircleShape();
    ballshape.setRadius(1f);


    FixtureDef ballfixture = new FixtureDef();
    ballfixture.density = 1000f;
    ballfixture.friction = .3f;
    ballfixture.restitution = .7f;
    ballfixture.shape = ballshape;

    b = world.createBody(balldef);
    f = b.createFixture(ballfixture);


} 
1
What happens then? The body just stays quiet?ssantos
yes it dont change its position in y direction downwordsRavi Kumar Mistry
Could you post the values for your step constants?ssantos
private static final float TIME_STEMP = 1/60; private static final int VELOCITY_ITERAIONS = 8; private static final int POSITION_ITERATION = 3;Ravi Kumar Mistry

1 Answers

6
votes

This is your problem.-

final float TIME_STEMP = 1/60;

You're doing an integer division resulting in 0. In other words, your world is stuck in time. Try this one.-

final float TIME_STEMP = 1.0f / 60.0f;