RaananW

The new Physics Engine architecture

Recommended Posts

So!

I don't know how many of you have noticed the commit from yesterday, but Babylon's physics engine has a new architecture.

There is now a physics impostor class that will be your one-stop-shop for all physics needs. Applying impulse, force, changing velocity, everything is there. There is also a way to register callbacks to be called before and after the physics step and when the mesh collides against other meshes.

Those API might change a bit until 2.4 is officially out, but get used to the new architecture!

What's new? A lot!

A short list (before I finish writing the full documentation and tutorials):

  • Dedicated PhysicsImpostor for each mesh, dedicated PhysicsJoint for each joint/constraint
  • New Joint-System - Is is now possible to add many kinds of links (depends on the underlying physics plugin) - A Distance Joint, A Hinge, a Ball-And-Socket and more. Documentation is being written :) But just to get people interested - @Wingnut, you might find this a lot of fun - http://www.babylonjs-playground.com/#1ND6TH 
  • Physics plugin is mainly transparent - changing from cannon to oimo (and soon to the new energy engine) should simply work. Try it in the last playground example. 
  • Having said that - there is a way to execute native cannon/oimo code in a very simple way
  • New callback mechanism for before and after the physics step AND when an impostor collides against a specific impostor.
  • Two-Way transformation update - No need to execute updatePhysicsBody[Position]() - physics rotation and position is automatically updated.
  • A way to recreate the body, in case parameters like the scale has changed.
  • Physics Plugins rebuilt with performance adjustments. 
  • Set Velocity
  • Apply Force
  • And many many other little things

For those who want to see what was done under the hood, the PR is here - https://github.com/BabylonJS/Babylon.js/pull/983

Fell free to ask questions, and please wait patiently for the documentation that will arrive soon. (Or "sooner or later").

There are a lot of things on my TODO list to add to this engine (Spring objects, Physics-Particle-System, further joints and constraints and more), but this is step 1. And it is finally done :)

Enjoy physics!

 

Share this post


Link to post
Share on other sites

Thanks Raanan! 

Man, is it fast (with Cannon).  Much slower with Oimo, for me.  Did anyone else test the demo with Oimo?  Please report, thx.

Raanan, will you be attempting backwards-compat?

(Wingy notices a tiny water puddle under a frozen API)  :)  Global warming?

Share this post


Link to post
Share on other sites

@Wingnut - I did my best to keep everything backwards-compatible. If something is not working i would love to know in order to fix it.

Oimo was not changed, Cannon didn't either. The only thing that changed was the Babylon architecture. A bit of performance issues were fixed, but nothing that will really be noticeable on a normal computer. 

Having said that - My new favorite is Cannon.js for implementing complex physics scenes. The newest version is wonderful. Oimo's newest version is not working correctly for some reason. When I find out why I will upgrade to the newest Oimo as well. 

 

Share this post


Link to post
Share on other sites
On 2/23/2016 at 1:13 AM, RaananW said:

and more. Documentation is being written :) But just to get people interested - @Wingnut, you might find this a lot of fun - http://www.babylonjs-playground.com/#1ND6TH

This playground seems to not work, I get an error of n is not a function. I would really like to see a working example of this so I can do a fully deployed vehicle demo, which I will include comments on for others to be able to do the same.

Share this post


Link to post
Share on other sites

Am I mistaken but can I dig through the methods available in my physics plugin by console.log(new BABYLON.CannonJSPlugin()) or Oimo depending on which I wanna dig through and checking what that objects prototypes are and locating what ever methods I need for rigging a vehicle?

Ive never done this before but I was just thinking that this might work, then I could stop bugging you guys for methods.

Share this post


Link to post
Share on other sites

I think it would be better to simply look at the code :)

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

But it is a bit more complex than this - you will have to understand the PhysicsImpostor class and the PhysicsJoint class. I take the blame, but I am working on it ;) 

 

Share this post


Link to post
Share on other sites

Wow I'm a dummy... Yea this is the stuff I needed to look at the whole time.  I will be able to make it happen now thank you.


**EDIT**
http://www.babylonjs-playground.com/#TX01V#0

Im about to add controls and joints but I think I am going in the right direction?

*** ANOTHER EDIT ***
So I am trying to connect one tire at this point could someone help me figure out whats going on here?  Im not dropping any errors, but adding the joint effectively does nothing?
http://www.babylonjs-playground.com/#TX01V#4

Share this post


Link to post
Share on other sites

Is the documentation of the new architecture of the phisics available?

I saw that on the mesh class had access to getPhysicMass() but no setPhysicsMass() function. But observing the new architecture, I saw that there was a setter getPhysicsImpostor().setParam("mass", 0) for changing the mass for example. I almost add the missing functions in the class AbstracMesh but this is not helpful with the new architecture. But the documentation would be cool  and seParam is cool too. :D

 

Share this post


Link to post
Share on other sites

so!

Basic usage (without getting too much into more advanced features) is documented here - http://doc.babylonjs.com/overviews/Using_The_Physics_Engine

To your question - physics and mesh are now totally separated. The old functions are there for legacy reasons, but from now on changing the physics mass, friction, or anything else will be done in the impostor.

I will document the more advanced features (like real-time mass changing and its consequences) in the next tutorial. 

I am planning at least two more tutorials - advanced usage and deeper understanding of joints. This might take some time, let me know if you have any questions to the basic usage. And the code is always there :)

Share this post


Link to post
Share on other sites

Very nice documentation on physical Raanan. is well detailed, well explained ... if documentation of advanced physics is like that, it's really great. The physics of Babylon really starts to please me. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.