Updating the mass applies a force to the physics body

I’m running into a problem, where I want to update a physics impostors mass after it has been created. I’m using the method setMass, according to the docs: PhysicsImpostor | Babylon.js Documentation

Here is a playground which demonstrate the issue: https://playground.babylonjs.com/#BEFOO#1624

The code increases the mass every 2 seconds by 1kg. After one or two times, the sphere begins to shoot into the sky. In the commented out code below, I basically did the same thing, like the cannon physics plugin does (see: Babylon.js/cannonJSPlugin.ts at master · BabylonJS/Babylon.js · GitHub) but this seems to not have this issue (Edit: Though, I didn’t check if the new mass is actually applied properly).

Yes, this is an edge case. When mass increases, small penetration in the ground happens. And the physics engine responds just like it’s a collision and the sphere bounces.

I just tried with AmmoJS and it seems to better handle this case.

Do you have an explanation why this is not happening, when I call the cannon methods directly?

Oh and another thing I observed, when the object is in mid-air and the mass gets updated, there is clearly a change in velocity visible. I updated the playground with a lower gravitiy where it’s easy to observe this behaviour: https://playground.babylonjs.com/#BEFOO#1628

I had the same issue in my project, where there’s zero gravity applied and the object was floating without any contact/collision. However I can’t tell for sure if there is another issue which causes this.

I’m going to record a screencast later today.

I’ve debugged and it’s the same code that’s being executed.

Changing in velocity might be an issue with cannon solver. does it repro with other engines?
Maybe cannon stores forces appplied to object and a change in mass will also change linear velocity.

Here’s the screencast, where one can clearly see the change in velocity while the sphere beeing in mid-air:

Edit: Like for you, when I change the physics plugin to Ammo I don’t see this issue. However calling the methods on the physicsBody directly:

sphere.physicsImpostor.physicsBody.mass = newMass;
sphere.physicsImpostor.physicsBody.updateMassProperties();

also does not show the issue, which for me, is an indicator that something is wrong with the Babylon Cannon Physics Plugin

I have no idea why it’s happening. I traced down the code and the same method updateMassProperties is called.

I noticed another issue, where the velocity never really drops below a certain value, even though there are no forces applied: Cannon Physics: physics body never switches to sleep state

This might be related to this or maybe not :man_shrugging: