Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Physics2D Center Of Mass incorrect after changing object scale

Discussion in 'Physics' started by Meatloaf4, Jul 31, 2020.

  1. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    76
    I'm seeing what appears to be a center of mass bug after scaling an object with a PolygonCollider2D and a RigidyBody2D. See below image for the gameobject settings.
    upload_2020-7-31_11-0-2.png

    The Center Of Mass seems to adjust incorrectly as I Scale the object up. See attached gif.

    I expect that the center of mass should stay at the same point as when the scale is 1,1,1 and it is scaled up uniformly.

    I'm on Unity 2018.4.17 and the code i'm using to display the center of mass is below

    Code (CSharp):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4. [CustomEditor(typeof(Rigidbody2D))]
    5. public class RigidbodyEditor : Editor
    6. {
    7.     void OnSceneGUI()
    8.     {
    9.         var rb = target as Rigidbody2D;
    10.         Handles.color = Color.red;
    11.         Handles.SphereHandleCap(1,rb.transform.TransformPoint(rb.centerOfMass),Quaternion.identity,0.1f, EventType.Repaint);
    12.     }
    13.  
    14.     public override void OnInspectorGUI()
    15.     {
    16.         GUI.skin = EditorGUIUtility.GetBuiltinSkin(UnityEditor.EditorSkin.Inspector);
    17.         DrawDefaultInspector();
    18.     }
    19. }
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,936
    You don't actually show the center of mass changing which would've been useful. Just open the "Info" panel on the Rigidbody2D.

    The local center of mass shouldn't change but the world center of mass obviously would.
     
    Meatloaf4 likes this.
  3. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    76
    Ah got yeah. I've gone ahead and attached a gif showing off the Rigidbody2D info.

    It does indeed look like the Local Center Of Mass is changing.
     

    Attached Files:

  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,936
    Hmm, so the collider center is offset from the local position and you scale but don't expect the local center of gravity to change? Picture in your mind where you think it is, change the scale then see where you think it should be. It won't be at the same position.

    Take a circle centered on the position (no offset). Change the scale and the local center of mass won't change as you scale. Offset the circle up a little and scale and it will because that local position changes. If you change the position or Z rotation then the local center of mass won't change.

    Scale will always affect it if the center of mass doesn't coincide with the point around which scaling takes place.

    In 2D the center of mass isn't invariant on scale. When you scale in 2D that isn't some runtime thing that can change dynamically. It will cause the shapes to be recreated at that size and they will exist in the physics system at that size, not some non-scaled version of it. Box2D doesn't have any kind of transform support from for shapes that allows dynamically resizing them, only XY position and Z rotation and they are completely immutable. Indeed, in your case you're using a PolygonCollider2D so you scale the outline path and we have to decompose that into multiple primitive convex polygons too. Maybe it's this part that is causing the confusion.
     
    Last edited: Aug 3, 2020 at 10:40 AM
  5. Meatloaf4

    Meatloaf4

    Joined:
    Jul 30, 2013
    Posts:
    76
    Sorry I suppose why I was getting confused is I didn't see how my collider center was offset.

    After some closer investigation though I can see that the pivot point for the sprite is what effects the bounds center. I had the pivot point at the bottom center and thus the collider bounds were being shifted to accommodate that I presume.

    So now that this is finally starting to make some sense (really appreciate the help btw!) based on your experience can you give me some guidance on the best way to move forward. Essentially I want to scale up my rocket to 20% of what the normal size is but I want to ensure the center of mass adjusts as if the bounds center was 0,0,0.

    Some solutions I was thinking through were as follows.
    1. Manually adjust the center of mass after scale increase setting Rigidbody2D.centerOfMass.
    2. Adjusting the pixels per unit of the missile sprite so that it increases the sprite size by 20% but I can still keep the scale at 1,1,1.

    Thanks so much in advance and for all the help thus far.

    ---Edit---

    I went ahead and adjusted the pixels per unit but realized pretty quickly that the downside there is I have to adjust all my child gameobjects in relation to this new sprite size. It looks like the manual adjustment of the center of mass might be the best way to go about this then.
     

    Attached Files:

    Last edited: Aug 5, 2020 at 11:05 PM
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    2,936
    Rendering has no effect on physics so a sprite won't change center of mass or its bounds. If the collider is created offset at the position of the sprite then it happens to relate but that's it. These systems don't communicate with each other at all.

    I'm a little bit confused by this though, the COM will be correct for any Transform operation and the Rigidbody2D will rotate around that point. If you don't want that then you'll have to set an alternative yourself.
     
unityunity