How to define center of mass of an object?

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


Say I have a sphere mesh and I want to add collision object to it, so it can roll on the ground. The center of mass is always in the middle.

How can I change this and make my sphere heavier on one side, so when no other forces are applied, it will roll, so that the heavy part is touching the ground and resist further rolling a little bit?

:bust_in_silhouette: Reply From: Zylann

You can’t properly define it. By default the center of mass is at the origin of the body, regardless of shapes it contains.
For now, you have to offset your collision shapes in such a way that the origin of the body matches the position you want.

I posted an issue earlier to have the ability to change not only that, but also inertia tensor: Custom center of mass · Issue #28062 · godotengine/godot · GitHub

So if i make a rigid body with (0,0,0) coordinates, and then make a sphere with radius of 1, move it up by 1 and create a matching collision object, I should have a sphere with the center of mass located on its lowest point, right?

Skipperro | 2019-08-02 12:59

The position of the rigidbody itself doesnt matter, but the position of the collision shapes relatively to the rigidbody does the trick. So yeah if you create a sphere as a child and give it a local position of (0, 1, 0), its center of mass should be at the bottom.

Zylann | 2019-08-02 13:13

OK, so for objects that don’t have to dynamically shift their center of mass and can have it defined this way by hand, this is actually pretty good solution. Thanks!

Skipperro | 2019-08-02 13:19

:bust_in_silhouette: Reply From: SvapoManiac

Mine is a more of a rudimental idea but you could use a local Position2D to mark the center of mass , you could set it the center and use it as a vector to where apply impulses or forces, and then move the Position2D to where you want it to be by script

True, but shifting mesh and collision object relative to body is much simpler and doesn’t require any scripts. Or are there any benefits of this solution, that I miss?

Skipperro | 2019-08-02 15:47

i thought you wanted to change the center of mass while in runtime
it’s just another way to do that, not better, not worse, but surely rudimental Hahah :slight_smile:

SvapoManiac | 2019-08-02 17:17

Well, I didn’t tried it yet, but in theory if I would move mesh and collision relative to body at runtime via a script and make a counter-move for the body (in opposite vector), this should change center of mass while object would visibly stay in the same spot :slight_smile:

Skipperro | 2019-08-02 17:21

I’m not an expert of balls , but i think your solution should work :=)

SvapoManiac | 2019-08-02 17:23

I thought about that solution too but it boils down to “teleporting collision shapes”, which I had a lot of trouble with in the past, with bodies falling through ground or jumping around. It really should not be that complicated to change center of mass and inertia tensor, it’s litterally a few variables somewhere in Bullet Rigidbody :stuck_out_tongue: (we could even think about having the engine compute them automatically, provided the density of each shape!)

Zylann | 2019-08-02 17:27

teleporting with physics objects is always a problem
It would be cool to make a feature request about that
in the meanwhile we could code such a function in the game itself, maybe as a singleton
i would need to revise some classic physics anyway Hahah

SvapoManiac | 2019-08-02 18:14