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

Possible bug in calculation of compound body centroid #483

Closed
Qriva opened this issue Sep 5, 2017 · 1 comment
Closed

Possible bug in calculation of compound body centroid #483

Qriva opened this issue Sep 5, 2017 · 1 comment

Comments

@Qriva
Copy link
Contributor

Qriva commented Sep 5, 2017

As I know the first body in body.parts is the parent (main) body. If body consists of more than 1 part, every next part become child body so the first one become some kind of container for the rest.
During analyzing of Bodies.fromVertices function i found that Body object after setting all parts it calculate total mass, inertia and centroid based on these parts and there comes _totalProperties function - but I see that centroid can be calculated wrong. I talking about last lines of this function:

var _totalProperties = function(body) {
    // [...]
    // sum the properties of all compound parts of the parent body
    for (var i = body.parts.length === 1 ? 0 : 1; i < body.parts.length; i++) {
        var part = body.parts[i];
        properties.mass += part.mass;
        properties.area += part.area;
        properties.inertia += part.inertia;
        properties.centre = Vector.add(
            properties.centre, 
            Vector.mult(part.position, part.mass !== Infinity ? part.mass : 1)
        );
    }

    properties.centre = Vector.div(
        properties.centre, 
        properties.mass !== Infinity ? properties.mass : body.parts.length // this one 
    );
    return properties;
};

This "if" properties.mass !== Infinity ? properties.mass : body.parts.length return total mass of body parts if mass is not infinity (what means body is static) and otherwise it returns body.parts.length but as my logic say it is wrong because the first part is the parent body and mass of first one is not calculated to total mass as you can see in loop conditions. Is that ok ? I think this should be divided by number of parts without parent.

@liabru
Copy link
Owner

liabru commented Sep 17, 2017

Good catch, I think you may be right. Looks like this would affect static bodies made up of parts. Will check it out.

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

No branches or pull requests

2 participants