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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Question about physics when scaling object

Discussion in 'Physics' started by DforDeclare, Apr 24, 2023.

  1. DforDeclare

    DforDeclare

    Joined:
    Jul 6, 2022
    Posts:
    2
    07.png 36.png
    First, apologize for my poor english.
    Rigidbody was added to capsule and cube, and the cube was frozen the position and rotation. This is what i expected, when I scaling this cube quickly in y direction, the capsule above it will be bounced vertically. But actually, the capsule will be pushed aside.

    Scaling code here
    Code (CSharp):
    1. private void FixedUpdate()
    2.     {
    3.         if (s)
    4.         {
    5.             Vector3 scaleAcc = new Vector3(2, 8, 2) - transform.localScale;
    6.             this.transform.localScale = transform.localScale + scaleAcc * 0.2f;
    7.         }
    8.     }
    Currently, Setting the scale of the cube is not gonna helping. The best way to change the position and rotation of a rigidbody is adding velocity and angularvelocity. I wonder is there any way to change the scale like adding velocity and angularvelocity.

    Btw there is a werid situation here (maybe bug?). When the position and rotation of the cube were frozen, and the cube goes through the ground. If the capsule touches this cube right now, the capsule will be bounced inexplicably.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,434
    Scaling an object doesn't produce physically correct behaviors. It's equivalent to replace the original object with a different object in a single physics frame. The physics engine will detect both bodies colliding as if they were just instanced in that situation, and will try to separate them. Some times the result will be the expected, sometimes not.

    To my knowledge, there's not a solution in Unity / PhysX that simulates the changes in scale as the objects physically growing or shrinking, so these kind of side effects will be produced.
     
    DforDeclare likes this.
  3. DforDeclare

    DforDeclare

    Joined:
    Jul 6, 2022
    Posts:
    2
    Thanks for your reply!!! Your answer inspired me a lot. Later I learned that Unity's rigid body is actually an encapsulation of PhysX, but changing the scale of the object is replacing it really beyond my expectation. I think I'll find the solution about my question. Again, thanks for your answer~
     
  4. arkano22

    arkano22

    Joined:
    Sep 20, 2012
    Posts:
    1,690
    Just to add to Edy's answer, same can be said of any transform manipulation: setting the position, rotation or scale of a transform is seen by the physics engine as an instant change. So if you set the position of the transform, you're essentially teleporting the object. If you set the scale, you're instantly replacing the object with a bigger one. As far as physics are concerned, the object never "moved" or "grew" in any way.

    To move objects in a physically consistent way, use rigidbody.MovePosition and rigidbody.MoveRotation. There's no equivalent method for scaling.

    As to why the capsule is pushed to the side instead of upwards in your case, it's because that's the shortest "escape route" from inside the box. Most physics engines use what's known as the minimum translation vector (aka MTV) to solve collisions. In your case, moving the capsule upwards is the longest route, but the engine will always choose the shortest one.
     
    DforDeclare and Edy like this.