Previous Section  < Day Day Up >  Next Section

Dot Product

Remember when you first learned the multiplication tables? Your teacher probably told you that 2 times 3 could be written as 2 x 3 or as 2 • 3, and that they mean the same thing. This statement is true for scalar quantities. However, it's not true for more complex structures, such as vectors. We'll first look at 2 • 3, which is called the dot product. Then we'll investigate the cross product, 2 x 3, in the next section.

The most important thing to remember about the dot product of two vectors is that it always returns a scalar value. Let's first discuss how to calculate it, and then we'll talk about what that number represents.

NOTE

Some math texts call the dot product of two vectors the scalar product because that is what the dot product returns.


The Dot Product in 2D

A • B = a1b1 + a2b2

for any 2D vectors A = [a1 a2] and B = [b1 b2].


Notice that you take the product of the two x components and add it to the product of the two y components. This process results in a single number. You might have already guessed how to extend this process to 3D.

The Dot Product in 3D

A • B = a1b1 + a2b2 + a3b3

for any 3D vectors A = [a1 a2 a3] and B = [b1 b2 b3].


NOTE

The dot product can be performed on vectors of any dimension, not just 2D and 3D. You'll see an example of higher dimensions in Chapter 5, "Matrix Operations," which looks at multiplying matrices. Even though there might not be a physical representation of 4D or even 10D vectors, the only restriction on the dot product is that both vectors must be the same size.


The dot product is a very powerful vector operation; there's a lot of information embedded in that single number. The first point of note is that it gives you information about the angle between the two vectors (if they are 2D or 3D).

Perpendicular Check

If A • B = 0, A B.


NOTE

The symbol might be unfamiliar; it's the symbol for perpendicular. So if A • B = 0, A is perpendicular to B.


This is a very efficient way to check for perpendicular lines. For 2D vectors, it requires only two multiplications and one addition. For 3D vectors it requires three multiplications and two additions and then a check to see if it's equal to 0. If the dot product is not equal to 0, its sign (positive or negative) provides helpful information.

Positive or Negative Dot Product

If A • B < 0 (negative), q > 90°

If A • B > 0 (positive), q < 90°

where q is the angle between vectors A and B.


Example 4.13: Checking for an Object in View

Suppose the camera in your game is currently sitting at (1,4), and vector C = [5 3] represents the camera view. You know that an object's location is (7,2). If the camera can see only 90° in each direction, is the object in view?

Solution
  1. The first thing you need to find is the vector that points from the current camera position to the object's position, as shown in Figure 4.18.

    Figure 4.18. Camera view.

    graphics/04fig18.gif

    You can find the vector from the camera to the object by subtracting the camera's position from the object's position. Call it vector D = [(7 – 1) (2 – 4)] = [6 –2].

  2. Calculate C • D:

    C • D: 5(6) + 3(–2) = 30 – 6 = 24

  3. Because C • D > 0, the angle between the camera's view and the vector to the object must be less than 90°, which means that the object is in view.

This quick comparison works if all you want to know is if the angle between the two vectors is greater than or less than 90°. If you need more information about the angle, the dot product can also be used to find the angle's exact degree measure.

The Angle Between Two Vectors

graphics/04equ13.gif


where q is the angle between vectors A and B.


Example 4.14: The Angle Between Two Vectors

Suppose vector C = [5 2 –3] represents the way you are currently moving (current velocity), but you want to turn and follow vector D = [8 1 –4]. What is the angle of rotation between your current vector C and the desired vector D?

Solution
  1. The first thing you need to find to use this new formula is C • D:

    C • D = 5(8) + 2(1) –3(–4) = 40 + 2 + 12 = 54

  2. The next thing you need is the magnitude of each vector. (You might need to flip back earlier in this chapter for a review of how to find the magnitude when you know the components.)

    graphics/04equ14.gif

  3. Plug these three things into the new formula to calculate q:

    graphics/04equ15.gif

    0.9733 cosq

    q cos–1(0.9733)

    q 13.3°

    The player must turn 13.3° to follow the new vector.

One convenient property of the dot product is that it provides the length of the projection of one vector onto another. To visualize the projection, look at Figure 4.19. Vector A is being projected onto vector B. Imagine that a light source is directly above vector A, and it's shining down on vector B. The length projection is the length of vector A's shadow along the line defined by vector B. The only snag is that the vector being projected onto vector B must be normalized, which means that it has a length of 1. (This idea of projection will resurface in future chapters.)

Figure 4.19. Projection of A onto B.

graphics/04fig19.jpg

The dot product is a very powerful operation that will come up in many later chapters. You'll see it next in Chapter 5. In terms of programming, it's a relatively inexpensive operation, so you'll see it used in many applications. The next section discusses the other method of "multiplying" vectors—the cross product. I believe you'll find the dot product much simpler, but they have very different uses.

Self-Assessment

1.

Find A • B for vectors A = [–2 8] and B = [4 1].

2.

Find C • D for vectors C = [–1 4 2] and D = [3 0 5].

3.

Are vectors A and B in question 1 perpendicular?

4.

Find the angle between vectors C and D in question 2.

5.

Suppose the camera in your game is currently sitting at (0,0) and vector F = [4 9] represents the camera view. You know that the location of an object is (3,–2). If the camera can see only 90° in each direction, is the object in view?


    Previous Section  < Day Day Up >  Next Section