How to get center of mass of a RigidBody3D set to CenterOfMassMode Auto?

Godot Version

4.7.2

Question

According to the documentation, the only way to get a RigidBody3Ds’ center of mass via script is the property “center_of_mass”. However, when center_of_mass_mode is set to CENTER_OF_MASS_MODE_AUTO, it says that the property is not updated at all, so it seems to only be used when setting a custom center of mass. And in testing, it just returns the origin of the object as expected.

I happen to be really fond of how the default auto setting influences the center of mass of a RigidBody, especially since I edit the colliders on the fly in my game, and its’ able to account for that really well. But I also have a feature where the player can grab physics objects, and I’d like for the point the player holds in front of them to be the objects center of mass.

As far as I can tell, there’s absolutely no option for this, at all. I even dug into PhysicsServer3D, it seems there’s an option to read the center of mass like so:

PhysicsServer3D.body_get_param(RigidBody.get_rid(), PhysicsServer3D.BODY_PARAM_CENTER_OF_MASS)

but reading between the lines in the documentation seems to suggest this is also exclusively for setting a custom center of mass, and it again always seems to return the origin in game.

Is there a proper way to do this? Or am I gonna have to hack together an alternative solution to ‘guess’ the center of mass? The RigidBodies in my game have unpredictable colliders, and the origin is often not a good indication of its’ true center.

You can get the center of mass with PhysicsDirectBodyState3D.center_of_mass. You can access the body’s state in the RigidBody3D._integrate_forces() callback or using PhysicsServer3D.body_get_direct_state()