1
votes

I have a structure made up of welded bodies.

I apply impulses/forces to move the structure around but I notice that the structure isn't moving perfectly straight. It begins moving in the direction of the Vector and then slowly turns, eventually going around in a circle.

The problem is addressed Here

It appears the problem is that the force needs to be applied to the center of mass. However, I am unsure how to get the center of mass from a structure that is welded together.

I have the center of mass of each body in the structure, but they are all at the center. Is there a way to compute the center of mass for a complex, welded structure?

1

1 Answers

0
votes

It's as simple as taking a weighted sum of the world coordinates of each object's centre of mass.

b2Vec2 GetWorldCentreOfMass(std::vector<b2Body> *bodies) {

    // Compute total mass
    float32 totalMass = 0.f;
    for (b2Body &body : bodies) {
        totalMass += body.GetMass();
    }

    // Compute centre of mass
    b2Vec2 centreOfMass(0.f,0.f);
    for (b2Body &body : bodies) {
        b2Vec2 r = body.GetWorldCenter();
        float32 m = body.GetMass();
        centreOfMass += m*r / totalMass;
    }

    return centreOfMass;
}

See: https://en.wikipedia.org/wiki/Center_of_mass#A_system_of_particles

Then use void ApplyLinearImpulse(const b2Vec2& impulse, const b2Vec2& point);:

b2Vec2 centreOfMass = GetWorldCentreOfMass(bodies);
for (b2Body &body : bodies) {
    body.ApplyLinearImpulse(impulse, centreOfMass);
}