Jump to content

Make physics time = real time?


Recommended Posts

I create a multiplayer game, where i expect that when a body has a velocity of  100px/s, and it moves for 3 (realtime) seconds, that it will have moved ~300px. 


With arcade and P2 physics, if there is any lag (framerate is not 60fps?), the above is not guaranteed. E.g. after 3 (realtime) seconds, the object only moved ~250px (in some situations). 


Is it possible to tell the physics system to "catch up", e.g. to do a physicsUpdate(timeSinceLastFrame), instead of physicsUpdate(1/60)?


Link to comment
Share on other sites

Thanks for your answer  :-). I also found this option, but, according to: https://github.com/photonstorm/phaser/blob/v2.4.4/src/physics/p2/World.js#L787

This options does:


 if (this.useElapsedTime)




And physicsElapsedhttp://phaser.io/docs/2.4.4/Phaser.Time.html#physicsElapsed


The physics update delta, in fractional seconds.
This should be used as an applicable multiplier by all logic update steps (eg. preUpdate/postUpdate/update) to ensure consistent game timing. Game/logic timing can drift from real-world time if the system is unable to consistently maintain the desired FPS.



So imho it does not really fix my problem (?).




I could patch phaser source like this, but i dont know if it catastrophically breaks stuff?

 if (this.useRealTime)



Edit: Niceness

Link to comment
Share on other sites


Yeah, patching it to use game.time.time makes the objects behave much like expected (traveling 600px or 603px in 3 seconds, and not like 540 or 583, with velocity=200). At least in my simple test program.


Just using "this.game.physics.p2.useElapsedTime = true;" does not do the trick i'm afraid.

Link to comment
Share on other sites

My example: http://phaser.io/sandbox/edit/RZZJqOIl

- Press enter to move spaceship for 3 seconds. Output in JS console

- Or: Move with keys, collision detection still works


ATM with chrome+ff i get reliable x=713, instead of x=800. 


Its possible to manually update x/y of the object, but ofc its kind of a joke to have a library like Phaser, and then manually update the position of its objects. 

Link to comment
Share on other sites


Both in Arcade and P2 it's possible to manually move the body each update with time.elapsed (not using velocity, but doing body.x += distance). time.elapsed is a wall-clock time, therefore the bodies really move 100px in a wall-clock second. 


Attention: In Arcade, the collision detection has to be performed AFTER the movement of the body, in the update() function. The other way round does not work. e.g:


update() {
    if (leftKeyPress) {
sprite.body.x += movePixelPersecond * game.time.timeElapsed / 1000;
// this HAS to be after sprite.body movement. 
game.physics.arcade.collide(sprite, otherstuff);

Other remarks:

Using P2 in Phaser just to perform collision detection with sensors seems to be fsckd. Sensors seems to not really work. 

Also: move everything with sprite.body.x, not sprite.x
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...