Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inertia not updated when calling Body.setMass() #378

Closed
touho opened this issue Mar 8, 2017 · 4 comments
Closed

Inertia not updated when calling Body.setMass() #378

touho opened this issue Mar 8, 2017 · 4 comments
Labels

Comments

@touho
Copy link

touho commented Mar 8, 2017

I started using Matter.js in my game editor where you can create bodies and edit them later on. I noticed that these two code snippets give different results:

var body = Matter.Bodies.rectangle(x, y, w, h, { density: 0.01 });
var body = Matter.Bodies.rectangle(x, y, w, h, { density: 0.001 });
Matter.Body.setDensity(body, 0.01);

Both of the snippets will set density to 0.01, but since inertial is only calculated on creation of the body, these snippets will result in different body.inertial value. Therefore bodies act really weird.

When density changes, mass is recalculated. Shouldn't inertial be also recalculated when mass changes? If you think so, I would be happy to contribute.

@touho
Copy link
Author

touho commented Mar 8, 2017

Not recalculating inertia is probably good optimization if plenty of bodies change their mass a little. But atleast a mention in the comment or taking third parameter Body.setMass = function(body, mass, recalculateInertia) would be helpful.

@liabru
Copy link
Owner

liabru commented Mar 8, 2017

Good spot. Recalculating inertia in this case is easy, we can just do:

var moment = body.inertia / (body.mass / 6);
body.inertia = moment * (mass / 6);

I'll tag this as a bug, thanks.

@liabru liabru added the bug label Mar 8, 2017
@touho
Copy link
Author

touho commented Mar 9, 2017

// allow options to override the automatically calculated properties
Body.set(body, {
	axes: options.axes || body.axes,
	area: options.area || body.area,
	mass: options.mass || body.mass,
	inertia: options.inertia || body.inertia
});

This code might break if inertia is set in setMass function. Might have to separately call setInertia afterwards.

I really like how you have structured the library. Well done!

@Technostalgic
Copy link

Technostalgic commented Apr 11, 2017

Ran into this bug too, had me real confused for a hot minute.
For me, I was using Matter.Body.setDensity to set the density of a body, but when I tested it, it seemed that the body's rotational velocity was being computed correctly, but the inertia didn't account for its linear velocity.

I'm still a bit confused as to why the inertia not being updated only seemed to be effecting the linear velocity and not the rotational velocity.

on an unrelated note:

liabru
var moment = body.inertia / (body.mass / 6);
body.inertia = moment * (mass / 6);

I assume is supposed to be:
var moment = body.inertia / (body.mass / 6);
body.inertia = moment * (body.mass / 6);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants