Jump to content

What are physical equations used by cannon.js and how to map them onto a Babylon code


kavsnzre
 Share

Recommended Posts

Hello everyone!

I'm trying to understand how to use physics engine in Babylon. I understood the basical instructions and all about this is clear.
But I've a serious problem that I'll explain with an example.

I've a cube that falls onto a plane having a fixed angular velocity, here is the playground: http://www.babylonjs-playground.com/#UQNWL

Obviously:

  • if I set the frictions with very large values then the box will very fastly loose its angular velocty when it'll fall onto the plane
  • if I set both the frictions values equals to 0 then the box will never stop and will rotate onto the plane forever...

But...what if I want that the cube will stop in 5.76746 seconds? In this case, which values I have to put into the "friction" variables? Maybe I've the answer:

  1. I have to know the physical equations used by cannon.js,
  2. then I've to solve my physics  exercise,
  3. and then I've to put the correct values into the "friction" variables.

If I'm right, where can I find the EXACT equations used by cannon.js? Is there a sort of manual? Or I've to deduce them from the code?
Is there another way to solve the problem? 

Thank you so much!
Best regards.

Link to comment
Share on other sites

Welcome kavsnzre as a non expert in cannon physics I can suggest only trial and error but am sure the experts will give you a better answer. To help the experts answer your question would you put the relevant parts of your code into a playground. This way the lovely people on the forum can make changes and suggestions and be even more helpful.

Link to comment
Share on other sites

Hi @kavsnzre, welcome to the forum.

   Here's some links for the third party Cannon.js physics engine.  Hope this helps.

https://github.com/schteppe/cannon.js/tree/master/src/equations

http://schteppe.github.io/cannon.js/docs/classes/FrictionEquation.html

You might see a method in mainline Cannon.js code:  this.updateFriction(timeStep); and it is likely related to

https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/babylon.physicsEngine.js#L99

BJS interfaces with the third party CannonJS engine... via 3-4 various objects.  They can all be seen here...

https://github.com/BabylonJS/Babylon.js/tree/master/src/Physics

And I imagine the timestep is related to frames per second and thus affected by platform performance, but that's way over my head.   Just for fun, I made another playground where spin lasts about 5 secs (trial and error method as @JohnK mentioned).  Your spin duration may vary.  :)

@schteppe DOES have a forum membership here, but it is unclear how often he visits.  Good luck, keep us posted.

Link to comment
Share on other sites

Thanks for the mention @Wingnut! I don't hang out here every day but sometimes it happens ;)

@kavsnzre you can read about the equations in this PDF. http://www8.cs.umu.se/kurser/5DV058/VT15/lectures/SPOOKlabnotes.pdf

I hate to break it to you, but the equations used in game physics are not exact. They usually use iterative solvers that can solve the equations more and more precise depending on how much CPU power you are willing to pay. Also, the equation are only solvable "in one direction", you can't really input a time value and expect the engine to give you the friction constant. It's optimized to only output game object transforms.

What you CAN do is run the simulation many times until you find a value for the friction that matches your time value.

I hope this helps :)

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