Jump to content

Why physics mass and restitution now work in cannon.js ?


Recommended Posts

I use 



cannon version 0.6.2 (for physics and SphereImpostor

I need cannon because it implements mesh impostor.

I have ball and set it this values

playBall.setPhysicsState( { impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: 1, restitution: 0 } );

Can anybody tell me if there are problems with mass and restitution, because if I change value there is no diffrenece !


If I use oimo physics then there is difference if I change mass and restitution, but I need mesh impostor for maze. So I can use oimo.

Can anybody thell me if there is bug in cannon for mass and restituion ?

Or in cannon.js is not implemented mass and resitution?

Link to comment
Share on other sites

That's the NEW stuff, P8.  Now get back to troubleshooting where the backwards-compat bug is, eh? 

Hurry up, we haven't got all day!  :)  Yes, Ian's ball is bouncing again, but we need to check on Raanan to make sure he doesn't need an ambulance.  :)

The first person to discover WHY setPhysicsState version is causing bad sphere bounce... wins 13 cents and my left sock!  WOW!  Ready?  GO!

Link to comment
Share on other sites

challenge accepted

changing any of the settings has an effect now.

this honestly handles the exacts same way and is in fact calling the same method if im not mistaken.
you would still access the imposter thorough the object.pBody  if you need to change anything or access any imposter local data.


^^ the semi-never ending bouncing ball

Link to comment
Share on other sites

6 minutes ago, Wingnut said:

hmm, well done.  You win.  So why isn't THIS thing working?  http://www.babylonjs-playground.com/#BEFOO#5

I must have a mistake in there somewhere.   hmm.

Ohhh... duh Wingy... box imposter on the sphere.  Brilliant.  :)

I have done the exacts opposite before, I had a sphere on a box and I could not for the life of me understand why it behaved the way it did lol

Link to comment
Share on other sites




ground.setPhysicsState( BABYLON.PhysicsEngine.MeshImpostor, { mass: 0, restitution: 0 });
ball.settPhysicsState( { impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: 2, restitution: 0 } );



perplexus.pBody = new BABYLON.PhysicsImpostor(perplexus, BABYLON.PhysicsImpostor.MeshImpostor, { mass: 0, friction: 0 ,restitution: 0 ,move: false }, scene);
ball.pBody = new BABYLON.PhysicsImpostor(ball, BABYLON.PhysicsImpostor.SphereImpostor, { mass: 1, friction: 0.5 ,restitution: 0.5 ,move: true }, scene);

I think that mass on ball is same no matter if I change ball mass to 1 or 5 or 15. Ball always behaves the same way.

And you forget set gravity 10 or 5 or something.  I have gravity set:

scene.enablePhysics(new BABYLON.Vector3(0, -5, 0), new BABYLON.CannonJSPlugin() );

scene.gravity = new BABYLON.Vector3(0, -5, 0);

ok gravity works OK, and mass works OK.

Heavy ball and lighter ball with same gravity have same acceleration speed  isn't it ?

Link to comment
Share on other sites

  • 2 weeks later...


I never noticed the question. sorry for the very late answer.

Can you direct me to the point where backward-compatibility is broken? I tried the first Wingnut demo, and changing the restitution value seems to work.

Just wanted to say - this demo is the first demo in the documentation - http://doc.babylonjs.com/overviews/Using_The_Physics_Engine#basic-physics-scene

I think backwards-comp is a wonderful thing, but if you develop something new, use the new syntax, it is more... future proof :)


Link to comment
Share on other sites

  • 4 months later...

if you add sphere.physicsImpostor.applyImpulse

And if sphere and ground have restitution is 0. After you add impulse to sphere. Sphere begin crazy buncing. 
OK upper playground works ok, until you don't add applyImpulse to sphere. (But in 2.4.0-alpha it works OK).
Problem is wiht 2.4.0 release and 2.5.0-alpha version.

How can we set RESTITUTION TO 0. I think that restitution=0 have some problem in physics. Can anybody check this?

Link to comment
Share on other sites

Hi Ian.  I once heard someone say...  that any physics imposter param (such as mass, friction, resti)... is IGNORED if set to zero.   Try setting your restitution to .00001 or similar, and let's see what happens.  Write back if ya like... ping the wing by using @Wingnut blue label in your reply.  Also, please please please... make playgrounds that show the issue, if you can.  thanks!  talk soon, be well.

Link to comment
Share on other sites

Mass should only be a value between 1 and -1.


friction and restitution can be 0, but if you really need 0 restitution you need to overwrite the y velocity to 0 on a grounded state or when a collision happens with the ground.


 These go over this issue of a physics controller on the bottom.  It's kinda badly strucutred but you'll get the idea by checking out these two scripts:




the one problem I'm running into is browser shortcuts but I think if I give the app focus that won't be a problem... I may change the crouch key from ctrl to shift for now to get around the ctrl-w issue.


Link to comment
Share on other sites

Mass should be a positive value, or 0 if the body shouldn't move. It can be a high number, the physics engine will take that into account when calculating the collisions.

As Wingnut said, there is a small problem with restitution:0 when using the old (deprecated) API (setPhysicsState). Try using the new one, see if it works.

Link to comment
Share on other sites

  • 4 weeks later...

Just out of curiosity, if I set mass to 0, on an object I want to be static. Moveable objects, or objects with a mass of 1 or more, are now able to pass through the static object. In other words, a mesh seems to lose all of it's physics completely. Unless I've misunderstood something. Here's an example!




Link to comment
Share on other sites

Forcing a position might not work, as the physics engine will simple accept it, especially with objects that have no mass.

It is possible to update the position, and the physics engine will simply decide when to "eject" the object away from the collided object. like this - http://www.babylonjs-playground.com/#OYE6Q#7

The best would be to actually use impulse, force, or set the linear velocity of the object - http://www.babylonjs-playground.com/#OYE6Q#6

Also - try using the new API wherever possible. thou backwards compatible, it is much easier to debug the new one.


Link to comment
Share on other sites

@RaananW I think I understand what you're saying, so things like 'animate' and simply changing the position are ignored by the actual physics engine. 

In your examples, the box moves around erratically and jumps all over the place. Is there away to just make it move from its current position, to a picked point, smoothly? 

Thanks so much for you help! I'm a complete n00b to game dev, and Babylon. 



Link to comment
Share on other sites


the first sentence is not entirely true. the position is not ignored by the physics engine, but is forced upon it. It simply has to "accept" the fact that you want the box there, but it will try doing its thing to keep physics going. So if you force the boxes to collide, it will "eject" one of them out in the next frame. that is, unless you keep on forcing a different position.

So - you are the master, you decide where everything goes, but you also need to understand that this is bothering the rest of the components doing their job.

About the example - it is moving in the direction I "ordered" it to. I applied an impulse, and it moves in this direction. You can use the linear velocity feature to force a move in a certain direction, or use forces / impulses. That's the best way to do that with a physics engine. Sadly animations or not so physics friendly.

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.

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...