Jump to content

Updating the world according to time with p2.js


LordofLolcats
 Share

Recommended Posts

Hello everyone,

 

It's my first topic in that forum and I'm here today because I've an (logicial ?) issue with the physics librairy p2.js.

I'll try to explain my problem in the simplest way possible.

 

I'm working on a game based on my own framework coupled with p2.js. In that game, the physical world needs to be updated at a constant time. For now, I'm using a requestAnimationFrame to call the world.step function but that means if the player have a slower PC, the game will be slower and the opposite is also true.

 

I've made an animation in Photoshop (not perfect but whatever) to show you what I exactly want:

52eac16db4.gif

 

So, I've two objects that are supposed to move at the same speed. Let's assume that the red line is the timeline. With the first object, we see when we have a little freeze, he stop then continue with the same speed where he stopped. But when you look at the second one, after the freeze occurred the object goes to the correct position following the timeline.

 

My question is, can we do that with p2.js ? Or it's not about p2.js and I'm missing something ?

I did some tests based on the documentation with the others arguments for the function world.step() but doesn't seems to work at all.

 

Hope you can help me with this problem and thanks for your answers !

Have a great day :)

Link to comment
Share on other sites

Hi,

 

If you're making a simple scene with two dynamic or kinematic bodies moving with their velocity, p2 is supposed to fix that.

 

If you pass the parameters to world.step() like this, your game should be frame rate independent (code from the character demo):

var timeStep = 1 / 60, maxSubSteps = 10, lastTime;function animate(t){    requestAnimationFrame(animate);    var dt = t !== undefined && lastTime !== undefined ? t / 1000 - lastTime : 0;    world.step(timeStep, dt, maxSubSteps);    lastTime = t / 1000;}

If you expect huge lags, you probably want to increase maxSubSteps.

 

Read more here: https://github.com/schteppe/p2.js/wiki#user-content-stepping-the-world

 

If this does not fix it, then maybe your setup is a bit more complex and needs some special code. How do the players control their corresponding physics body?

Link to comment
Share on other sites

Hi,

 

If you're making a simple scene with two dynamic or kinematic bodies moving with their velocity, p2 is supposed to fix that.

 

If you pass the parameters to world.step() like this, your game should be frame rate independent (code from the character demo):

var timeStep = 1 / 60, maxSubSteps = 10, lastTime;function animate(t){    requestAnimationFrame(animate);    var dt = t !== undefined && lastTime !== undefined ? t / 1000 - lastTime : 0;    world.step(timeStep, dt, maxSubSteps);    lastTime = t / 1000;}

If you expect huge lags, you probably want to increase maxSubSteps.

 

Read more here: https://github.com/schteppe/p2.js/wiki#user-content-stepping-the-world

 

If this does not fix it, then maybe your setup is a bit more complex and needs some special code. How do the players control their corresponding physics body?

 

The players doesn't control the bodies. That actually solves my problem, again thank you for your precious help.

This doc ( http://schteppe.github.io/p2.js/docs/classes/World.html#method_step ) about world.step is quickly documented but doesn't offer all those details that help us to understand.

 

Have a nice day.

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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