LordofLolcats Posted July 26, 2015 Share Posted July 26, 2015 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: 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 Quote Link to comment Share on other sites More sharing options...
schteppe Posted July 27, 2015 Share Posted July 27, 2015 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? LordofLolcats 1 Quote Link to comment Share on other sites More sharing options...
LordofLolcats Posted July 27, 2015 Author Share Posted July 27, 2015 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.