Projectiles

 < Day Day Up > 

Projectiles

One of the easiest types of motion to spot in a game is projectile motion. Any object that has been thrown, kicked, or shot and that is flying through the air is considered a projectile . Think about the last time you threw a baseball or kicked a soccer ball. What type of path did the ball follow? That's rightit followed a parabolic path! You might want to flip back to Chapter 2, "Geometry Snippets," for a quick review of the parabola. Check out the parabola that opens downward. Doesn't it look like the trajectory of a missile that's just been shot at an angle? Any first-person or third-person shooter will have tons of projectiles, so how do we model them mathematically?

The easiest way to approach a 2D projectile is to break all the vector quantities into components , as you did in the preceding section, and then separate the horizontal components from the vertical ones. The components are completely independent of each other, so mathematically you can treat them separately. If you don't believe me, try this simple experiment. Get two identical balls or pencils, and find a table with a nice smooth surface. Roll one ball off the edge of the table, and watch it fall to the floor in a parabolic path. Now repeat the process, but this time drop the other ball from the same height as the table at the exact same time that the first ball reaches the edge of the table. Do they both hit the floor at the same time? They should. This tells you that the horizontal motion of the first ball does not affect its vertical motion. Both balls fall vertically in the same way. Figure 10.5 shows what happens from the side view.

Figure 10.5. Ball experiment side view.

graphics/10fig05.gif

If you watch the experiment from the front view, where you have no depth perception, the motion of both balls appears identical, as shown in Figure 10.6.

Figure 10.6. Ball experiment front view.

graphics/10fig06.gif

Now that we have established that vertical motion is completely independent of horizontal motion, let's address them separately. The best part of separating them is that we're back to 1D motion, which means just dealing with positive and negative numbers . Let's look at vertical motion first.

If we look at just the vertical components of the displacement, velocity, and acceleration vectors, we're really back to vertical 1D motion, which was discussed in Chapter 8. Looking at the displacement, the object goes up to a maximum height and then falls back down. The velocity starts out positive, decreases to 0 at the maximum height, and then speeds up in the negative direction as it falls down. This, of course, is caused by negative acceleration due to gravity (9.8m/s 2 ). Because we're back to 1D motion with constant acceleration, we can use the five equations of motion discussed in Chapter 8.

Vertical Components of a Projectile

a y = 9.8m/s 2

v fy = v iy + at

D y = v iy t + a y t 2


Example 10.6: Vertical Components of a Projectile

Suppose you're coding a game like Spyro the Dragon where the player climbs onto a cannon, and it launches cannon balls at a speed of 20m/s at a 30 angle. How much time will it take a cannon ball to come back down to the ground?

Solution
  1. The cannon ball's initial velocity is not straight up in the air. It's at an angle, so you need to find the vertical component. Remember that the vertical component is equal to the overall magnitude times the angle's sine:

    v iy = 20m/s(sin30) = 10m/s

  2. You're back to 1D vertical motion. Find the time it takes to get to the maximum height, and then double it for the total time up and down. List what you know and what you're trying to find:

    Given

    Find

    v i = 10m/s

    t = ?

    v f = 0m/s

     

    a = 9.8m/s 2

     
  3. Choose an equation based on the list. In this case, the first equation ( v f = v i + at ) will work:

    v f = v i + at

    0m/s = 10m/s + (9.8m/s 2 ) t

    10m/s = (9.8m/s 2 ) t

    t = 1.02s

  4. Remember that this is only the time up to the maximum height. The total time up and back down is twice that, so t = 2.04s.

Now let's turn our attention to the horizontal components of the projectile's vector quantities. The horizontal components are even easier to work with, because as soon as a projectile is airborne , it does not speed up or slow down in the horizontal direction (if we ignore air resistance). This means that the horizontal component of the acceleration is always 0. In other words, the horizontal velocity does not change; it stays constant. And that constant velocity is equal to the horizontal displacement divided by time. Again, we're back to 1D motion with constant velocity, so we can use the five equations of motion just like we did in Chapter 8. What's even nicer about the projectile is that the horizontal acceleration is 0, and if you plug that into the equations of motion, they all reduce to one equation: v x = D x / t . That's the only equation you need to worry about for the horizontal components.

Horizontal Components of a Projectile

a x = 0m/s 2

v x = D x / t


Example 10.7: Horizontal Components of a Projectile

Once again, you're coding a game like Spyro the Dragon where he's climbed onto a cannon, and it launches water balloons at a speed of 20m/s at a 30 angle. If the edge of the screen is 30 meters away from the launch, how much time will it take the cannon ball to fly out of view?

Solution
  1. The cannon ball's initial velocity is at an angle, so you need to find the horizontal component. Remember that the horizontal component is equal to the magnitude times the angle's cosine:

    v iy = 20m/s(cos30) = 17.32m/s

  2. Now you're back to 1D horizontal motion. Make a list of what you know and what you're looking for:

    Given

    Find

    v = 17.32m/s

    t = ?

    D x = 30m/s

     

    a = 0m/s 2

     
  3. Because this is a projectile, the acceleration is 0, which means that the velocity stays constant. You need to worry about only one equation, so use it to find the time:

    v = D x / t

    17.32m/s = 30m/ t

    t = 30m/(17.32m/s)

    t = 1.73s

The cannon ball will go off the edge of the screen 1.73 seconds after it has been launched.

Now let's put the two pieces together. Earlier I said that all the projectile's vector quantities can be broken into components and then separated. You've been working with one more quantity that's not a vector. Time is a scalar quantity, so it has no direction. This means that time is the one element that can bridge the horizontal and vertical parts of projectile motion. For example, the water balloon you looked at in the last two examples had both horizontal and vertical components to its motion. You found in Example 10.6 that it took 2.04 seconds for the water balloon to go up and back down to the ground. That's the same amount of time it takes the water balloon to travel horizontally before it hits the ground. You can use the same 2.04 seconds to calculate how far away the balloon will hit the ground. That takes you to the horizontal information, so make a list:

Given

Find

v = 17.32m/s

D x = ?

t = 2.04s

 

a = 0m/s 2

 

Because the acceleration is 0, you can use the equation v = D x / t to find the horizontal displacement:

v = D x / t

17.32m/s = D x /2.04s

D x = 35.33m

The trick here is that it takes 2.04 seconds to go up and back down, so it also takes 2.04 seconds to travel 35.33 meters horizontally before hitting the ground.

Let's look at a few examples where this comes in handy. In these examples, you'll see that I place the horizontal and vertical components on opposite sides of the page. You also might want to consider sketching the motion to help organize all the numbers.

Example 10.8: Falling from a Known Height

Suppose you're making a 2D game like Joust with platforms at various heights. One platform is 8 meters above the ground. If the player (on his ostrich) runs off the ledge at 10m/s without jumping, how far away from the edge will he land on the ground?

Solution
  1. Start by sketching the motion. This is done in Figure 10.7.

    Figure 10.7. The player runs off the ledge.

    graphics/10fig07.gif

  2. Break all the vectors into components, and separate the horizontal from the vertical in two lists:

    Horizontal

    Vertical

    Given

    Find

    Given

    Find

    v x = 10m/s

    D x = ?

    v iy = 0m/s

     

    a x = 0m/s 2

     

    a y = 9.8m/s 2

     
       

    D y = 8m

     

    Because the player does not jump, his initial velocity is completely horizontal, so all 10m/s goes to the horizontal component, and the vertical initial velocity is 0m/s. Also notice that D y is negative because the player is falling.

  3. You're looking for horizontal displacement, so start with the horizontal list. Because the acceleration is 0, you have only one equation to worry about v x = D x / t . Unfortunately, you can't use it until you know the time. The good news is that time is neither horizontal nor vertical, so it is the same value in both lists. This means that if you can find out how long it takes the player to fall, you can use the same time to calculate the horizontal displacement.

  4. Go to the vertical list and see if you can find the time. You're right back to vertical 1D motion, and you have a list, so choose the appropriate equation:

    D y = v iy t + a y t 2

    8m = (0m/s) t + (9.8m/s 2 ) t 2

    8m = (9.8m/s 2 ) t 2

    t 2 = 1.633s 2

    t = 1.28s

  5. Now revisit the lists. You've found the time it takes to fall to the ground. You can also use that for the horizontal time:

    Horizontal

    Vertical

    Given

    Find

    Given

    Find

    v x = 10m/s

    D x = ?

    v iy = 0m/s

    t = 1.28s

    a x = 0m/s 2

     

    a y = 9.8m/s 2

     

    t = 1.28s

     

    D y = 8m

     
  6. Now you have enough information on the horizontal side to calculate D x :

    v x = D x / t

    10m/s = D x /1.28s

    D x = 12.8m

The player lands 12.8 meters from the edge of the platform.

In this example, the player's initial velocity is completely horizontal. Let's look at the same situation but with an initial velocity at an angle.

Generally in a game, we don't calculate where a player is going to land or strike another object unless it is vital to some form of prediction code we're implementing. In the scenario previously described, once the player leaves the platform, in every frame we would apply gravity to his motion while also adjusting his position accordingly until we detected a collision with either the ground or another platform. Let's take a look at how this works. The player's position and his current direction of movement could be stored in arrays of two floats. Let's assume a screen coordinate of (200, 200) for the player and a velocity at 10 pixels/second:

 float player_pos[2] = { 200, 200 }; float player_motion[2] = { 10, 0 }; 

In every frame, we would call a function to update the player's position, as follows :

 // purpose: to update the player's position // input:   none // output:  the updated position void updatePlayerPos() {     for(int i = 0; i < 2; ++i)         player_pos[i] += player_motion[i]; } 

We would also need to call a function to update the player's motion. In the game of Joust , horizontal motion would be affected by things like player input and collisions with objects, but in this instance, we're concerned more with the vertical motion, which is affected by gravity until it is affected by a collision. So in every frame, we would need to check to see if there was something underneath the player, and if not, we would need to allow gravity to affect his motion:

 // purpose: to update the player's motion if there is nothing under him // input:   accel - the amount of constant vertical acceleration per second //           fps - our frames per second, in other words our time interval // output:  update player_motion[1] (vertical velocity) void updatePlayerVertMotion(float accel, float fps) {     if(/*check to see if there is nothing underneath the player*/)     {         player_motion[1] = player_motion[1] + accel * fps;     } } 

This same code could be used to solve the following problem as well, the only difference being that the player would begin the scenario with a negative initial velocity in the Y direction. You may be confused by this statement, because generally in this book a negative y-component for velocity represents downward motion, but keep in mind that in a game the Y axis decrements in the upward direction, so negative velocity in the vertical would indicate upwards travel.

Example 10.9: Jumping from a Known Height

Suppose you're making the same Joust-like game with platforms at various heights. One platform is 8 meters above the ground. If the player (on his ostrich) stands at the ledge and jumps off with an initial speed of 10m/s at a 30 angle, how far from the edge will he land on the ground?

Solution
  1. Start by sketching the motion. This is done in Figure 10.8.

    Figure 10.8. The player jumps off the ledge.

    graphics/10fig08.gif

  2. Break all the vectors into components, and separate the horizontal from the vertical in two lists:

    Horizontal

    Vertical

    Given

    Find

    Given

    Find

    v x = 10cos30

         

    = 8.66m/s

    D x = ?

    v iy = 10sin30

     

    = 5m/s

         

    a x = 0m/s 2

     

    a y = 9.8m/s 2

     
       

    D y = 8m

     

    Notice that D y is still 8m even though the player jumps up first. Remember that D y is displacement, not the actual distance.

  3. You're looking for horizontal displacement, so start with the horizontal list. Because the acceleration is 0, you have only one equation to worry about v x = D x / t . Unfortunately, you can't use it until you know the time. The good news is that time is neither horizontal nor vertical, so it is the same value in both lists. This means that if you can find out how long it takes the player to jump and fall, you can use the same time to calculate the horizontal displacement.

  4. Go to the vertical list, and see if you can find the time. You're right back to vertical 1D motion, and you have a list, so choose the appropriate equation:

    D y = v iy t + a y t 2

    8m = (5m/s) t + (9.8m/s 2 ) t 2

  5. You can use the quadratic formula here, or you can bypass this by finding v fy and then use a different equation for time. Because the quadratic formula has a square root (which is an expensive operation) and it's difficult to remember, use the second option:

    graphics/10equ03.gif


    (Remember that you want the negative root because the player is falling when he hits the ground.)

  6. Use v fy in a different equation to find the time:

    v fy = v iy + a y t

    13.48m/s = 5m/s + (9.8m/s 2 ) t

    t = 1.89s

  7. Now revisit the lists. You've found the time it takes to jump and fall to the ground. You can also use that for the horizontal time:

    Horizontal

    Vertical

    Given

    Find

    Given

    Find

    v x = 10cos30

         

    = 8.66m/s

    D x = ?

    v iy = 10sin30

     

    = 5m/s

     

    v fy = 13.48m/s

     

    a x = 0m/s 2

     

    a y = 9.8m/s 2

    t = 1.89s

    t = 1.89s

     

    D y = 8m

     
  8. Now you have enough information on the horizontal side to calculate D x :

    v x = D x / t

    8.66m/s = D x /1.89s

    D x = 16.33m

