Drifting center of mass

Assigning slightly different (by at least two axis) center of mass (CoM) causes it to drift in a world space.

There are prepared scene and script in the package attached. As simple as a stone axe.
Scene consist of a simple cube in (0;0;0) with default size, scale and mass. Gravity disabled. A script is attached to the cube. It changes cube’s CoM to (0,1; 5; 0).

using UnityEngine;

public class testscript : MonoBehaviour
{
    public Rigidbody rb;
    public bool updateCoM = true;
    public Vector3 newCoM = new Vector3 (.1f, 5f, 0f);
    public bool updateMass = false;
    public float newMass = 1000f;
    public bool resetCoM = false;
    public bool showDebug = false;
 
    // Update is called once per frame
    void FixedUpdate()
    {
        if (resetCoM) rb.ResetCenterOfMass();

        if (updateCoM) rb.centerOfMass = newCoM;
        if (updateMass) rb.mass = newMass;
        var WCoM1 = transform.TransformPoint(rb.centerOfMass);
        var WCoM2 = rb.worldCenterOfMass;
        var pos1 = rb.position;
        var pos2 = transform.position;

        if (showDebug)
        {
            Debug.LogFormat("World CoM as transform.TransformPoint(rb.centerOfMass) ({0:f5}, {1:f5}, {2:f5}) vs rb.worldCenterOfMass ({3:f5}, {4:f5}, {5:f5})", WCoM1.x, WCoM1.y, WCoM1.z, WCoM2.x, WCoM2.y, WCoM2.z);
            Debug.LogFormat("rb.position ({0:f5}, {1:f5}, {2:f5}) vs transform.position ({3:f5}, {4:f5}, {5:f5})", pos1.x, pos1.y, pos1.z, pos2.x, pos2.y, pos2.z);
        }
    }
}

A few seconds in the runtime

If you enable debug.log, you may see something like this


, which says us that calculated world space CoM is different from the World CoM retrieved as a rigidbody property. How is that possible?
Let’s take a look at the second row. Transform.position is constant, but rigidbody.position is not. It changes. That’s why calculated CoM has to be correct, but actually it drifts and you can see it from the World CoM property.

There is definitely something wrong with it as rigidbody shouldn’t move at all in such conditions.

====
Moreover, as an addition. Disable CoM assigning, enable mass assigning and you’ll see that CoM continues to drift (shown in the video as well). :hushed:

Unity version is 2020.3.18f1

7641958–952207–Center Of Mass error.unitypackage (3.75 KB)

I would suggest producing a bug report with all the above if you haven’t already.

Nope. How can I do that? :sweat_smile:

====
Oh. I see. Help → Report Bug. Thanks!

This is an unfortunate consequence of PhysX actually storing everything relative to the centre of mass, so every time you modify it, coordinates have to be recalc’ed and there appears drift. It’s easy to reproduce this even by setting mass every frame.