Matter-js: Gravity and constraints computations seem to ignore body mass values

Created on 1 Mar 2020  路  4Comments  路  Source: liabru/matter-js

Hello!
First of all, thank you for the effort you put into developing this great project, I'm having a good time working with it.

I would like to point out some issues me and my colleagues ran into while trying to represent simple scenarios involving falling bodies and spring constraints.

At first, we created two identical bodies, configuring them to have no body or air friction at all, but different mass values (by setting the density property). By letting them fall free for an indefinite time, we were expecting to see some difference in their speeds, with the heavier one going faster than the other, but that wasn't the case.
Looking at the code responsible for updating the bodies velocities and positions (Body.update function) we noticed the mass term was missing from one of the steps with which you implement the Verlet integration method.
In particular I'm referring to this piece of code:

var deltaTimeSquared = Math.pow(deltaTime * timeScale * body.timeScale, 2);

// from the previous step
var frictionAir = 1 - body.frictionAir * timeScale * body.timeScale **/ body.mass**,
velocityPrevX = body.position.x - body.positionPrev.x,
velocityPrevY = body.position.y - body.positionPrev.y;

// update velocity with Verlet integration
body.velocity.x = (velocityPrevX * frictionAir * correction) + (body.force.x / body.mass) * deltaTimeSquared;
body.velocity.y = (velocityPrevY * frictionAir * correction) + (body.force.y / body.mass) * deltaTimeSquared;

The term surrounded by the double asterisk in the frictionAir variable assignment is the one we think should be added for the mass to be considered when updating the velocity value. We forked the code to try to apply this small change and had the result we initially expected.

Then, we went on by attaching the two bodies to two different spring constraints (stiffness value of 0.001), both with a fixed pointA (no other body attached).
Again, our bodies went up and down while being tied to their springs, but without any differences given by their masses: the entity of the oscillations were identical.
I suspect this second issues is somehow related to the other one, maybe a mass term should be added during some computation. I'm trying to look around in the Constraint.solve function and playing with the code, but haven't had any luck yet.

I hope this was of your interest, I also thought that you may also be aware of this and not be interested in modifying the formulas for some reason, but I still want to point it out, and ask for some advice on how to have masses influence the behavior of the bodies in our cases.

Obviously, I'm always available for more clarifications on this. I could also open a pull request with the small modification I wrote before, if you want.

Thank you again, looking forward for your reply!

question

All 4 comments

Glad you're enjoying using the project!

If I understand what you mean, it's that you want a body with a larger mass to fall faster than a smaller mass body?

Normally mass should have no effect on the velocity of bodies falling under gravity in a vacuum, since the force applied is always proportional to mass, resulting in the same acceleration.

What you should probably change to get the effect I think you might be looking for is the body.frictionAir, which should change the terminal velocity when falling. See the air friction example, hopefully that's what you need.

Thank you for your answer!

You got the point, but I'm not sure to completely understand your answer. It is correct that mass shouldn't impact in case we're referring to a vacuum system, but that's only true when frictionAir is set to 0.
If its value is positive, then we can say that air is present and has an impact on falling bodies, and the entity of the force that slows them down also depends on their masses, as I pointed out in the open post.
With our small change, even bodies with the same frictionAir value fall with different speeds if their masses differ.

The second question, involving constraints, is related to the same problem, since the effect of mass doesn't seem to be taken into consideration during the computation of constraint forces, as was happening for gravity.

the entity of the force that slows them down also depends on their masses, as I pointed out in the open post.

Not sure that it does though, at least in the common approaches for fluid drag I've seen e.g. using the drag equation, where it's a function of the body's area and shape (or volume in 3D), the fluid density and the relative velocity of both, but not the mass of the body.

The engine still uses a more simplified approach than that one though. I considered factoring in area too originally, but I think most people probably wouldn't find this very intuitive in a physics engine.

If you want this in your project though you should be able to factor in the area to the air friction e.g. body.airFriction = 0.001 * body.area for each body after creation, even mass too if that's the effect you're looking for.

Then, we went on by attaching the two bodies to two different spring constraints (stiffness value of 0.001), both with a fixed pointA (no other body attached).
Again, our bodies went up and down while being tied to their springs, but without any differences given by their masses: the entity of the oscillations were identical.

Sorry I missed your second question about constraints: one thing I should probably make clearer somewhere is that soft constraints act differently to springs, even though they appear to be like springs. Mass is factored in, but not in the same way as for a spring, which is why there is no difference in period as you might expect.

In future I might look into adding more realistic springs, but for now a user has made a plugin for this, which you can check out here.

Thanks again a lot for the tip.

However, I am not planning to take the shape of the body into account yet.
Sorry for being inaccurate in my previous answer. What I should have said is that the effect of the drag force (the acceleration of the body) does depend on the mass of the body. This of course holds because the drag force is mass-independent, as you pointed out. This effect is taken into account by dividing body.frictionAir by body.mass.

About the second point, I understand, maybe we relied too much on the approximation between matter-js "soft constraints" and real world springs. We will definitely check out the plugin you referenced and see if it suits our situation.

Was this page helpful?
0 / 5 - 0 ratings