Jump to content

Make physics time = real time?


nobody
 Share

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)
        {
            this.world.step(this.game.time.physicsElapsed);
        }

 

 

 

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)
        {
            this.world.step(this.game.time.time);
        }

 

 

Edit: Niceness

Link to comment
Share on other sites

Update:

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

Fixes:

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

 Share

  • Recently Browsing   0 members

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