Jump to content

Phaser physics differing between platforms


JaminJohn
 Share

Recommended Posts

I'm new to Phasor and using it to make a little game for my son with a firetruck driving around and spraying water to put out fires. When the space bar is pressed, the game creates individual water drop sprites coming from the truck with gravity turned on. I give the water drop velocity a slightly random component to make it spread out and not all follow a single parabola. There is a timed event to destroy each water drop. Here is a code snippet creating a water drop that is shooting to the right.

        drop = waterdrops.create(truck.body.position.x + TRUCK_SIZE1_X/2,
                                    truck.body.position.y - 20,
                                    'waterdrop');
        drop.frame = 0;
        drop.body.gravity.y = 300;
        drop.body.velocity.y = -100 + randn()*20 + truck.body.velocity.y/2;
        drop.body.velocity.x =  100 + randn()*20 + truck.body.velocity.x/2;
        game.time.events.add(WATERDROP_TIME, destroyDrop, this, drop);

I fiddled around with different numbers until it looked like a nice spray of water coming out and hitting the ground.

This works great when developing on my Macbook Air. However, the really weird issue is that when I run it on my Windows 7 machine the water only goes half as far before being destroyed. I tried doubling the delay time and this made it work well on windows 7 but then on the Macbook the water goes way too far. I also notice that the truck itself seems to go slower. It seems to me that if the body.velocity and body.acceleration values are scaled differently on each machine.

My first though was that maybe it has to do with screen resolution, but thinking about it further that doesn't seem to make sense.

So, why is there a difference in water drop behavior between the two platforms?

Also, is there a better way to do what I'm trying to do here? As I said, I'm new to Phasor and this is just what I came up with after reading a few tutorials and looking at other examples.

The game as it stands is hosted here -> http://drnx1m5jr5iqt.cloudfront.net/

Thanks!

Ben

 

** UPDATE

 I ran the game on another Windows 7 machine and it works okay. So the problem seems to be with my older Windows 7 Machine. Could this be an issue of client side CPU processing capability? Does Phaser adjust frame rate to deal with slower computers?

 

Edited by JaminJohn
Update
Link to comment
Share on other sites

Well, I'll answer my own question, at least partially. As far as I can tell the physics assumes that your game will run at a constant 60 fps. However, I did some testing and found that when running my game on slow the system the frame rate is closer to 30 fps. This is where the issue arises. I also notice that on both my slower and faster system the frame rate fluctuates slightly over time.

So, my solution to this issues is to create a physics scale factor which is a ratio of the ideal frame rate to the current frame rate:

function updatePhysicsSF(){
    PHYSICS_SF = 60/game.time.fps;
}

This is used to scale initial velocity of each water drop. and the square of this is used to scale the gravity setting and the truck acceleration. That seems to get things working well on both systems.

To me, this seems like a bug in Phaser. The documentation claims that body.velocity is in pixels/second, but that doesn't hold when the frame rate changes from 60 fps to something else. It would be nice if this were accounted for in the Phaser library itself, or if velocity were defined in pixels/frame or something like that. I'm still curious to hear from someone else if my understanding is flawed or if my approach is too far "off the beaten path" of phaser game development.

Ben

Link to comment
Share on other sites

Arcade Physics is non-deterministic I'm afraid and entirely locked to the frame rate. A side-effect of its original implementation in Flixel, which was carried over into Phaser and has remained in place. If you've found a solution I'd recommend strongly sticking with it (to avoid having to move to a more independant solution like p2)

Link to comment
Share on other sites

The fix I put in my game is actually very simple. I just scale the velocity & acceleration terms by the current frame rate. I'm surprised that this scaling isn't built in to Phaser or whatever underlying physics engine is being used. I suppose it would require extra computation that typical games probably don't really need.

At any rate, it would probably be helpful to others in the future to have a note about this in the documentation. It's a bit misleading that body.velocity is stated to be in pixels per second when in actuality this only holds as long as the browser is running at 60 fps.

Link to comment
Share on other sites

On 7/27/2017 at 7:54 PM, JaminJohn said:

As far as I can tell the physics assumes that your game will run at a constant 60 fps. However, I did some testing and found that when running my game on slow the system the frame rate is closer to 30 fps. This is where the issue arises. I also notice that on both my slower and faster system the frame rate fluctuates slightly over time.

You can set game.forceSingleUpdate=false. See, e.g., the phaser-plugin-advanced-timing demo.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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