Planning ballistic trajectories with air resistance for games

March 26, 2015 Math, Physics
Ballistic trajectories

Having enemies throw grenades or launch projectiles in games often requires some form of trajectory planning in order to find the best launch moment, speed and angle. When trying to take drag and wind into account as well, this process quickly becomes complex and inefficient.

The research paper below proposes a novel and practical approximation of 2D and 3D trajectories with drag and wind, which allows for very efficient planning.

Analytical Ballistic Trajectories with Approximately Linear Drag”, Giliam J. P. de Carpentier, International Journal of Computer Games Technology, vol. 2014, Article ID 463489, 13 pages, 2014. doi:10.1155/2014/463489. [PDF] [Additional resources]

This paper includes formulas and code snippets that can be used to easily calculate the initial velocity to hit any target given the initial position, the target position, the gravity, wind and drag constants, and

  • the time to target, or
  • another position to pass through (or to shoot exactly over an obstacle, for example), or
  • the top height, or
  • the launch slope, or
  • the slope to hit the target at, or
  • the arc height, or
  • the exact launch speed, or
  • with the least amount of speed possible (the way humans typically like to do it)

Unity3D implementation

To demonstrate all this in practice, I've made a free and open-source demo project for Unity3D 3.5 and up. It exposes all the above ways of plannings through composable planner behaviours attached to automatic turrets that can shoot at static targets, moving targets, and (experimentally) even over polygonal obstacles. The source is available as the BitBucket GIT project You can run it in your web browser using the Web Player [Open] or download the standalone Windows version [ZIP].

Screenshot from the Unity3d demo project

Screenshot from the Unity3d demo project


Additional resources

  • The paper's two C++ algorithms but with improved spacing and color coding [PDF]
  • The paper's Figure 4 as an animated GIF [GIF]

Comments (7)

February 2, 2014

You did a really good job on the paper. Every scenario that one would need is clearly explained and accompanied with source code. Best of all, its written in C++.

My only dislike is probably due to my opinion so take it how every you see fit. But, when reading the source code in the paper is a little difficult due to there not being any spacing between comments and code.

February 2, 2014

Thanks! Spacing and color coding in the algorithms sadly got 'lost in translation' in the publishing process. I've added a link in the Additional Resources to a PDF containing the same code in the much more readable original layout for your convenience.

February 3, 2014

Thank you for releasing an easier to read version. Best of luck to all your future endeavors! ^_^

June 3, 2014

Great work! I'm also working on a similar project in Unity3D and I would really appreciate if you could share the project.

March 26, 2015

Hi, Thilina (and others). It took me a bit longer than expected, but the Unity3D project is finally publicly available. Enjoy.

March 30, 2015

Hi Giliam,

Congrats on your paper and execution. Does your model account for curved earth? Unity's terrain system is flat space. How do you reconcile curvature of the earth for your ballistic model in Unity and, is there any degradation in accuracy over distances greater than 2km?

March 31, 2015

Hi Davey. Thanks! The short answer: I don't. The long answer: The model assumes a 2D or 3D coordinate system with a constant gravity vector. Over distances most games cover (10-1000 meter or so), this constant gravity acceleration vector makes sense. For flight distances that cover much more substantial distances, the gravity vector on earth would both slowly rotate and (for most trajectories) change in magnitude during flight. Furthermore, the model doesn't take into account other potential relevant factors like the Coriolis effect of the earth, almost-but-not-exactly quadratic drag, the projectile orientation, gyroscopic effects of the projectile, height-based pressure changes, unstable wind and lift fluctuations, and probably some other stuff that you would need to get a truly accurate trajectory. Consequently, this means that probably most other analytic models are off the table as well, as are probably most game physics simulators. So whether or not a model like this is good enough depends on your exact requirements for accuracy. It is, however, very precise and stable. That is, it should be possible to hit a mark at many kilometers because it doesn't use a simulation but calculates an arc the trajectory will follow exactly once launched (because both the planning and the in-flight execution are driven by the exact same function). Lastly, if your question is interpreted as not being about the physical accuracy but about the effect having projectiles go 'past' the horizon and have trajectories that are (in a way) curved along with the earth (but are still far from orbital trajectories), i think it could be worth considering modifying the code a bit to approximate that effect somewhat. For example, the math core of this project calculates all the trajectories in a particular 2D frame of reference. Most 3D functions simple wrap and map these 2D functions. Consequently, you should be able to use these 2D functions and interpret them as set of a (rotated) polar coordinates, conceptually interpreting x as an angle and y as distance to the center. Again, it's not completely physically accurate, but it might worth considering. I hope this answers your question. And if you need more information, let me know. Good luck!

Leave a comment