Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Drifting center of mass

Discussion in 'Physics' started by devilhunterxl, Nov 9, 2021.

  1. devilhunterxl

    devilhunterxl

    Joined:
    Dec 19, 2018
    Posts:
    14
    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).
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class testscript : MonoBehaviour
    4. {
    5.     public Rigidbody rb;
    6.     public bool updateCoM = true;
    7.     public Vector3 newCoM = new Vector3 (.1f, 5f, 0f);
    8.     public bool updateMass = false;
    9.     public float newMass = 1000f;
    10.     public bool resetCoM = false;
    11.     public bool showDebug = false;
    12.  
    13.     // Update is called once per frame
    14.     void FixedUpdate()
    15.     {
    16.         if (resetCoM) rb.ResetCenterOfMass();
    17.  
    18.         if (updateCoM) rb.centerOfMass = newCoM;
    19.         if (updateMass) rb.mass = newMass;
    20.         var WCoM1 = transform.TransformPoint(rb.centerOfMass);
    21.         var WCoM2 = rb.worldCenterOfMass;
    22.         var pos1 = rb.position;
    23.         var pos2 = transform.position;
    24.  
    25.         if (showDebug)
    26.         {
    27.             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);
    28.             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);
    29.         }
    30.     }
    31. }
    32.  
    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). :eek:
    ===
    Unity version is 2020.3.18f1
     

    Attached Files:

    trombonaut likes this.
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    I would suggest producing a bug report with all the above if you haven't already.
     
  3. devilhunterxl

    devilhunterxl

    Joined:
    Dec 19, 2018
    Posts:
    14
    Nope. How can I do that? :oops:


    ====
    Oh. I see. Help -> Report Bug. Thanks!
     
  4. yant

    yant

    Unity Technologies

    Joined:
    Jul 24, 2013
    Posts:
    577
    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.
     
    Edy likes this.