What goes into Fixed TimeStep for correct Physics (Box2D)?

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

What goes into Fixed TimeStep for correct Physics (Box2D)?

Postby just4phil » Tue Jun 05, 2012 7:30 am

hi folks,

after searching this forum a long time and reading lots of articles about the fixed timestep i found this code-snippet:
http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=1845&p=10046#p10046
(That post has been closed as "solved" so i start this new thread)

1.) isnt this snippet missing: box2dwelt.setAutoClearForces(false); ?
(in my game without setAutoClearForces the forces are stronger than they should be!)

2.) is this solution best-practice??

3.) do i update my "normal" game-logic simulation together with the box2d.step within this fixed timestep or is the game-logic updated with the normal deltatime (like the renderer)?

4.) i tried my game with this logic on a low-end galaxy 3 i5800 and it was unplayable.... looked like a spiral of death but i am not sure...
- how would/should this logic perform on low-end-phones?

5.) to me it seems that i get problems with box2d-collision detection (contact listener): FPS is falling down when box2d objects have multiple collisions(??) is there anything to do with the contact-listener if the box2d-step goes into the fixed timestep??

i hope that someone can help!
there are lots of posts about the fixed timestep but IMHO a real solution addressing game logic AND physic logic is missing.

bye
phil
just4phil
 
Posts: 935
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby just4phil » Tue Jun 05, 2012 7:36 pm

when i use this fixed timestep (without interpolation), the physics become predictable. thats cool to see.
but i get massive performance-problems, that seem to come from box2d collision detection.

Code: Select all
   private float accum = 0f;
   private final float step = 1f / 60f;
   private final float maxDelta = 1f / 20f; // keep from spiraling to death
   static final int VELOCITY_ITERATIONS = 3;
   static final int POSITION_ITERATIONS = 3;


         accum += Math.min(deltaTime, maxDelta);

         while (accum > step) {
               
            level.world.update(step, timeDiff, accel, touchState, guiCam);                // update my normal game logic
            level.world.box2dwelt.step(step, VELOCITY_ITERATIONS, POSITION_ITERATIONS);     // update box2d physics
            accum -= step;
         }


going with variable deltatime doesnt cause problems, but physics arent preictable and change evry time a bit

Code: Select all
      level.world.update(deltaTime, timeDiff, accel, touchState, guiCam);
      
      level.world.updateBox2d(deltaTime, VELOCITY_ITERATIONS, POSITION_ITERATIONS);



i am really confused, dont know how to fix that
and i am not sure if it is really neccessary that i use the fixed timestep (if predictable physics isnt neccessary)?

bye
phil
just4phil
 
Posts: 935
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby bach » Wed Jun 06, 2012 5:43 am

You definitely want to fix your timestep... Otherwise your physics behaviour is bound to performace. Which might not be a problem if it's just 5fps difference. But if someone plays this at 10fps compared to 60fps it'll be a significant difference! Try and slow down your game manually and you'll see why you need a fixed time step.

Best reference for this is still: http://gafferongames.com/game-physics/f ... -timestep/

Cheers,
Bach
bach
 
Posts: 713
Joined: Mon Mar 07, 2011 1:50 am

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby just4phil » Wed Jun 06, 2012 12:27 pm

Hi and thanks,
I know that and i read the gafferon article 20 times.
I am looking for a solution that works with box2d.

There must be a best practice somwhere with a good code snippet !?

Bye phil
just4phil
 
Posts: 935
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby broken-e » Wed Jun 06, 2012 5:56 pm

One thing about performance is that you are updating your entire game logic (as far as I can tell) each world step, rather than each frame. Your step is 1/60 of a second so more than likely you are averaging more than one per frame. I'd take the game logic out of that step while loop and put it after (or before).

Code: Select all
while (accum > step) {
    level.world.box2dwelt.step(step, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
    accum -= step;
}
level.world.update(step, timeDiff, accel, touchState, guiCam);
my latest release: SnowPaper
broken-e
 
Posts: 700
Joined: Sun Feb 06, 2011 7:20 am

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby just4phil » Wed Jun 06, 2012 6:57 pm

ok
i ll try that
but than i guess it should be update(deltatime) instead of update(step), like this:

Code: Select all
while (accum > step) {
    level.world.box2dwelt.step(step, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
    accum -= step;
}
level.world.update(deltatime, timeDiff, accel, touchState, guiCam);


??

thanks!
bye
phil
just4phil
 
Posts: 935
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby broken-e » Wed Jun 06, 2012 8:03 pm

just4phil wrote:ok
i ll try that
but than i guess it should be update(deltatime) instead of update(step), like this:

Code: Select all
while (accum > step) {
    level.world.box2dwelt.step(step, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
    accum -= step;
}
level.world.update(deltatime, timeDiff, accel, touchState, guiCam);


??

thanks!
bye
phil

Ah yes, it probably should be.
my latest release: SnowPaper
broken-e
 
Posts: 700
Joined: Sun Feb 06, 2011 7:20 am

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby just4phil » Wed Jun 06, 2012 8:31 pm

after more testing it seems that under android i only get predictable physics results when both update calls are within the fixed-time-step!

i have to do more investigation to find out what causes my performance problems .....
just4phil
 
Posts: 935
Joined: Fri Feb 03, 2012 10:07 pm
Location: Berlin

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby kalle_h » Wed Jun 06, 2012 9:54 pm

Try to update logic and physic only 30times per second.
Then render as fast as possible using interpolating scheme.
kalle_h
 
Posts: 666
Joined: Thu Dec 29, 2011 9:50 pm

Re: What goes into Fixed TimeStep for correct Physics (Box2D

Postby broken-e » Wed Jun 06, 2012 10:10 pm

just4phil wrote:after more testing it seems that under android i only get predictable physics results when both update calls are within the fixed-time-step!

i have to do more investigation to find out what causes my performance problems .....

Well truthfully I have no idea what you are doing in the game logic update. Are you manipulating physics in there?
my latest release: SnowPaper
broken-e
 
Posts: 700
Joined: Sun Feb 06, 2011 7:20 am

Next

Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest