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

Formula for calculating distance required for rigidbody to come to a full stop

Discussion in 'Scripting' started by ADaurio, Mar 3, 2019.

  1. ADaurio

    ADaurio

    Joined:
    Jul 12, 2015
    Posts:
    21
    This question is pretty straightforward, but I haven't been able to find any answers for it yet. I have a RigidBody2D with a preset drag value (3). How can I calculate the position it would come to a natural stop at based off of its current velocity?

    Looking around the net, I haven't seen many examples of how drag is actually applied to velocity for each fixed update.

    I'm hoping to use the prediction to force an AI to stop accelerating if it's within 1.5x stopping distance of a boundary.

    Thanks for taking the time to read this. Hopefully somebody has this formula on-hand.
     
  2. ADaurio

    ADaurio

    Joined:
    Jul 12, 2015
    Posts:
    21
    I'm taking a look now and giving it a try. I can't do a desired velocity of 0 for that formula, since it will produce an undefined error, but I can try a small value for now.

    [Edit]: Yup, that worked, thanks a lot. Here's the finished function:

    Code (CSharp):
    1. private Vector2 TotalVelocityToStop()
    2.     {
    3.         Vector2 initialVelocity = rigidbody.velocity;
    4.         float velocityMag = initialVelocity.magnitude;
    5.  
    6.         if (velocityMag <= 0) return Vector2.zero;
    7.  
    8.         float drag = rigidbody.drag;
    9.         float timeToStop = Mathf.Log10(0.1f / velocityMag) / (-drag); //0.1f instead of 0, since 0 would be undefined
    10.         Vector2 offset = initialVelocity * Mathf.Exp(-drag * timeToStop);
    11.  
    12.         return offset;
    13.     }
     
    Last edited: Mar 3, 2019
  3. ADaurio

    ADaurio

    Joined:
    Jul 12, 2015
    Posts:
    21
    Did some more testing and this formula isn't completely accurate. I've been looking into more drag calculations, but they all assume drag caps at 1, which is definitely not true in Unity.

    https://forum.unity.com/threads/drag-factor-what-is-it.85504/
    https://forum.unity.com/threads/physics-drag-formula.252406/

    The general consensus is
    Code (CSharp):
    1. velocity = velocity * ( 1 - deltaTime * drag);
    represents a FixedUpdate drag calculation, but that just gets weird when drag > 1.

    Any more ideas or more accurate calculations?
     
    CiberX15 likes this.
  4. ADaurio

    ADaurio

    Joined:
    Jul 12, 2015
    Posts:
    21
    Sorry for the delayed response. Just started a new job and have been busy.

    Sadly no. Unity doesn't use 'proper' drag physics. It uses a linear approximation of some sort form all the research I've done. I need a formula that will tell me where my RigidBody2D will come to a full stop in the engine, not in a real-world sim.