Jump to content

Lag causing physics bugs


Recommended Posts

So for a while I've been encountering random physics bugs in my game where spites clip through each other. These bugs are occasionally game breaking. I finally realized that it's happening because lag is causing long delta times, so the physics updates in large chunks but my update method isn't called fast enough to handle collision of all the bodies.


Since lag in my game is a pretty large problem that needs to be solved in and of itself, for now I think I need some workaround to stop the physics from updating when the delta time is larger than a certain value, to allow me to release my game on time (it needs to be done by Monday) without game breaking bugs and fix the lag later.


Thanks for any help!

Link to comment
Share on other sites

Try if the property game.time#deltaCap helps in this case. This is exactly what you're searching for. For me it prevented the worst behaviour with p2 constraints, but the game remained unplayable, as many collisions were not found at a fps rate of 40-50.


I wonder how you could use the physics engine at a frame rate as low as 30fps. At the moment it seems impossible to me. The time steps at 30fps (0.0333) are double in size than at 60fps (0,01667). To compensate for this you would need to increase the internal step resolution of p2 which itself would reduce the fps rate again. This is kind of regression I do not know how to fix.


Anyway. Try if deltaCap helps you. And keep in mind: Disable your physics debug rendering of your fixtures/shapes when you're testing performance. To draw debug information is heavy costly and can rob you dozens of fps! Especially on mobile.


Regards George

Link to comment
Share on other sites

The documentation says of deltaCap: "If you need to cap the delta timer, set the value here. For 60fps the delta should be 0.016, so try variances just above this."

This seems misleading because 0.016 is in seconds and all other time measurements I've used were milliseconds. Which one is deltaCap? I gave it milliseconds so that could explain the problem. I'll run more tests

Link to comment
Share on other sites

It's measured in seconds like the frame rate is given in the unit frames per seconds. Just use the form 1/60, 1/30 and so on. Lower than 1/60 makes no sense. As my frame rate was as low as 40 fps on some devices I played with these higher limits as my deltaCap: 1/30, 1/40. It helped and prevented the worst glitches especially with joints- they just disappear with a time step that is too high.


What are your fps measurements? What the glitches? My project was cancelled due to the bad performance and all physics glitches on lower frame rates. It's now going to be implemented as an app :( My game only runs on the very latest devices (60fps iphone 5, 55fps on galaxy s4, compare: 19fps on iphone4)  and if that's not enough I got a fps drop of more than 25fps in non stock browser on iOS (Safari is stock, chrome/firefox uses the slower webkit component). On Android I had to to force to use canvas instead of webgl (thankfully I read this somewhen here). With webgl I had frame rates of 5fps on some browsers where the canvas rendered game had 30fps.


Maybe you have some possibilities left to fix your performance in order to fix your glitches ? You're release is planned for today? I really wish you good luck with this!

Link to comment
Share on other sites

So today may be my last chance to fix this, yes. Thanks for your help and concern! The problem is that what might otherwise be a playable amount of lag (for the patient) can make the player fall through the ground or whatever they're standing on, causing instant game over. This is totally cause for a rage quit. This is pretty much the only physics bug I couldn't fix, even with deltaCap.

My game is using AUTO to pick webgl/canvas, so should I change that?

Link to comment
Share on other sites

Hou have two options left:

1. Maybe you ask schteppe the owner of p2 how to fix physics for lower frame rates. https://github.com/schteppe/p2.js

Open an issue on p2's github repository to do so.


2. Try to increase the overall performance of your game.


For option 2:

Do you have problems on mobile or desktop? What are your target platforms?

I only mentioned the canvas/webgl performance difference in case you experience problems on mobile.

If you have problems on desktop you can be sure that your game won't run on mobil at all.


To check your overall performance look at the frame rate. This is such a crucial value and you should have done it already during development. Your target value is 60fps. This is the target rate of any web browser.


To check it:

//init once in your boot screen for example. this enable the advanced performance monitoringgame.time.advancedTiming = true//draw your framerate via render() in the top left cornergame.debug.text(game.time.fps, 0, 0 )

To check which renderer is in use just look in the console at the phaser header with the version of phaser or output it via 

var renderer = game.renderType === Phaser.WEBGL ? 'webgl' ? 'canvas'game.debug.text(renderer", 0, 20 );

Steps to debug:

A: First of all check your current framerate to have a base value for everything you do after.

B: Force Canvas as a renderer. Maybe this fixes something in your case. I do not know where you are testing on

C: Try to remove everything related to the physics, but keep the game running and producing sprites. If you can't do this try to remove parts of the physics at least. Look at the fps. Any changes?

D: Remove entire parts of your game and find the most costly parts by looking at your fps.

E: You could use the profiler of your browser. But this is a larger and separate topic.

F: Do you use spritesheets? If not: do so! They increase performance due to the fact how a gpu is working. If you already use them check their sizes. Do not put large backgrounds in a spritesheet. That will kill your performance and make it worse that using no spritesheets at all. Keep your sprite sheets at sizes of 512 or 1024. Do not go bigger. Use multiple spritesheets instead.




Link to comment
Share on other sites

Thanks for such a fast reply! I should have clarified that my game actually uses Arcade physics. It runs at 60 fps on my laptop in chrome, but on Safari on a Mac laptop I get worse performance and that's when the bug happens. I haven't noticed particularly bad performance on mobile chrome with my android. Right now I'm testing with internet explorer.

Also, putting deltaCap to a test value of 0.001 proved that it has no effect on Arcade physics, as my problem still occurs

Link to comment
Share on other sites

deltaCap is used in an arcade body to limit the velocity change, so it should have some effect. I have no experience with the arcade engine and couldn't find any apparent bugs in the arcade source with deltaCap. Maybe it's simply not what you're looking for. Try to get the graphics performance optimised as this is more promising. Good luck with your submission.

Link to comment
Share on other sites

Alright, I opened an issue on the repo and Rich helped me find the problem by proving it was on my end. Here is the solution I found:

"My version must have been outdated as I opened phaser.js and found deltaCap was not being applied to physicsElapsed. I updated the code to match the current source and the problem was solved."

Thank you for your help! I was able to get the fix done in time for the contest and will be putting up a game spotlight shortly.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...