Vector Versus Scalar

 < Day Day Up > 

Whenever you introduce a quantity in your game, you must distinguish whether it's a vector quantity or a scalar quantity. The difference between the two lies in the direction.

You've been working with scalar quantities since you learned to count. A scalar is just a number; sometimes it's called a magnitude without any direction. A vector quantity has both magnitude and direction. For example, suppose your friend is having car trouble, and he calls and asks you to come pick him up because he's only 2 miles away. You say no problem and hop in your car. But how will you find him? If he had said 2 miles due east on your street, you might have had a better chance of locating him. That's the difference between vectors and scalars. The scalar version of 2 miles doesn't give you enough information. The vector version of 2 miles due east gives you the direction you need to find your buddy. The direction makes all the difference in the world. So a scalar quantity is just a number, or a magnitude. A vector quantity is a magnitude with direction included. This might seem like a minor distinction, but it makes a world of difference when you start simulating motion.

Scalar = magnitude only

Vector = magnitude + direction.


In code, scalars can be stored in any number of variable types designed to hold numbers , depending on the range of values which need to be stored. Anything from a char, which can hold values from 128 and 127, to an unsigned long, which can hold values between 0 and 18,446,744,073,709,551,616 (or 2 to the power of 64), to a double, which can hold values between +/-1.7E +/-308 (up to 15 digits). There are, however, no built-in data types designed specifically for storing vectors, although 3D API's like OpenGL and Direct3D each have a standard which they adhere to. Therefore, vectors can be stored in code as either an array of floats or as a user -defined data type:

 // An array of 3 floats is one way to store a vector, i j and k float 3Dvector[3] = { 0, 0, 0 }; // A user-defined data type is another struct 3Dvector { float x, y, z; }; 

By using a data type, it is possible to create an extremely powerful structure or class which can encompass almost every operation which could be needed. This will be covered in more detail in further chapters.

When you're programming, always be sure to include the direction whenever you're dealing with an object in motion. Some quantities, such as time and points, can't have a direction, so a scalar number is fine. However, quantities such as displacement, velocity, and force describe an object in motion, so always include the direction.

You might not be familiar with the terms displacement and velocity . Displacement is the vector version of distance, and velocity is the vector version of speed. For example, 55mph is just a scalar, so we call it speed. However, 55mph due east is a vector, so we call it velocity. These two quantities are discussed in greater detail in Chapter 8, "Motion in One Dimension."

The trickiest part of working with vectors is dealing with the direction. In one dimension there are only two possible directions, so positive or negative can be used to indicate which one. For example, if all an object can do is move left or right on the screen, positive numbers indicate to the right, and negative numbers indicate to the left. Similarly, we often use positive numbers for up and negative numbers for down when dealing with vertical motion.

NOTE

Chapter 1, "Points and Lines," discussed the different coordinate systems for 3D. In this book, up, right, and out from the screen are the positive directions, and down, left, and back behind the screen are the negative directions. Be sure to pay close attention to the coordinate system defined by the engine you are working with.


Example 4.1: Pong : Positive Displacement

Suppose you're programming the motion of the paddle in a Pong game. The paddle can only move up and down. If the center of the paddle starts at (20,50) and moves to (20,400), what is its displacement?

Solution

Looking at the y-coordinates , you can see that the paddle moves from the 50-pixel mark to the 400-pixel mark. Therefore, its displacement must be 350 pixels.

Example 4.2: Pong : Negative Displacement

Again, you're programming the motion of the paddle in a Pong game. The paddle can only move up and down. If the center of the paddle starts at (20,400) and moves to (20,50), what is its displacement?

Solution

Looking at the y-coordinates, you can see that this time the paddle moves from the 400-pixel mark to the 50-pixel mark. Therefore, its displacement must be 350 pixels.

NOTE

Notice that, in one dimension, positive or negative is all you need to indicate the direction. Example 4.1 had a positive displacement, but when you flipped the positions in Example 4.2, the displacement became negative.


