Frame rate independent physics using delta time?

Hey guys,

I’m just doing some performance testing on older devices and noticed that moving things with physics shows a drop in fps.

If I was moving an objecting using :translate(), I could just use deltaTime like so:

object:translate(xAmount\*deltaTime,yAmount\*deltaTime);

However, when I have the main character jumping across the screen using :setLinearVelocity(), there doesn’t seem to be a way of controlling this.

Any ideas?

Thanks!

rob,

I might have actually found how to do this. Just testing it now:

physics.setTimeStep(dt/60); -- Run every frame

This seems to be working nicely, apart from the accuracy of the physics simulation is reduced at low frame rates. I’ve added the following to compensate for that:

physics.setPositionIterations( 8\*dt ) -- Run every frame

@roballison182,

Are you trying to run at 30 FPS or 60 (check your config.lua file).  If 60, I suggest 30.  

If you’re running 60 FPS, you’re doing twice as many physics calculations.  While these are still wholly dependent on delta time and not a multiple of the frame, you will see a decline in performance, hiccups, and lag on older devices if you have lots of physics objects and a high frame rate.

This all assumes you’re running at 60FPS.

-Ed

Hi Ed, Thanks for getting back. The game is running at 60fps, but it’s crucial that it does. I understand that older devices will have a harder time, but that’s worth it if newer devices have smooth gameplay. The changes I’ve made above work really well. What it does is change what happens when there’s a performance drop: Before: Gameplay would slow down After: You see some dropped frames (basically running at a lower frame rate temporarily). But the game still runs at the same speed. Thanks, Rob

You can selectively run 60FPS on some devices and 30FPS on others.  Just make the config.lua file adjust based on system details.

https://docs.coronalabs.com/daily/api/library/system/getInfo.html

  • Am I on android,
  • Is this device running an older version of android (more likely to be an old device)
  • … more  There are are a number of details you can extract that would help you set up a targeted config.lua.

Regardless, I’d try this:

  • Take a device that is giving you trouble.
  • Remove your current fix
  • Change to 30 FPS 
  • See how it plays.  

I take it that you’ve got fast moving objects so you don’t want large skips between updates? i.e. 60FPS equals (approximately) twice as many draws and half the distance moved per frame. So fast moving objects seem to move more smoothly.

Or, is there another specific reason for 60 FPS.  Just curious.

Thanks for getting back again.

I’m going to give this a go with older devices, thanks.

The gameplay is really really fast, so I’m concerned that 30 fps will look horrible :smiley:

Here’s what it looks like: https://instagram.com/p/5exe6ANgk4/

I’ll give it a go though!

Well, it seems I have two accounts for the forum. How has that happened?! :smiley:

Amazing looking effects there, Rob! Thanks for the tip on physics.setPositionIterations(). I hadn’t looked at this function before, or its counterpart physics.setVelocityIterations(). I am curious if they incur the same amount of overhead when increased. I am eager to hear back on how the change to 30fps affects the performance on older devices.

Hey, thanks :slight_smile:

30fps didn’t change much to be honest, as when I was using delta time, it would get down to about that amount anyway!

What I’m doing now is separating off every little effect and building up an index to enable/disable them based on device performance. Something I should have done a long time ago.

Regarding the physics thing, I’ve found that simply using the following works better

physics.setTimeStep(0); 

This tells the physics engine to work based on time, rather than frame based. This did cause some physics glitches at some points, so I increased the position iterations to compensate:

physics.setPositionIterations(16); 

I hope that helps

I’m glad you kept digging and found a solution that worked.  It has been a while since I used setTimeStep() and setPositionIterations(), so thanks for bringing them up, and reminding me they are part of the toolbox of solutions.

Cheers,

Ed

Game looks great, keep us posted :slight_smile:

I might have actually found how to do this. Just testing it now:

physics.setTimeStep(dt/60); -- Run every frame

This seems to be working nicely, apart from the accuracy of the physics simulation is reduced at low frame rates. I’ve added the following to compensate for that:

physics.setPositionIterations( 8\*dt ) -- Run every frame

@roballison182,

Are you trying to run at 30 FPS or 60 (check your config.lua file).  If 60, I suggest 30.  

If you’re running 60 FPS, you’re doing twice as many physics calculations.  While these are still wholly dependent on delta time and not a multiple of the frame, you will see a decline in performance, hiccups, and lag on older devices if you have lots of physics objects and a high frame rate.

This all assumes you’re running at 60FPS.

-Ed

Hi Ed, Thanks for getting back. The game is running at 60fps, but it’s crucial that it does. I understand that older devices will have a harder time, but that’s worth it if newer devices have smooth gameplay. The changes I’ve made above work really well. What it does is change what happens when there’s a performance drop: Before: Gameplay would slow down After: You see some dropped frames (basically running at a lower frame rate temporarily). But the game still runs at the same speed. Thanks, Rob

You can selectively run 60FPS on some devices and 30FPS on others.  Just make the config.lua file adjust based on system details.

https://docs.coronalabs.com/daily/api/library/system/getInfo.html

  • Am I on android,
  • Is this device running an older version of android (more likely to be an old device)
  • … more  There are are a number of details you can extract that would help you set up a targeted config.lua.

Regardless, I’d try this:

  • Take a device that is giving you trouble.
  • Remove your current fix
  • Change to 30 FPS 
  • See how it plays.  

I take it that you’ve got fast moving objects so you don’t want large skips between updates? i.e. 60FPS equals (approximately) twice as many draws and half the distance moved per frame. So fast moving objects seem to move more smoothly.

Or, is there another specific reason for 60 FPS.  Just curious.

Thanks for getting back again.

I’m going to give this a go with older devices, thanks.

The gameplay is really really fast, so I’m concerned that 30 fps will look horrible :smiley:

Here’s what it looks like: https://instagram.com/p/5exe6ANgk4/

I’ll give it a go though!

Well, it seems I have two accounts for the forum. How has that happened?! :smiley:

Amazing looking effects there, Rob! Thanks for the tip on physics.setPositionIterations(). I hadn’t looked at this function before, or its counterpart physics.setVelocityIterations(). I am curious if they incur the same amount of overhead when increased. I am eager to hear back on how the change to 30fps affects the performance on older devices.