Jump to content

Understanding physics in Babylonjs


friz42
 Share

Recommended Posts

Hi!

I am new to Babylonjs. I wanted to learn more how it handles physics. I've found great demo: https://www.babylonjs.com/demos/physics/ , but I could not find any code that what do something like that. I actually wanted to make springs similar to this https://github.com/bit101/CodingMath/tree/master/episode16 but in 3D. Is there any opensource code that shows dynamic physics in babylonjs? Otherwise what materials would you suggest to study to understand how physics works in babylonjs and what are limitations.

Thanks!

P.S. I know about this lesson https://doc.babylonjs.com/overviews/using_the_physics_engine

Link to comment
Share on other sites

Hi @friz42,

welcome!

That's a great question. The overview you found in the doc is really just an overview. One of my assigns github issue is to make the physics documentation great again, and you really made a good point in convincing me this is very much needed.

The best person to ask for demos is @Wingnut, which is an amazing list of wonderful demos from many different people. I hope he will paste a few playgrounds soon, so you can get to know the physics engine a bit more with code examples.

I would recommend to start with the simple playgrounds in the overview, and take it from there. You can also search for playgrounds on the documentation website.

The engines we are using, cannon.js (https://github.com/schteppe/cannon.js) and oimo.js (https://github.com/lo-th/Oimo.js/) have native demos of what's possible with the engines themselves. Anything you can do there is also possible with babylon (and hopefully a bit easier as well). 

Link to comment
Share on other sites

Oh yeah, and THANKS for choosing ME!  heh

Ahhh Raanan... throwing a little poop back-at me, huh?  :D

Welcome Friz 42! 

But really, @Raggar is physics-engine hot hot hot!  He dives into "native" layers and just hacks and whacks like a mad man.  Brutal!

Reading his code is almost as tough as reading @NasimiAsl shaderBuilder code!  GRUESOME.  @Pryme8 is a hot physics engine coder, too... I've seen him do some "holy crappers".

We all know, though, that these physics engines are not for children.  It's best to have full coverage insurance for the entire neighborhood, hard hats, safety shields, leather gloves, and medical teams standing-by.

Sometimes, it just takes experiment after experiment... to get a "feel" for physics engines and impostor usage.  Start with impulsing (or setLinearVelocity) on some simple shapes, learn your friction, mass, restitution params.  Try some gravity and ramps, etc.

Oh yeah, springs.  Someone was playing with springs... here.   Someone was DRAWING springs here... but not using any physics.  MY recent "springs" project is a puppy-level (beginner) landing gear for a Lunar Lander-like space craft.  Pretty bad coding on my part.  :)  X, Y, Z keys... and shifted X Y Z keys... active for impulsing the green box.

I'm not very good-at "springJoints", but they are pretty fun.  Requires patience, like all learned things.   (also, I see some inconsistencies when that "lander" demo is reRUN and reLOADED.  weird.)

In my lander playground, ONE end of the TWO springs on the lander-legs... are 'anchored'  to points that are hovering in space.  There is no mesh hooked to one end of the springs.  They are invisibly located "above" the two front (boundingBoxed) lander legs. 

Only two springs are in the scene.  The back two legs have no springs, but they are connected to the front two legs... via two lockJoints.  (like a cross-member between the front and back lander-legs).  Thus, the "back" legs use the same two springs as the front legs.

Way too much info, huh?  AND, it sucks... not being able to "see" physics impostors and physics joints, right? 

We (I) have been thinking about a "show the physics" layer, using the new Babylon GUI system... but... it's not done.  When first done... it would simply put name labels at various places in the scene... to show where the impostors and joints are located (as best it can).  It is somewhat difficult to do "visualization" of physics formulas.  I wish I had a magical idea... for that.  Be well.  We're here with aspirin... for your experimenting pain.  :)

Link to comment
Share on other sites

with physics, break it till you make it! ^_^

Just do tons of experiments to test out a bunch of ideas, make dumb gadgets and rig ups that have no purpose other then for you to mess with it.

Also google is your best friend, look up real physic algorithms and try to understand them and how their systems would translate to BJS.  
Its kinda a long road to start down, but follow the examples you can find and just keep hacking.

Link to comment
Share on other sites

Thank you for links. Most of them I already found by searching "spring" in playgrounds. I see that springjoints are working, but I am not sure how physical are they. I've searched for documentation for cannon.js to understand how springjoints work, but I've found only list of classes used http://schteppe.github.io/cannon.js/docs/classes/Spring.html. Does anyone know where I can get a bit more verbose information about cannon.js implementations?

Link to comment
Share on other sites

https://github.com/BabylonJS/Documentation/issues/454

It's on the BJS "todo" list.  (joints)

And parenting is also there, which is OUR way of creating compound impostors.

(I think we are supposed to add impostors to the children FIRST, and add impostor to parent, last.)  :)

We were sort-of hoping that Friz42 would write the verbose Joints Tutorial.  ;)

*shrug*  Cannon springs are both compression and extension... in the same object (pushers or pullers).  At deep levels, they are formulas, of course.  You can examine them easily with the built-into-browser object inspector.

There appears to be no "spring-loaded hingeJoint"... built-into CannonJS.  I went looking, a bit.  We once talked about a motor-control algorithm that would make a hingeJoint ACT like it was spring-loaded.  But, everyone got scared of that idea, and we all took a nap, instead.

Do ya think the swinging doors on the saloon in the cowboy movies... had springs?  I bet not.  I bet each door raised height a tiny bit when it was swung-open, and then gravity did the work to close the door, via some kind of hinge "cup" thing.   hmm.  Why am I still talking?

At the BJS plugin layer... https://github.com/BabylonJS/Babylon.js/blob/master/src/Physics/babylon.physicsJoint.ts#L188

Three set-ables... length, stiffness, damping, along with mainPivot and connectedPivot.  They're reasonably easy to understand.  They "hand-off" their values to "native" properties within the actual 3rd party engine.  You can also "talk native" directly... in 2 ways.  One way is done with a feature on our plugins, and the other is simply "going-around the plugin". 

"Talking native" means getting/setting values much deeper into the physics engine... than would normally be allowed at the BJS physics plugin layer/api.

But, I bet YOU want "friendlier" and with illustrations.  :)   Yep, me too.  Work work work.  Raanan's physics car document is pretty decent, but not much info about springs.

Old Info:  Earlier, I lied to everyone... when I said that one end of my springs were not attached-to any mesh.  They actually are, but their mainPivot is offset SO FAR... that it IS truly hovering in space.   A joint must have an impostor at each end.  I just wanted to clarify and self-spank for being a dirty rotten liar.  :)  Also, MANY joints can share an impostor.  The front legs of the lander have springJoints and lockJoints.  When done right, they don't fight. (I'm a poet!)   But, a "battle of the springs" would be a fun drinking/betting game, huh?

Link to comment
Share on other sites

41 minutes ago, d13 said:

I'm curious: which is best to use Cannon.js or Oimo.js?

Hi! If you haven't read this post:

 I would suggest you to read it.

Or in few words I can retell you what I understood from it: Cannon.js is more realistic but far from perfect, Oimo.js is faster and better for big scenes with many interacting objects, Energy.js can be fast and realistic, but it is very raw right now. There are other engines as well and you can try them if you have a spirit of pioneer. (should this be added to "using_the_physics_engine" tutorial?)

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