# Predicting trajectory of Box2D physics body using both: linear dumping and gravity

I would like to calculate position of physics body after some time because of predicting shots trajectory in my game.

I found some great answer here where Iter Ator provides equation to calculate actual velocity of body after time, provided time, starting velocity and linear dumping but no gravity. I was able to integrate this solution to get distance traveled in time equation. Problem is that this doesnt take gravity into account.

There is also this tutorial that has many information about predicting physics body trajectory but doesnt take linear dumping into account at all.

What I would love to have is combined single equation that covers both linear dumping AND gravity to predict Box2d physics body actual velocity/position but math is too hard for me :(.

Problem seems to be that physics body velocity is both accelerated by gravity and dumped at the same time.

All the equations are in a frame with the y axis vertical pointing upwards and the x axis horizontal pointing in the initial direction of the movement.
The position is noted $$\ (x;y) \$$, the speed is $$\ (\dot{x};\dot{y}) \$$ and their initial values at $$\ t = 0 \$$ are respectively $$\ (x_0;y_0) \$$ and $$\ (\dot{x}_0;\dot{y}_0) \$$.

Your projectile undergoes the action of two forces:

• its own weight, always oriented towards the ground: $$\ \vec{W} = -mg. \vec{y} \$$
• the friction from the air (drag), opposing the direction of motion and assumed linear: $$\ \vec{F} = -k. \vec{v} \$$

Newton's second law gives us:
$$\ m \ddot{x} = -k \dot{x} \$$ (1)
$$\ m \ddot{y} = - k \dot{y} \ - \ mg \$$ (2)

(1) can be integrated once to give a first order ODE that we can solve:
$$\ m \dot{x} = -kx \ + \ D \Rightarrow \dot{x} = -\frac{k}{m} x \ + \ \frac{D}{m}\$$ where $$\ D \$$ is the integration constant. $$\ x(t) = C e^{-\frac{k}{m}t} + \frac{D}{k} \$$ with $$\ C = -\frac{m}{k} \dot{x}_0 \$$ and $$\ D = k x_0 + m \dot{x}_0 \$$

(2) In is a second order non-homogeneous ODE, skipping some steps, the solution is:
$$\ y(t) = y_0 \ + \ \frac{m}{k} \dot{y}_0 \ - \ \frac{m}{k} \dot{y}_0 e^{-\frac{k}{m} t} - mgt \$$

You can get the velocity equations by derivating the position equations.
I've only included very minimal details, I can expand some steps if needed.

• Thank you for this. I have some questions on how do I use those equations and how to understand it. 1. Why is mass included? From what I observed working with box2d, mass of object doesnt influence anything in order of drag or gravity...two objects with different mass would move exactly the same way in terms of gravity and drag. 2. How do I include initial velocity of physics body as this is suppposed to be for predicting shots trajectories in game. Feb 26, 2021 at 5:57
• 1. The mass does not affect gravity but it does affect drag. The drag is proportional to the speed of the projectile. To get the acceleration due to it, you have to divide by the mass. For gravity, it is constant and equals to mg so dividing by the mass cancels it out. 2. The initial position is (x0;y0) and the initial speed is (x0 dot;y0 dot) in the formulas above. I'm adding a list of variables used in my answer. Feb 26, 2021 at 8:15
• I don't see a mass term in the expression for Box2D linear damping explained in the answer linked in the question above. Box2D's approximation might not be identical to the drag you're used to modelling. Feb 26, 2021 at 12:46
• @DMGregory They used a single abstract constant that has to be set directly by the designer, so one has to directly input the value of k/m into b->m_linearDamping (assuming constant mass as I did in the answer). I've found this to support it github.com/erincatto/Box2D/issues/454#issuecomment-303008391 . I'm not familiar with Box2D at all so please correct me if I'm understanding this wrong. Feb 26, 2021 at 12:59
• @Sacha Yes, there is only one drag constant ("linear damping") and it applies on all bodies regardless of their mass...so do I just calculate k as k = b*m and use equations as they are? What about initial velocity body has? Is it included? Feb 26, 2021 at 18:42