Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Deceleration on Impacts

Discussion in 'Physics' started by Nanako, Feb 19, 2015.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    How does unity handle the deceleratikon of rigidbodies on impacts? I did some testing.
    So far all ive done is crank the fixed timestep to 0.01, and i fired a gravityless-ball v ery slowly at a kinematic wall at a velocity of Vector3.forward (so the magnitude of velocity is 1.0)

    , and tracked the magnitude of its velocity during the impact:



    in these debug messages, the leftmost number is the magnitude of velocity, and the right one is the current frame number (number of fixedupdate calls since start of running). note also i added a conditional to not display message when the velocity was >=1 or = 0

    As can be seen here, it takes about 7 frames to lose most of its speed, but then gets stuck at 8.642675E-06 until frame 393 (15 frames)

    I need some help making sense of this. How is unity doing this gradual deceleration, is there a deterministic pattern to it ? And why does it get stuck at some very low value for 15 frames (0.15 seconds) before finally coming to rest?
     
  2. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    i've been doing some testing, i made this simple test harness script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Diagnostics;
    4.  
    5. public class VelocityMonitor : MonoBehaviour
    6. {
    7.     int i = 0;
    8.     public float j;
    9.     float mag =10f;
    10.     Stopwatch timer = new Stopwatch();
    11.    
    12.     void Start()
    13.     {
    14.         rigidbody.velocity = Vector3.forward*mag;
    15.     }
    16.     void FixedUpdate()
    17.     {
    18.         j = rigidbody.velocity.magnitude;
    19.         if (j < mag && j > 0.0001f)
    20.         {
    21.             timer.Start();
    22.             print(j + "  num " + i);
    23.         }
    24.         else if (timer.IsRunning)
    25.         {
    26.             timer.Stop();
    27.             print("Total Time Taken to stop " + timer.Elapsed.TotalSeconds);
    28.         }
    29.         i++;
    30.     }
    31. }
    32.  
    I also cranked up the fixedTimestep to 0.005 (200 ticks per second!)
    with this value, i was able to get readouts for speeds up to about 8 M/s. The faster the ball is going, the less time it takes to decelerate to a stop, it seems. I maybe be wrong, but that doesn't seem to quite gel with my intuition about physics,

    at about 10 m/s it just goes from fullspeed to zero in a single frame, and i have to crank up the timestep some more to see anything in there