When dealing with displacement, direction is very important. There's a very big difference between distance (the scalar) and displacement (the vector). When calculating displacement, all you care about is where the object starts and where it ends. Whatever happens in between doesn't matter. Football is a great example of displacement versus distance. Suppose your receiver catches the football on the 20-yard line and starts running. There's a blocker in the way, so the receiver circles around the blocker, avoids the other defender running toward him, and eventually gets tackled on the 50-yard line.

If you look at his path mapped out in Figure 4.1, you can see that he runs much farther than 30 yards. However, as far as you're concerned for the game, he gained 30 yards for the team. The positive 30 yards is his displacement even though the actual distance traveled is much more. If you ignored direction completely, you'd be concerned with the scalar version, distance. That is the critical difference between distance and displacement.

Figure 4.1. A football player's displacement.

graphics/04fig01.jpg

Displacement

Displacement = final position initial position

D x = x f x i .


NOTE

Remember that, when calculating displacement, where direction matters, all you need to know is the starting point and the ending point. Only for the scalar version of distance do you care what happens in between.


Example 4.3: Distance Versus Displacement

This time, you're playing a simple version of Mario Brothers where all he can do is move left and right (he can't jump yet). Suppose Mario starts out with a horizontal position of 200 pixels. He starts to move to the right, but at the 250-pixel mark he realizes he missed a mushroom, so he backs up to the 100-pixel mark to get the mushroom. Then he runs forward to meet the princess at the 450-pixel mark. What's his overall displacement, and what's his actual distance traveled?

Solution
  1. Calculate displacement first. Mario starts out at the 200-pixel mark and ends up at the 450-pixel mark. Overall, his displacement is final position initial position = 400 pixels 200 pixels = 250 pixels.

  2. You can ignore direction when calculating distance, so add up the length of each segment of Mario's run. From 200 pixels to 250 pixels is 50 pixels. From there he goes another 150 pixels to the mushroom, so that's 200 pixels so far. Then he runs 350 pixels toward the princess, so that's 550 pixels total distance traveled.

Notice that distance and displacement are quite often very different values, as they were with Mario. Also, notice that you lose all the information about what happens in the middle when all you look at is displacement. You might want to consider breaking the motion into smaller time intervals so that you don't miss anything. Looking back at Mario, you might want to consider separating that scenario into three segments. Remember that, when coding, you need to use displacement to maintain the direction, so you might want to work with three segments: +50 pixels, 150 pixels to the mushroom, and +350 pixels to the princess.

NOTE

If you choose to program a game in real time, your time intervals will hopefully be close to 1/30 of a second, so you won't risk losing much information between frames .


Scalars are no big deal, because you've been working with them all your life. However, this vector idea might be new, and the trick lies in dealing with the direction. Motion in one dimension is easy, because positive or negative can indicate the direction of vector quantities. The rest of this chapter is dedicated to dealing with vectors in 2D and 3D, which are a little more complicated to work with.

Self-Assessment

1.

What's the difference between a vector and a scalar quantity?

2.

Is "65 feet" an example of a vector or a scalar?

3.

Is "35 seconds" an example of a vector or a scalar?

4.

If a runner on a straight track starts at the 5-ft. marker and stops at the 65-ft. marker, what's his displacement?

5.

If a confused runner on a straight track starts at the 65-ft. marker and stops at the 5-ft. marker, what's his displacement?

6.

Suppose you're playing Pong , and the paddle starts with a y-coordinate of 250 pixels. You move it down to the 100-pixel mark to hit the ball. Then you move up 300 pixels in anticipation of the next hit. As the ball approaches, you realize you're a little off, so you move down 20 pixels. What's the paddle's overall displacement?

7.

What is the actual distance that the paddle in question 6 travels ?

8.

In a baseball game, a player gets caught in a rundown when trying to steal second. After running back and forth, he ends up safely back on first base. What's his overall displacement for that play?


 < 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