This time, the player lands 16.33 meters from the edge of the platform.

In Examples 10.8 and 10.9, you were ultimately looking for the horizontal displacement. That won't always be the case. Let's look at one more example where you try to find the vertical displacement.

Example 10.10: Shooting Paintballs

Suppose the character in your game is shooting paintballs at the side of a building 20 meters from where he's lying on the ground. If the pellets leave the gun with an initial speed of 25m/s, and the character is aiming at a 40 angle, how high above the ground will the splattered paint appear on the wall?

Solution
  1. Start by sketching the motion. This is done in Figure 10.9.

    Figure 10.9. Shooting paintballs.

    graphics/10fig09.gif

  2. Break all vectors into components, and separate the horizontal from the vertical in two lists:

    Horizontal

    Vertical

    Given

    Find

    Given

    Find

    v x = 25cos40

         

    = 19.15m/s

     

    v iy = 25sin40

     

    = 16.07m/s

    D y = ?

       

    a x = 0m/s 2

     

    a y = 9.8m/s 2

     

    D x = 20m

         
  3. You're looking for vertical displacement, so start with the vertical list. Unfortunately, you don't have enough information to use any of the equations of motion yet. However, because time has no direction, it is the same value in both lists. This means that if you can find out how long it takes the paintball to travel 20m toward the building, you can use the same time to calculate the vertical displacement.

  4. Go to the horizontal list and see if you can find the time. Because a = 0, there's only one equation:

    v x = D x / t

    19.15m/s = 20m/ t

    t = 20m/(19.15m/s)

    t = 1.04s

  5. Now revisit the lists. You've found the time it takes to hit the wall. You can also use that for the vertical time:

    Horizontal

    Vertical

    Given

    Find

    Given

    Find

    v x = 25cos40

         

    = 19.15m/s

    t = 1.04s

    v iy = 25sin40

     

    = 16.07m/s

       

    D y = ?

    a x = 0m/s 2

     

    a y = 9.8m/s 2

     

    D x = 20m

     

    t = 1.04s

     
  6. Now you have enough information on the vertical side to calculate D y :

    D y = v iy t + a y t 2

    D y = (16.07m/s)(1.04s) + (9.8m/s 2 )(1.04s) 2

    D y = 11.48m

    So the paintball will go splat on the wall at a height of 11.48 meters.

NOTE

All these examples began with all metric units. Be careful if you estimate initial values in different units. Always convert all values to the same units before plugging anything into an equation.


All the examples we've looked at have been flat on a 2D screen. You might be wondering how this translates to 3D. Well, it works the same way. A parabola is a 2D shape, so ultimately any projectile moves in a flat 2D planeit just might not be the xy plane. The math still works. Just think of the components as horizontal and vertical, not necessarily parallel to the x- and y-axes. In the end, any horizontal component you find might need to be rotated in the xz plane, but the vertical components will always stay the same.

Self-Assessment

1.

A grenade is thrown from the top of a cliff at an angle of 25 with an initial speed of 15m/s. After 3 seconds in flight, how far will it have fallen below the ledge?

2.

Tom, the cat, is chasing Jerr,y the mouse, across the kitchen table, which is 1.5m high. At the last second, Jerry steps out of the way and lets Tom slide off the edge of the table going 5m/s. How far from the edge of the table will Tom land?

3.

An enemy in your game stands on the edge of a rooftop 10m above the ground and throws a boulder with an initial velocity of 15m/s @ 37. How far from the building would the player have to be standing to get hit by the boulder?

4.

Your game character has set up a slingshot on the ground that fires eggs with an initial speed of 15m/s at a 37 angle. If a building is positioned 16 meters away from the slingshot, how high on the wall will the egg go splat?

5.

If a quarterback throws a football downfield with an initial velocity of 20m/s at 35, how far away should a receiver position himself to catch the ball?


 < Day Day Up > 


Beginning Math and Physics for Game Programmers
Beginning Math and Physics for Game Programmers
ISBN: 0735713901
EAN: 2147483647
Year: 2004
Pages: 143
Authors: Wendy Stahler

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net