Max step physics (to avoid choppyness and bad simulation)

  ... the case you feel the need for a new feature or want to submit one.

Max step physics (to avoid choppyness and bad simulation)

Postby chozabu » Sat Dec 04, 2010 2:47 pm

Hiya,
I made a subclass of physics world :) It is like a regular physics world, but with a cap on the timestep - I was finding if my phone did something in the background, or there was another reason for choppyness my simulation would bcome unstable! buildings fall down, etc. And fixed time step ran too slow.
The problem with this is as it can hover between time based physics and frame-based physics Timing players completing a level and similar things wont be as fair - but that could have its time-steps capped too.
Basically, I'd rather slow-mo and stable than jerky and consistant time.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.chozabu.android.BikeGame;
  2.  
  3. import org.anddev.andengine.extension.physics.box2d.PhysicsWorld;
  4.  
  5. import com.badlogic.gdx.math.Vector2;
  6. import com.badlogic.gdx.physics.box2d.World;
  7.  
  8. public class MaxStepPhysicsWorld extends PhysicsWorld {
  9.         public static final int STEPSPERSECOND_DEFAULT = 60;
  10.         private final float mStepLength;
  11.         public MaxStepPhysicsWorld(final int pStepsPerSecond, final Vector2 pGravity, final boolean pAllowSleep) {
  12.                 super(pGravity, pAllowSleep);
  13.                 this.mStepLength = 1.0f / pStepsPerSecond;
  14.         }
  15.  
  16.         public MaxStepPhysicsWorld(final int pStepsPerSecond, final Vector2 pGravity, final boolean pAllowSleep, final int pVelocityIterations, final int pPositionIterations) {
  17.                 super(pGravity, pAllowSleep, pVelocityIterations, pPositionIterations);
  18.                 this.mStepLength = 1.0f / pStepsPerSecond;
  19.         }
  20.  
  21.         @Override
  22.         public void onUpdate(final float pSecondsElapsed) {
  23.                 this.mRunnableHandler.onUpdate(pSecondsElapsed);
  24.                
  25.                 float stepLength = pSecondsElapsed;
  26.                 if(pSecondsElapsed>= this.mStepLength){
  27.                         stepLength = this.mStepLength;
  28.                 }
  29.                 this.mWorld.step(stepLength, this.mVelocityIterations, this.mPositionIterations);
  30.                
  31.                 this.mPhysicsConnectorManager.onUpdate(pSecondsElapsed);
  32.         }
  33.  
  34. }
  35.  
  36.  
Parsed in 0.014 seconds, using GeSHi 1.0.8.4
chozabu
 
Posts: 107
Joined: Sat Oct 30, 2010 10:57 pm

Re: Max step physics (to avoid choppyness and bad simulation

Postby D13 » Fri Dec 10, 2010 7:15 pm

This is a really good idea I think. Thanks! Instead of penalizing faster phones, cut a bit of a break for slower phones when they experience a momentary lag. I think I may incorporate this into my own project that involves structures of many box2d bodies, as it was something I was a bit concerned about. My Desire does pretty good for my game, but I was always a bit unsure how some mid-performance devices would handle things. I don't think the differences that may crop up between accumulated game/engine time vs physics time will affect my project, but I may consider adding a "lag accumulator" to your idea that will try to make the physics world time gradually catch up to the engine time if it can. That way, any periodic lags can be handled on slower phones without destabilizing Box2D structures, but try not to give them an advantage either by having the physics time fall too far behind game time.
D13
 
Posts: 172
Joined: Wed Oct 06, 2010 6:08 am
Location: Toronto, Canada

Re: Max step physics (to avoid choppyness and bad simulation

Postby pekayatt » Fri Jul 08, 2011 8:41 pm

Man, I just need to say that this was exactly what I was looking for to make my game compatible with it clone for iPhone/COCOS2D...

Thank you very much!!! I would like to see this feature added to the main core, as a another implementation of the PhysicsWorld :D!
--
Pedro Kayatt

Game Lead Developer @ http://bit.ly/nakedMonkey
Skype: pekayatt
pekayatt
 
Posts: 41
Joined: Fri Jun 17, 2011 7:41 pm
Location: São Paulo

Re: Max step physics (to avoid choppyness and bad simulation

Postby pekayatt » Fri Jul 08, 2011 11:35 pm

Guys, in fact I am having a little "trouble" here... as I try to match the physics with our iPhone big brother I am sensible diferences between it. The major one is the speed, even trying to fix the fps to 30 we still have a much faster game.
Also the speed in the Galaxy 5 is much higher than in the Nexus One, which is a very strage fact given that the N1 is a much faster device oO!

Any idea?

EDIT: Solve it changing my Engine to a FPSLimitedEngine :D! Sorry my noob :P
--
Pedro Kayatt

Game Lead Developer @ http://bit.ly/nakedMonkey
Skype: pekayatt
pekayatt
 
Posts: 41
Joined: Fri Jun 17, 2011 7:41 pm
Location: São Paulo

Re: Max step physics (to avoid choppyness and bad simulation

Postby RyugaZ » Wed Nov 07, 2012 6:22 pm

Man u r great! Thx, since it fixed my box2d problems (at least those I have until now).
RyugaZ
 
Posts: 98
Joined: Fri Mar 09, 2012 4:36 pm

Re: Max step physics (to avoid choppyness and bad simulation

Postby chozabu » Wed Nov 07, 2012 11:31 pm

Weyhey! Glad to hear this little snippet is still being useful after so long!
chozabu
 
Posts: 107
Joined: Sat Oct 30, 2010 10:57 pm

Re: Max step physics (to avoid choppyness and bad simulation

Postby bilgihan1991 » Wed Jul 16, 2014 8:47 am

Thank you so much for this class , it works perfectly . Now i know the problem's solution :)
Colorful Squares

Image


Sign up with StartApp here for $15 when you hit 100k ad impressions!
They run a great range of Interstitial Ads for apps and games on a global scale, with banners available too.
bilgihan1991
 
Posts: 14
Joined: Mon Jul 07, 2014 12:05 pm

Re: Max step physics (to avoid choppyness and bad simulation

Postby chozabu » Wed Jul 16, 2014 8:55 am

Hey - I should warn, this class will give significantly less deterministic physics.

In wheelz, I think I switched to a fixed step world, but the change below may help

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 this.mPhysicsConnectorManager.onUpdate(pSecondsElapsed);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4

could perhaps be replaced with

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 this.mPhysicsConnectorManager.onUpdate(stepLength);
Parsed in 0.010 seconds, using GeSHi 1.0.8.4
chozabu
 
Posts: 107
Joined: Sat Oct 30, 2010 10:57 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 3 guests