Why do CollisionPolygon2D physics act so strange?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By FarbrorMartin

I’m working on a project where you can split polygons with lines. Like cutting them in pieces.
I calculate the resulting polygon parts and child to a rigidbody2D and add a CollisionPolygon2D, and it kind of works but they have strange behavior. It looks like the polyogons have unbalanced mass, so they don’t always fall to the ground correctly. If I push them they move and roll sort of ok, but when they land flat on the ground sometimes the tilt back up again and come to rest on one corner.

The CollisionPolygon2D shapes are correct, and the polygons do register physics collisions as expected. I read somewhere that it could be the center of mass that’s off, but I don’t know how to adjust that.

Looking for advice on what I can do to fix this!

Polygon resting on one corner, instead of falling over.

Another example of the shape coming to rest in an unbalanced position

Edit: images don’t seem to work for some reason. The images just shows the polygon resting on a corner, instead of flat against the ground.

:bust_in_silhouette: Reply From: FarbrorMartin

I figured out a solution.
It turns out the origin of the rigidbody is in one corner of the node, so to speak. That is, the rigidbody may have its origin at (0,0), and the collider will by default not be centered around the origin, but instead have one corner at the origin. This leads to the strange unbalanced center of mass.
So everything inside the rigidbody needs to be offset by half the width and height. E.g. if the collider is 200 wide and 100 high, it needs to be offset to the position (-100, -50). Doing that makes the physics works as expected.