Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Calculating the time for a 2D rigidbody to slow after removing torque

Discussion in 'Physics' started by GeekOverdrive, Aug 19, 2020.

  1. GeekOverdrive

    GeekOverdrive

    Joined:
    Dec 31, 2018
    Posts:
    8
    Hi there,

    I found this post and it seemed like the perfect answer, and the only one for that matter--except... it didn't work. I've played around with the solution given, and I cannot figure out why it doesn't work. I followed through his explanation and it seems to match up perfectly, except the actual physics don't work out the way it's calculated. It's not even close, most times nearly an entire second off.

    I'm not particularly great with physics, as I've come to find out. Is there anyone who wouldn't mind guiding me in figuring out how to calculate the time it will take a 2D rigidbody to slow down with angular drag?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,149
    It doesn't work because devs forget that engine physics is time-integrated and single continuous function won't always represent what's going to be calculated i.e. multiple steps of fixed-update or some other time interval.

    2D physics uses Box2D so look at the Box2D source. Here's where the angular velocity (w) is modified by the angular drag (damping): https://github.com/erincatto/box2d/...7e939d199d1c4/src/dynamics/b2_island.cpp#L222

    Note that "h" is the time-step.

    Or in pseudo-code:
    rigidbody2D.angularVelocity *= 1.0f / (1.0f + Time.fixedDeltaTime * rigidbody2D.angularDrag);

    This is what happens in a single simulation step.

    A body will go to sleep if it isn't moving beyond a linear or angular threshold. You can find that code here.
     
    GeekOverdrive likes this.
  3. GeekOverdrive

    GeekOverdrive

    Joined:
    Dec 31, 2018
    Posts:
    8
    Thanks for the reply Melv!

    I'd love to use the `isSleeping` check, but unfortunately, the situation in which I need to calculate the angular drag time includes movement using `AddForce`. I.e, it doesn't just move rotationally and wouldn't be sleeping when it comes to rest angularly.

    I spent a few hours trying to figure out how to calculate the angular drag time from that pseudo equation and the Box2D equation directly, but my results were far from correct. o_O Do you have any tips for how I should continue?

    Or were you saying it's not possible to be accurate with an equation like this?
    • "[A] single continuous function won't always represent what's going to be calculated i.e. multiple steps of fixed-update or some other time interval."
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,149
    You mean linear movement. Your original post only stated rotation and angular drag. You should try to clarify the full picture of what you're trying to achieve. Linear drag is the same and you can see it next to the angular drag I posted above. You don't state it but other factors affect the velocity like collision response and joints. If you're in contact with a surface then friction can occur etc. If not then it's simply down to drag.

    I don't follow what you're asking. To reiterate, the physics system obviously doesn't use the linked formulas over large time-scales, it just repeats them over small time-scales (typically Time.fixedDeltaTime) many times a second.

    TBH, without writing it for you it's hard to describe it any other way. In short though, without contacts (no collision response, friction or bouncing) then linear and angular velocities will reduce if you're using linear/angular drag. The link I provided shows you how velocities are reduces given those drag values. Repeat them using the time-steps many times and you'll see them reduce. If you use the function I linked to, it'll be identical to what happens in the 2D physics.
     
    GeekOverdrive likes this.
  5. GeekOverdrive

    GeekOverdrive

    Joined:
    Dec 31, 2018
    Posts:
    8
    Indeed. I didn't mention it because I figured it wasn't relevant. There won't be any collisions involved or anything else that would reduce the object's velocity besides drag in the situation I'm trying to use this for.

    I see what you are saying now. I had somehow locked my head around a single calculation being the answer. I don't know why I had a problem with just repeating the calculation until the desired value is reached in the first place.

    Thanks Melv. I'll do that :)
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,149
    Note that you can create a new Unity scene and create a local 2D physics scene in it. That allows you to add Rigidbody2D (etc) and perform manual simulation on it. This means the actual physics system can be used to calculate it because you just manually simulate it and read values until you're happy so wait until velocity is below a certain value or it goes to sleep etc.
     
    GeekOverdrive likes this.