mariogarranz

Physics timestep?

Recommended Posts

Hi there. First of all, I would like to thank Rich and the people of the Phaser team for this great framework. I just finished my first game with it and it was really helpful.

 

One thing that I noticed while coding the game was that having my game character jump, giving him some acceleration on the Y axis, would cause different jump heights depending on the speed of the computer running the game. I guess this is because the physics are not calculated based on the timestep between each call to the update() loop, having more powerful computers to apply gravity faster than less powerful ones.

Am I right about this? or maybe I am doing something wrong? Is there any way to fix this, other than implementing my own physics?

Thanks in advance for your help :)

Share this post


Link to post
Share on other sites

The method I used was:

 

1) Add some gravity to the player sprite body:

player.body.gravity.y = PLAYER_GRAVITY;

2) On the update() loop, if the up key is pressed, add some speed to the player on the Y axis:

 

if(game.input.keyboard.isDown(keys.UP)){  if(!jump){    player.velocity.y = -JUMP_ACCEL;    jump = true;    ....  }}
 
So I'm just letting the physics engine do the maths on the gravity. Is that wrong?
 
It is just a simple mini game that I came up with just to learn Phaser. I started with Jesse Freeman's Phaser tutorial, but since the code was mostly outdated and it was too simple, I ended with a complete mini game trying Phaser examples and reading some info on this forum.
 
You can try it here:
 
 
Thank you very much for your help.

Share this post


Link to post
Share on other sites

Thanks Jesse, but to be honest the new sprites suck compared to the swift animation of the player you had  ^_^ 
 

 

For problems with gravity you mentioned I recommend this article:

http://www.niksula.hut.fi/~hkankaan/Homepages/gravity.html

 

That's quite an interesting article. I was looking at this one, quite similar, before:

http://gafferongames.com/game-physics/fix-your-timestep/

 

 

That's why I was asking if I should code the jump physics myself, or if I was just doing it wrong and there was another way so that just adding gravity would work at the same speed on every device.
 

Share this post


Link to post
Share on other sites

Yeah that http://gafferongames.com/ article is amazing, it revolutionised my understanding of how to handle physics time steps.

Prior to reading it I was experiencing exactly the same problem you described, whereby the player would jump differently on different devices.

Once I implemented the 'time accumulator' system from the article the jump worked identically across the various different targets I tested on.

Share this post


Link to post
Share on other sites

0.95 used a subdivided timestep, but it caused such jerky/irregular movement on mobile I removed it for 1.0.

 

Looking at the code though I realise we do use an integrated velocity (you can see this in updateMotion) but for some reason the integration was never completed, so I've added that in to 1.0.7-dev just now :)

Share this post


Link to post
Share on other sites

I'm tempted to try out an RK4 based timestep (which is what the gafferongames approach uses) but will wait for 1.0.8 to go into dev first. In my experience it's only really helped on desktop and generally caused issues on mobile (where frame rates are already really low), but I'll definitely give it another shot.

Share this post


Link to post
Share on other sites

Hi, I am facing a problem here.

update and render function only work if the browser tab is selected, if I navigate my mouse to other tab or other program, update and render will stop, but the timer will keep going. 

I am doing a game which need update and render function to keep  going if I move my cursor out of that page. Is there any solution?

Share this post


Link to post
Share on other sites

Hi, I am facing a problem here.

update and render function only work if the browser tab is selected, if I navigate my mouse to other tab or other program, update and render will stop, but the timer will keep going. 

I am doing a game which need update and render function to keep  going if I move my cursor out of that page. Is there any solution?

 

Hi claire.

Actually this question is not related to the original question on this thread. I guess it would be a better idea to open a new thread to talk about this so that if someone finds the same problem he or she may be able to find the solution easier.

 

Anyway, since there is no complete documentation yet I'm not sure of the way pause works, but if you check the Stage class code, you will see this:

 

visibilityChange: function (event) {        if (this.disableVisibilityChange)        {            return;        }        if (event.type == 'pagehide' || event.type == 'blur' || document['hidden'] == true || document['webkitHidden'] == true)        {       // console.log('visibilityChange - hidden', event);       this.game.paused = true;        }        else        {       // console.log('visibilityChange - shown', event);       this.game.paused = false;        }    },

So, it seems like there is a boolean property of the game stage called "disableVisibilityChange" that, if set to true, ignores tab change events on your browser.

 

My guess is this should solve your problem:

game.stage.disableVisibilityChange = true;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.