How to calculate angular velocity of multiple bodies

Hello, everyone!

I'm currently struggling with a little problem. I have multiple bodies and I want to calculate their common angular velocity about their center of mass. I have no problem calculating the center of mass. Then I calculate individual angular moments about the center of mass and add them together. Then I divide the whole thing by the sum of inertias. It looks like this:
Code:
totalInertia = 0;
totalAngMomentum = 0;
for (body: bodies) {
    distance = dst(body.getPosition(), centerOfMass);
    inertia = body.getInertia() + body.getMass() * distance^2;
    ang_vel = body.getLinearVelocity().dot(normalize(body.getPosition() - centerOfMass).rotate(PI/2)) / distance;
    totalInertia += inertia;
    totalAngMomentum += inertia * ang_vel;
}
angVelocity = totalAngMomentum / totalInertia;

There must be something wrong with this, because it gives slightly different values depending on the bodies' angular orientation.

Thanks for your help.
Angular momentum is conserved. Angular velocity, however, is not... Furthermore this only holds for a given fixed point in space. If you move around the center about which your angular momentum is being calculated, then that conservation of angular momentum is also out the window. Looks like you are correctly calculating some inertia-normalized notion of average angular velocity. It's hard to see what such a quantity is useful for, but the point is you should only expect the angular momentum quantity there to remain constant (for some isolated system).

But I may be wrong because to be honest, I dont see why your total inertia would ever change, so that would then mean that it is your angular momentum that is changing when it shouldn't be.

Just double-check your code... if that is meant to be actual C++ code and not pseudocode,
Code:
^2
does not actually square a variable in C++...

I also don't know about if having that mass * distance^2 term in the total inertia calculation makes sense. For objects that aren't welded together why would moving them farther away from some arbitrary location contribute to "total inertia" proportionally to the square of that distance? Why is it squared? Isnt rotational resistance of some point mass on a rigid beam just directly proportional rather than to the square? Just what is this total inertia to begin with, anyway?

Original thread: https://box2d.org/forum/viewtopic.php?t=9940