Monday, December 10, 2012

Game AI - Predicting Physics

     Sports, Shooters, Platformers, Adventure, Racing, Arcade, and even games like pong all has some sort of physics in them no matter how simple they may be. These Physics effect objects in the game that directly impact how AI should act in different situations.  Learning to use Physics can help make it so your AI goes from a clueless loser to a competitive challenge.  But I'm not talking about just using Physics to make a game look and feel more realistic, what I'm here to talk about is using our knowledge of in game Physics to allow our AI's to predict the correct outcome based on the world around it, this way our AI looks like it is actually making intelligent decisions.  

     Us humans make judgement based on physics all the time, for example when you are playing catch, when you  go to throw the ball you don't sit there and take the time to accurately calculate how hard to throw the ball and at what angle you should throw it, you have a general feel and understanding that you have picked up just from living in this world of physics everyday.  While the AI in our games don't the those "gut feelings" about how hard to throw a ball, they have the possibility of something I would consider much better, Cold, Hard, Math.  The AI is built on a computer and there is nothing a computer is better at then solving equations, with the correct equations at its side and precise data from the world around it, our AI's have the possibility to never miss a catch, have the perfect shot, and always dodge the projectile. Very quickly you can see just how powerful this type of information can be for an AI. (2)

     This is where the fun starts, the math behind the physics, and accuracy vs efficiency when and what trades off you should make based on the game. Predicting Physics isn't an all or nothing system,  you can tailor how much accuracy you want/need in your game, because in all honestly perfect accuracy isn't always a good as it sounds.  Imaging playing a FPS game where whenever you came into range of an enemy who had you in line of sight you got head shot.  It would make the player very angry very quickly, so while we have the math to make this possible its definitely not the best route to take.  Projectile Math the most common for Games considering to take care of any projectile whether it be a bullet, snowball, football, or something of the likes. Projectile math takes the following variables Gravity(9.81 m/s ^2 on Earth, is usually doubled in games), Angle of Launch, Initial Velocity  initial height, time, and distance traveled. with them you can find any other one if it is unknown.  Considering we control the world in which this AI is running  we should have easy access to gravity, angle, velocity,height and time.  We would normally use this to find out where our projectile is going to land. (2)

For a 2D space (http://www.iforce2d.net/b2dtut/projected-trajectory) has a great tutorial explaining the math behind the magic.  When looking to find where our object will land we use the following equation  

 p(n) = po + nv + (n^2 + n)a/2


p(n) is the position on the nth time step
n is time step
p0 is the starting position at the 0th time step
v is the starting velocity per time step
a is the acceleration due to gravity per time step


Something to keep in mind when calculating where the projectile will land is not always what you want to use.  Using the last time point in the equation is where the bullets,ball,objects are landing.  It would be very terrible in a sport game like football if the AI always went to the point where the ball would hit its feet, it would look very strange catching the ball or miss it all together.  You want to make sure in situations like this as well as shooters that the projectiles end somewhere around chest height.  Obviously this is all based on the projectile being used and isn't a constant rule.(1)

The Equation above is a basic form of the projectile trajectory equation  and is the one most used by games it is recommended for Platforms that don't have the best computing power if you still want realish looking projectiles in that game.  The more worried about reality you are the more processing intensive it becomes.  The example above leaves out a few variables that are definitely present in the real world.  I speak of Drag and Rotation.  These projectiles like projectiles in most games pretend there is no resistances around them, I speak of Air resistance.  While  this is only a slight influences especially on things like bullet projectiles if you want reality  like for a sports game you are going to have to include it and at this point the math becomes much more complicated and processing intensive.  This also goes for rotation, most if not all projectile do rotate at least slightly which will add to lift causing the projects to go higher or farther, once again on low end platforms i would recommend leaving this out as well because the more variables you add the harder the math, the harder the math the more CPU intense it is for you AI to figure out every so many frames and react to it possibly causing the game to slow.(3)

A good example with Air Resistance
 http://phet.colorado.edu/sims/projectile-motion/projectile-motion_en.html

When worrying about air resistance you take the equation that we have above and where we apply the effects of gravity we take the air resistance force and add it with the acting gravity force.  This way the total acting force is now also including Air Resistance.  An example of which is shown here (http://www.riotstories.co.uk/science/projectile-motion-ii-air-resistance-linear-in-speed/)(3)

Just as these equations can become more complicated they can also be simplified for lower end platforms or if the game just doesn't care about looking real.  Below is an example of accurate prediction of shots if there is no negative force acting upon it.  No gravity or resistances,  see how the AI leads in front of the player when there is no obstruction.  This is because the AI is calculating with a similar version of the above formula if the shot will hit the target and when.  In this example the programmer added a slight error margin which allows the AI to not always hit its target, this makes it more playable for the player not always being hit but still making it feel like a challenge.  (2)


This is the core essence and purpose of predicting physics, it allows accurate "smart" seeming AI because they look like they actually know how to aim/throw/block.  They move in a timely matter and if the error margin is small enough it will cause quite a challenge.  (2)









Works Cited
  1. "Box2D Tutorials - Projected Trajectories." Box2D Tutorials - Projected Trajectories -                                                        Iforce2d. Box 2D, 6 Nov. 2011. Web. 11 Dec. 2012.
  2. Millington, Ian, and John David Funge. Artificial Intelligence for Games. Burlington, M          MA: Morgan Kaufmann/Elsevier, 2009. Print.
  3. Sinclair, Steve. "Riotstories." Riotstories. ITest Solutions Ltd, 13 Mar. 2012. Web. 11 Dec. 2012.

Hello World!

Nothing is more fitting for a first blog post of a programmer then the two first words we see printed out in every new language.