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.
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:
private Vector2 TotalVelocityToStop()
{
Vector2 initialVelocity = rigidbody.velocity;
float velocityMag = initialVelocity.magnitude;
if (velocityMag <= 0) return Vector2.zero;
float drag = rigidbody.drag;
float timeToStop = Mathf.Log10(0.1f / velocityMag) / (-drag); //0.1f instead of 0, since 0 would be undefined
Vector2 offset = initialVelocity * Mathf.Exp(-drag * timeToStop);
return offset;
}
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.
The general consensus is velocity = velocity * ( 1 - deltaTime * drag); represents a FixedUpdate drag calculation, but that just gets weird when drag > 1.
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.