Tricks of the Windows Game Programming Gurus:Playing God: Basic Physics Modeling

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Modeling Projectile Trajectories

Falling objects are fairly exciting, but let’s see if we can do something a little more appropriate for video game programming! How about computing trajectory paths? Take a look at Figure 13.15, which shows the general setup for the problem. We have a ground plane, call it y=0, and a tank located at x=0, y=0, with a barrel pointed at an angle of inclination θ (theta) with the x-axis. The question is, if we fire a projectile with mass m at a velocity vI, what will happen?

Figure 13.15 The trajectory problem.

We can solve the problem by breaking it up into its x,y components. First, let’s break the velocity into an (x,y) vector:

    Vix = V*cos θ

    Viy = V*sin θ

Trust me.

Okay, now forget about the x part for a minute, and think about the problem. The projectile is going to go up and down and hit the ground. How long will this take? Take a look at our previous gravity equations:

    V(t) = v0 + 9.8 m/s2*t.

The position for the y-axis is

    y(t) = y0 + v0*t + 1/2 * 9.8m/s2 * t2.

The first one tells us the velocity relative to time. That’s what we need. We know that when the projectile reaches its maximum height, the velocity will be equal to 0. Furthermore, the amount of time that the projectile takes to reach this height will be the same amount of time it takes to fall to the ground again. Take a look at Figure 13.15. Plugging in our values for initial y velocity of our projectile and solving for time t, we have

    Vy(t) = Viy - 9.8 m/s2*t

Math Note that I replaced the 9.8 value of acceleration with a. I did this to re-enforce that the acceleration is just a number, and you can make it whatever you wish.

Note that I flipped the sign of the acceleration due to gravity because down is negative and matters in this case, and in general, when the velocity equals 0:

    0 = V*sin θ - a*t (a is just the acceleration)

Solving for time t, we get

    t = Viy * (sin θ)/a

Now the total time of flight is just time up + time down which equals t+t=2*t since the projectile must go up, then down. Therefore, we can revisit the x component now. We know that the total flight time is 2*t and we can compute t from (Viy * (sin θ)/a). Therefore, the distance that the projectile travels in the x-axis is just:

    X(t) = vix*t

Plugging in our values, this results in

    xhit = (V*cos θ) * (V*(sin θ)/a)

or

    xhit = Vix * Viy/a

Neat, huh?

That’s the physics behind everything, but how do you model it in a program? Well, all you do is apply constant x-axis velocity to the projectile and gravity in the y-axis and test for when the projectile hits the ground or something else. Of course, in real life the X and Y velocities would diminish due to air resistance, but throwing that out the algorithm I just described works great. Here’s the code to do it:

    // Inputs

    float x_pos      = 0, // starting point of projectile

          y_pos      = SCREEN_BOTTOM, // bottom of screen

          y_velocity = 0, // initial y velocity

          x_velocity = 0, // constant x velocity

          gravity    = 1, // do want to fall too fast

          velocity   = INITIAL_VEL, // whatever

          angle      = INITIAL_ANGLE; // whatever, must be in radians

    // compute velocities in x,y

    x_velocity = velocity*cos(angle);

    y_velocity = velocity*sin(angle);

    // do projectile loop until object hits

    // bottom of screen at SCREEN_BOTTOM

    while(y_pos < SCREEN_BOTTOM)

         {

         // update position

         x_pos+=x_velocity;

         y_pos+=y_velocity;

         // update velocity

         y_velocity+=gravity;

         } // end while

That’s all there is to it! If you want to add a wind force, just model it as a small acceleration in the direction opposing the X-motion, and assume that the wind force creates a constant acceleration against the projectile. As a result, you simply need to add this line of code in the projectile loop:

    x_velocity-=wind_factor;

Where `wind_factor` would be something like 0.01—something fairly small.

As a demo of all this trajectory stuff, check out `DEMO13_4.CPP|EXE` on the CD. A screen shot is shown in Figure 13.16. The demo allows you to aim a virtual cannon and fire a projectile.

Figure 13.16 The projectile demo.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.