Search Unity

How Does Physics.sleepThreshold Work?

Discussion in 'Physics' started by topsekret, Mar 24, 2017.

  1. topsekret

    topsekret

    Joined:
    Apr 18, 2013
    Posts:
    69
    According to the documentation, Physics.sleepThreshold is "The mass-normalized energy threshold, below which objects start going to sleep.". However, from my tests, this is not precisely the criteria that Unity uses to determine when to sleep a Rigidbody.

    To test this, I wrote a script the briefly applies a constant torque about the y-axis to a sphere under conditions of no gravity. I had another script on this sphere keep track of a queue of Rigidbody properties for the past several FixedUpdate ticks. This script would also check each FixedUpdate if the sphere Rigidbody was sleeping. On the frame that it went to sleep, it would log the history of Rigidbody stats so I could see the kinetic energy of the Rigidbody the frame before it went to sleep and compare this to Physics.sleepThreshold.

    Kinetic energy is the sum of translation kinetic energy and rotational kinetic energy, which is 0.5 * mass * speed^2 + 0.5 * momentOfInertia * angularVelocity^2. When Unity says "mass-normalized", I am assuming they either mean to divide the kinetic energy by mass or to divide the translational term by mass and the rotational term by momentOfInertia. I tried observing both values on the frame the Rigidbody went to sleep, but both were about significantly lower than Physics.sleepThreshold and it seems like the Rigidbody should have gone to sleep much earlier.

    Can a Unity dev or someone with Unity source code access please illuminate exactly how the Physics.sleepThreshold is used to determine when to put Rigidbodies to sleep?
     
  2. ZJP

    ZJP

    Joined:
    Jan 22, 2010
    Posts:
    2,649
    Up
     
  3. AndyGainey

    AndyGainey

    Joined:
    Dec 2, 2015
    Posts:
    216
    I briefly perused the nVidia PhysX documentation and found this:
    If I read that correctly, the energy you're measuring is simply the threshold to begin the countdown toward sleep, not the threshold to immediately put an object to sleep. Probably helps avoid scenarios where the energy of an object fluctuates above/below the threshold for a while, causing the object to sleep/wake repeatedly.
     
    topsekret likes this.