[SOLVED]Inconsistent Box2D speeds

Anything libgdx related goes here!

[SOLVED]Inconsistent Box2D speeds

Postby r89 » Sat Aug 27, 2011 4:36 pm

Edit: Problem solved, i was passing a massive deltaTime to the world. Found it and got a half decent solutuion with the following code (obviously not a final solution)
Code: Select all
        if(Gdx.graphics.getDeltaTime() < .015)
           world.step(Gdx.graphics.getDeltaTime(), 3, 3);
        else if(Gdx.graphics.getDeltaTime() < .065)
           world.step(Gdx.graphics.getDeltaTime()/2, 3, 3);
           world.step(Gdx.graphics.getDeltaTime()/3, 3, 3);

Okay im not really sure where this should go so im posting it here.

I have a segment of code where a projectile is created with an impulse based on user input.
Code: Select all
private void createProjectiles(ProjectileID id)
   PolygonShape pPoly = new PolygonShape();
   pPoly.setAsBox(.8f, .8f);
   BodyDef pDef = new BodyDef();
   pDef.type = BodyType.DynamicBody;
   pDef.position.x = 10 + (float)Math.abs(7*(float)Math.cos(MathUtils.degreesToRadians*cannonAngle));
   pDef.position.y = 9 + (float)Math.abs(7*(float)Math.sin(MathUtils.degreesToRadians*cannonAngle));   
   Body pBody = world.createBody(pDef);

   Vector2 theImpulse = new Vector2(power*(float)Math.cos(MathUtils.degreesToRadians*cannonAngle),
   pBody.applyLinearImpulse(theImpulse, pBody.getLocalCenter());
   projectiles.add(new Projectile(pBody, pType));

This segment of code is called by a touchUp event, if that matters.
The values that are placed in the impulse are always the correct values. That being said for some reason the velocity of the body in question is dependent on FPS. I thought that perhaps it was the impulse using the getDeltaTime as its duration, so instead i used a force in place of an impulse and it is still dependant on FPS. After that frustration i simply set the velocity equal to theImpulse. Still no good. Im obviously missing something very easy. any help would be abliged.

When the FPS are 60+ it works fine, however when i port it to my phone and the FPS bounce from 20-35 i get different speeds of the projectile based on the FPS (or getDeltaTime not sure) at that instant.

I also tried to change the values of the pDef instead and i cannot get it to operate correctly. Any help would be greatly appreciated.
Posts: 1
Joined: Sat Aug 27, 2011 4:24 pm

Re: [SOLVED]Inconsistent Box2D speeds

Postby t4ils » Sat Aug 27, 2011 6:53 pm

I always pass a fixed timestep to box2d.

The official manual goes that way about it :
You can get away with larger time steps, but you will have to be more careful about setting up the definitions for your world. We also don't like the time step to change much. A variable time step produces variable results, which makes it difficult to debug. So don't tie the time step to your frame rate (unless you really, really have to). Without further ado, here is the time step.

2.4 Simulating the World (of Box2D)
Posts: 122
Joined: Thu Mar 03, 2011 8:54 am
Location: Savigny-sur-Orge, France

Re: [SOLVED]Inconsistent Box2D speeds

Postby chuckDiesel » Sun Aug 28, 2011 2:48 am

I'll try to help you out.. do something like this.. although mine may be wrong.. please feel free to comment..

static float FIXED_TIMESTEP = 0.008f;
static float MINIMUM_TIMESTEP = 0.001f;

static final int VELOCITY_ITERATIONS = 6;
static final int POSITION_ITERATIONS = 6;

// maximum number of steps per tick to avoid spiral of death
static final int MAXIMUM_NUMBER_OF_STEPS = 20;

public static float GRAVITY = -9.8f;
public static float LAST_PHYSICS_STEP = 0;
public static float STEPS_PERFORMED = 0;
public static float FRAME_TIME = 0;
public static float DELTA_TIME = 0;
public static World CURRENT_WORLD = "Your world"

public static void stepPhysicsWorld() {
float startTime = System.nanoTime();
float frameTime = Gdx.graphics.getDeltaTime();
FRAME_TIME = frameTime;
int stepsPerformed = 0;
while ( (frameTime > 0.0) && (stepsPerformed < MAXIMUM_NUMBER_OF_STEPS) ){
float deltaTime = Math.min(frameTime, FIXED_TIMESTEP );
frameTime -= deltaTime;
if (frameTime < MINIMUM_TIMESTEP) {
deltaTime += frameTime;
frameTime = 0.0f;
DELTA_TIME = deltaTime;


STEPS_PERFORMED = stepsPerformed;
float endTime = System.nanoTime();
LAST_PHYSICS_STEP = (endTime - startTime);
Posts: 29
Joined: Sun Aug 07, 2011 4:39 pm

Return to Libgdx

Who is online

Users browsing this forum: No registered users and 1 guest