Sign in to follow this  
Gan_HOPE326

Time integration method in P2 physics

Recommended Posts

Hi all,

 

I've just experienced some trouble with P2 physics. My game uses Newtonian gravity, which requires pretty good integration, and I get some huge errors by just using basic P2 methods (applyForce etc.). My guess is, this is an integration problem, since Newtonian gravity is notoriously hard to integrate numerically. I was considering some fixes, but does anyone know first what integration method does P2 use for its physics step, and if it is possible to change it to a more expensive and precise one? For example, could I do a leapfrog or a Runge-Kutta without editing the source code? I don't know where to find these methods either, I've tried searching the source code but didn't find it. Thanks!

Share this post


Link to post
Share on other sites

The P2 Physics in the phaser documentation is only a wrapper for the p2.js library. Not all its properties are exposed in the wrapper. For example, to access the real p2js body in a phaser body, you need to access its data property.

Here is the p2.js library, with all the information you need: https://github.com/schteppe/p2.js
 

--

P.D.
Sorry. Edit.

Also, maybe you are expecting applyForce to work l ike applyImpulse in Box2D? It happened to me. applyForce just adds to the forces applied over time; it factors dt in. There is no applyImpulse, but you could just multiply the force with your expected dt.

Share this post


Link to post
Share on other sites

The P2 Physics in the phaser documentation is only a wrapper for the p2.js library. Not all its properties are exposed in the wrapper. For example, to access the real p2js body in a phaser body, you need to access its data property.

Here is the p2.js library, with all the information you need: https://github.com/schteppe/p2.js

 

--

P.D.

Sorry. Edit.

Also, maybe you are expecting applyForce to work l ike applyImpulse in Box2D? It happened to me. applyForce just adds to the forces applied over time; it factors dt in. There is no applyImpulse, but you could just multiply the force with your expected dt.

 

 

Thanks!

No, I wasn't expecting applyForce to work like that, I never used Box2D anyway. In fact, I'm a physicist, so I would have never mixed the two  :D . What happens is that basically I have my little starship trying to orbit around a planet with gravity going like 1/r^2, but the energy isn't conserved properly and the rocket spirals down to its doom without even completing one loop. I know numerical imperfections are unavoidable, but didn't expect them to be THIS bad. So I was considering making the position a kinematic variable and integrating it on my own, but since that requires more work I was wondering whether it could be avoided. Checking P2.js though it looks like the integration is carried out using the "naive" Euler method (v(t+dt) = v(t) + a(t)*dt; x(t+dt) = x(t) + v(t+dt)*dt;) and there aren't any options for slightly more expensive but more precise methods like Runge-Kutta. So it looks like I'm on my own for this one (either that, or I make an addition to P2 myself...).

EDIT:

Alright, I found out the issue... it was something really stupid, in fact. I didn't know it, but the body.damping parameter was set to 0.1 by default, thus leading to the body slowly losing kinetic energy... I didn't realize it when it was just moving in vacuum, but as soon as planets were added it became dramatically relevant. Just setting body.damping = 0 solved everything!

Share this post


Link to post
Share on other sites

: ) You should do that. I often find that physics written on javascript aren't usually very stable, I supose it is because javascript is kind of heavy. Asm is doing great things on Firefox, though. I hope you do make the contribution to P2.js, it would be really cool.

 

The best javascript written physics library I know is the one that comes with Turbulenz, but Turbulenz is so complicated and I am to lazy that I mostly leave everything related to it alone.

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.