How far will a 2d object travel before coming to a stop given constant force and drag

Hey all,

I’m trying to figure out how to calculate the distance a 2d physics body will travel before coming to a stop, given a constant force (deceleration) and drag.

I figured it was some simple equation but I have no background in physics and can’t find the answer online. I figure this is a common enough problem, but I cant get it.

I think I need to be able to calculate top speed given a force and drag, but I’m not sure how to go about it.

*** EDIT

My goal is to know when to begin deceleration in order to arrive precisely at a target position. Both rotation and position. As well as to be able to know what max speed will be.

If you set rigidbody drag to 0 and avoid friction then it should be following the basic equations of motion (Equations of motion - Wikipedia). Remembering F(force) = m(mass) x a(acceleration) and you are there:

given:
v^2 = u^2 + 2as

where
v = final velocity (0 if it comes to a stop)
u = initial velocity
a = acceleration = Force / mass
s = distance (the thing you want to know)

then
s = -u^2 / 2a

I avoid using the internal rigidbody drag as I prefer to have full control over the drag forces if they are important and set it to 0. If you want more information on it, I found this post How drag is calculated by Unity engine.? - Questions & Answers - Unity Discussions

If you want to calculate the top speed of an object in air you will want to look at this equation:

Drag force = 0.5 x air density * velocity^2 * coefficient of drag * frontal area

Coefficient of drag is usually found by experimentation or flow simulation as it is dependent upon both shape and fluid properties. You will find lots of examples on the internet.

The top speed will be where your maximum available driving force equals this drag force.

If you have sliding friction going on as well, then you will have to dig deeper and see how Unity (PhysX) deals with it.

I’ve generally learnt not to trust PhysX to be strictly following the laws of physics, this is a game engine and it is more likely to be efficiently, if not accurately, approximating them.

1 Like

The full Box2D source code for the 2D physics engine is here. The motion dynamics can be found in the island code here with most notably the velocity integration and damping here. It iterates all the bodies, adds to the linear velocity both applied forces and gravity integrated over the time-step and angular velocity has torque applied. Then it adds in drag for both linear and angular. Later it adds the linear/angular velocity onto the position/rotation integrated over the time-step.

This is a few lines of code so it’s pretty simply to perform in your own code.

Here’s table which should help you translate it into Unity data:

  • b2Body = Rigidbody2D

  • m_linearVelocity = Rigidbody2D.velocity

  • m_angularVelocity = Rigidbody2D.angularVelocity

  • m_linearDamping = Rigidbody2D.drag

  • m_angularDamping = Rigidbody2D.angularDrag

  • m_invMass = 1 / Rigidbody2D.mass

  • m_InvI = 1 / Rigidbody2D.inertia

  • m_force = sum of forces added via Rigidbody2D.Addforce etc since last simulation step.

  • m_torque = sum of torque added via Rigidbody2D.AddTorque since last simulation step

  • h = timeStep in seconds. By default this is Time.fixedDeltaTime.

An important thing to consider is that the simulation is “stepped” in discrete time intervals using numerical integration therefore taking a continuous function over a large time interval won’t be equivalent.

Mostly though it’s pretty simple stuff for 2D as you can see in the source.

1 Like

Thank you both for your replies. For whatever reason, I didn’t even think to go to the box2d source. Herp derp. Iv’e helped translate portions of box2d into javascript once upon a time and should have known go to the source.