Summary: Learn the fundamentals of the vector dot product in both 2D and 3D. Learn how to update the game-math library to support various aspects of the vector dot product. Learn how to write 2D and 3D programs that use the vector dot product methods in the game-math library.
This module is one in a collection of modules designed for teaching GAME2302 Mathematical Applications for Game Development at Austin Community College in Austin, TX.
What you have learned
In the previous module, you learned how to write your first interactive 3D game using the game-math library. You also learned how to write a Java program that simulates flocking behavior such as that exhibited by birds and fish and how to incorporate that behavior into a game. Finally, you examined three other programs that illustrate various aspects of both 2D and 3D animation using the game-math library.
What you will learn
This module is the first part of a two-part mini-series on the vector dot product . By the time you finish both parts, you will have learned
| Image 1: A 3D image before back-face culling. |
|---|
![]() |
.
| Image 2: The 3D image after back-face culling. |
|---|
![]() |
I recommend that you open another copy of this module in a separate browser window and use the following links to easily find and view the Images and Listings while you are reading about them.
The homework assignment for this module was to study the Kjell tutorial through Chapter 10, Angle between 3D Vectors .
I won't repeat everything that Dr. Kjell has to say. However, there are a few points that I will summarize in this section.
Basic definition of the vector dot product
The vector dot product is a special way to multiply two vectors to produce a real result. A description of the vector dot product follows.
the vector dot product of two vectors is the product of the lengths of the vectors multiplied by the cosine of the angle between them
By the angle between them , I mean the angle that would be formed if you were to draw the two vectors with their tails in the same location.
For example, Image 3 shows a black vector and a magenta vector drawn with their tails at the origin. Eyeballing the picture suggests that the angle between the two vectors is forty or fifty degrees.
| Image 3: Two vectors with their tails at the origin, program DotProd2D02. |
|---|
![]() |
Can do more than eyeball
Fortunately, we can do more than eyeball the angle between two vectors. Image 3 shows the screen output produced by the program named DotProd2D02 that I will explain in this module. DotProd2D02 is a 2D program. I will also explain a 3D version named DotProd3D02 in this module as well.
In Image 3 , the top four user input fields allow the user to enter the x and y coordinate values of two vectors according to the labels that identify those fields. When the user clicks the OK button, the first vector is drawn in black with its tail at the origin and the second vector is drawn in magenta with its tail at the origin. The dot product of the two vectors is computed and displayed in the bottom left text field, and the angle between the two vectors is computed and displayed in the bottom right text field.
Don't need to know the angle between the vectors
Upon seeing the description of the dot product given above , you may reasonably be concerned about needing to know the angle between the vectors before you can compute the dot product. Fortunately, as you will see later , it is possible to compute the dot product of two vectors without knowing the angle. In fact, being able to compute the dot product is one way to determine the angle between two vectors.
As you can see, the value of the dot product of the two vectors shown in Image 3 is 6000 and the angle between the vectors is 49.3987 degrees. You will learn how those values were computed shortly.
Major properties of the dot product
Here are some of the major properties of the dot product of two vectors:
As you will see later, these properties apply to both 2D and 3D vectors. In many cases, they also apply to vectors having more than three dimensions as well.
Dot product of two vectors with the same orientation in 2D
Image 4 illustrates the first property in the above list: The dot product of two vectors with the same orientation is the product of their lengths.
| Image 4: Dot product of two vectors with the same orientation in 2D. |
|---|
![]() |
You may recall from some earlier experience that when a right triangle has sides with lengths of 30 and 40, the length of the hypotenuse is 50. That is the case for the magenta vector shown in Image 4 . Similarly, when the sides of the triangle are 60 and 80, the length of the hypotenuse is 100, as is the case for the black vector in Image 4 .
From the property given above, we know that the dot product of the black and magenta vectors shown in Image 4 is 5000, which agrees with the value shown in the Dot Prod output field in Image 4 .
Dot product of two vectors with the same orientation in 3D
Image 5 shows the dot product of two vectors with the same orientation in 3D. The image in Image 5 was produced by the program named DotProd3D02 .
| Image 5: Dot product of two vectors with the same orientation in 3D. |
|---|
![]() |
Manually calculate the value of the dot product
You may need to get your calculator out to manually compute the lengths of the two vectors in Image 5 . Computing the lengths as the square root of the sum of the squares of the three components of each vector gives me the following lengths:
Rounding the product of the two lengths gives the dot product value of 10000, which matches the value shown in the bottom left output field in Image 5 .
The length of a vector
Image 6 illustrates the second property in the above list : The length of a vector is the square root of the dot product of the vector with itself .
| Image 6: Dot product of a 3D vector with an identical vector. |
|---|
![]() |
Image 6 displays two vectors having the same coordinate values as the black vector in Image 5 . (The black vector is hidden by the magenta vector in Image 6 .) Because these two vectors have identical coordinate values, the dot product of these two vectors is the same as the dot product of either vector with itself.
We concluded earlier that the length of each of these vectors is 141.42. This length is the square root of the dot product of the vector with itself. Squaring and rounding this length gives a dot product value of 20000, which matches the value shown in the bottom left output field in Image 6 .
Dot product of vectors with opposite orientations
Image 7 illustrates the third property of the dot product given above : The dot product of two vectors having opposite orientations is the negative of the product of their lengths .
| Image 7: Dot product of vectors with opposite orientations. |
|---|
![]() |
The two vectors shown in Image 7 have the same absolute coordinates as the two vectors shown in Image 5 . However, the algebraic signs of the coordinates of the magenta vector in Image 7 were reversed relative to Image 4 , causing the magenta vector to point in the opposite direction from the black vector. (Note that the angle between the two vectors, as reported by the program is zero degrees in Image 5 and is 180 degrees in Image 7 .)
The point here is that the dot product of the two vectors in Image 7 is the negative of the dot product of the two vectors in Image 5 . This property will be used in another program in the second part of this two-part miniseries to achieve the back-face culling shown in Image 1 .
The computational simplicity of the vector dot product
If you have studied the Kjell tutorial through Chapter 10, Angle between 3D Vectors you have learned that the dot product of two vectors can be computed as the sum of products of the corresponding x, y, and z components of the two vectors. In particular, in the 2D case , the dot product is given by:
2D dot product = x1*x2 + y1*y2
Similarly, in the 3D case , the dot product is given by:
3D dot product = x1*x2 + y1*y2 + z1*z2
Note that these two formulations don't require the program to know anything about the angle between the two vectors, as is the case in the earlie r definition.
The dot product of perpendicular vectors in 2D
The dot product has many uses in game programming, not the least of which is determining if two vectors are perpendicular.
Image 8 illustrates the fourth property in the above list : The dot product of perpendicular vectors is zero . This is an extremely important property in that it allows game programs to easily determine if two vectors are perpendicular. I will begin with a 2D discussion because the topic of perpendicularly of vectors is less complicated in 2D than in 3D .
| Image 8: The dot product of perpendicular vectors in 2D. |
|---|
![]() |
By eyeballing Image 8 , you can see that the magenta vector is probably perpendicular to the black vector. Looking at the output fields at the bottom left and the bottom right in Image 8 , you will see that the dot product of the two vectors is zero and the angle between the two vectors is 90 degrees.
Restating the perpendicularity property
Most of us learned in our earlier mathematics classes that the angle between perpendicular lines is 90 degrees. Therefore, the angle between perpendicular vectors must be 90 degrees. (See a definition of perpendicularity .)
Most of us also learned in our trigonometry classes that the cosine of 90 degrees is 0.0. Since, by definition , the value of the dot product of two vectors is the product of the lengths of the vectors multiplied by the cosine of the angle between them , and the angle must be 90 degrees for two vectors to be perpendicular, then the dot product of perpendicular vectors must be zero as stated by the fourth property in the above list of properties .
An infinite number of perpendicular vectors
By observing Image 8 , it shouldn't be too much of a stretch for you to recognize that there are an infinite number of different vectors that could replace the magenta vector in Image 8 and be perpendicular to the black vector. However, since we are discussing the 2D case here, all of those vectors must lie in the same plane and must have the same orientation (or the reverse orientation) as the magenta vector. In other words, all of the vectors in the infinite set of vectors that are perpendicular to the black vector must lie on the line defined by the magenta vector, pointing in either the same direction or in the opposite direction. However, those vectors can be any length and still lie on that same line.
A general formulation of 2D vector perpendicularity
By performing some algebraic manipulations on the earlier 2D formulation of the dot product, we can formulate the equations shown in Image 9 that define the infinite set of perpendicular vectors described above.
| Image 9: A general formulation of 2D vector perpendicularity. |
|---|
|
As you can see from Image 9 , we can assume any values for y1, y2, and x1 and compute a value for x2 that will cause the two vectors to be perpendicular.
A very interesting case
One very interesting 2D case is the case shown in Image 8 . In this case, I initially specified one of the vectors to be given by the coordinate values (50,100). Then I assumed that y2 is equal to x1 and computed the value for x2. The result is that the required value of x2 is the negative of the value of y1.
Thus, in the 2D case, we can easily define two vectors that are perpendicular by
The actual direction that the second vector points will depend on which value you negate in the second vector.
Another interesting 2D case of perpendicular vectors
Another interesting 2D case is shown in Image 10 .
| Image 10: Another interesting 2D case of perpendicular vectors. |
|---|
![]() |
In Image 10 , I assumed the same coordinate values for the black vector as in Image 8 . Then I assumed that the values of the y-coordinates for both vectors are the same. Using those values along with the equation in Image 8 , I manually computed a required value of -200 for the x-coordinate of the magenta vector. I entered that value into the field labeled VectorBx = in Image 10 and clicked the OK button.
And the result was...
You can see that the value of the dot product of the two vectors in Image 10 is 0.0, and the angle between the two vectors is 90 degrees. Therefore, although the magenta vector in Image 10 is much longer than the magenta vector in Image 8 , the magenta vector in Image 10 is still perpendicular to the black vector. Thus, Image 8 and Image 10 show two of the infinite number of magenta vectors that are perpendicular to the black vector in those images.
The dot product of perpendicular vectors in 3D
As I mentioned earlier, the topic of perpendicularity in 3D is more complicated than is the case in 2D. As is the case in 2D, there are an infinite number of vectors that are perpendicular to a given vector in 3D. In 2D, the infinite set of perpendicular vectors must have different lengths taken in pairs, and the vectors in each pair must point in opposite directions.
An infinite number of perpendicular vectors having the same length
However, in 3D there are an infinite number of vectors having the same length that are perpendicular to a given vector. All of the perpendicular vectors having the same length must point in different directions and they must all lie in a plane that is perpendicular to the given vector.
Perpendicular vectors having different lengths may point in the same or in different directions but they also must lie in a plane that is perpendicular to the given vector.
A wagon-wheel analogy
Kjell explains the situation of an infinite set of 3D vectors that are perpendicular to a given vector by describing an old-fashioned wagon wheel with spokes that emanate directly from the hub and extend to the rim of the wheel. The hub surrounds the axle and each of the spokes is perpendicular to the axle. Depending on the thickness of the spokes, a large (but probably not infinite) number of spokes can be used in the construction of the wagon wheel.
Another wheel at the other end of the axle
In this case, the wagon wheel lies in a plane that is perpendicular to the axle. There is normally another wheel at the other end of the axle. Assuming that the axle is straight, the second wheel is in a different plane but that plane is also perpendicular to the axle. Thus, the spokes in the second wheel are also perpendicular to the axle.
If there were two identical wheels at each end of the axle for a total of four wheels (the predecessor to the modern 18-wheel tractor trailer) , the spokes in all four of the wheels would be perpendicular to the axle. Again, the point is that there are an infinite number of vectors that are perpendicular to a given vector in 3D.
A general formulation of 3D vector perpendicularity
By performing some algebraic manipulations on the earlier 3D formulation of the dot product, we can develop the equations shown in Image 11 that describe an infinite set of vectors that are perpendicular to a given vector.
| Image 11: A general formulation of 3D vector perpendicularity. |
|---|
|
(Although I didn't do so in Image 11 , I could have written three more equations that could be used to solve for x1, y1, and z1 if the given vector is notated as x2, y2, and z2.)
No simple case
Unlike with 2D vectors, (to my knowledge) , there is no case that simply allows you to swap coordinate values and change the sign on one of them to produce a vector that is perpendicular to another vector. However, given the equations in Image 11 , and given values for x1, y1, and z1, we can assume values for y2 and z2 and determine the value for x2 that will cause the two vectors to be perpendicular.
While this is a fairly tedious computation with a hand calculator, it is very easy to write Java code to perform the computation. Therefore, given a 3D vector, it is fairly easy to write Java code that will compute an infinite number of vectors that are perpendicular to the given vector.
A pair of perpendicular 3D vectors
Image 12 and Image 13 each show a magenta 3D vector that is part of the infinite set of vectors that are all perpendicular to the black 3D vector. In Image 12 , y1 was assumed to be equal to y2, and z1 was assumed to be equal to z2. For an x1 value of 25, a value of -125 was required to cause the magenta vector to be perpendicular to the black vector.
| Image 12: A pair of perpendicular 3D vectors. |
|---|
![]() |
Another pair of perpendicular 3D vectors
In Image 13 , x1 was assumed to be equal to x2, and z1 was assumed to be equal to z2.
| Image 13: Another pair of perpendicular 3D vectors. |
|---|
![]() |
For a y1 value of 50, a y2 value of - 25 was required to cause the magenta vector to be perpendicular to the black vector.
The black vector didn't change
Note that the black vector is the same in Image 12 and Image 13 . However, the magenta vectors are different in Image 12 and Image 13 . They represent just two of the infinite set of vectors that are perpendicular to the black vector. (They are two of the vectors in the infinite set of perpendicular vectors that are relatively easy to compute using program code.)
Computing the angle between two vectors
The fifth property in the previous list reads: The angle between two vectors is the same as the angle between normalized versions of the vectors, which is equal to the arc cosine of the dot product of the normalized vectors.
In other words, given two vectors, we can compute the angle between the two vectors by first normalizing each vector and then computing the dot product of the two normalized vectors.
(By normalizing, I mean to change the coordinate values such that the direction of the vector remains the same but the length of the vector is converted to 1.0.)
The dot product is equal to the cosine of the angle. The actual angle can be obtained by using one of the methods of the Math class to compute the arc cosine of the value of the dot product.
Used to compute the output angle in the programs
This is the procedure that is used by the programs in this module to compute and display the angle between two vectors as illustrated by the contents of the output fields labeled Ang(deg) or Angle (deg) = in many of the images in this module.
I will have more to say about this topic as I explain the code in the upgraded game-math library named GM02 as well as the following four programs that demonstrate the use of the upgraded game-math library:
I will also provide exercises for you to complete on your own at the end of the module. The exercises will concentrate on the material that you have learned in this module and previous modules.
In this section, I will present and explain an updated version of the game-math library named GM02 .
This game-math library is an update to the game-math library named GM01 . The main purpose of this update was to add vector dot product and related capabilities, such as the computation of the angle between two vectors to the library.
The following methods are new instance methods of the indicated static top-level classes belonging to the class named GM02 .
Will only explain the new 3D methods
I have explained much of the code in the game-math library in previous modules, and I won't repeat those explanations here. Rather, I will explain only the new 3D code in this module. Once you understand the new 3D code, you should have no difficulty understanding the new 2D code.
You can view a complete listing of the updated game-math library in Listing 8 near the end of the module.
Source code for the method named GM02.ColMatrix3D.dot
Listing 1 shows the source code for the new instance method named GM02.ColMatrix3D.dot . If you have studied the Kjell tutorials, you have learned that the dot product can be applied not only to two vectors, but can also be applied to the column matrix objects that are used to represent the vectors. Listing 1 computes the dot product of two ColMatrix3D objects and returns the result as type double.
| Listing 1: Source code for the method named GM02.ColMatrix3D.dot. |
|---|
|
This is one of those cases where it is very easy to write the code once you understand the code that you need to write. Listing 1 implements the equation for the 3D dot product that I provided earlier and returns the result of the computation as type double .
Source code for the method named GM02.Vector3D.dot
Listing 2 shows the source code for the method named GM02.Vector3D.dot . This method computes the dot product of two Vector3D objects and returns the result as type double .
| Listing 2: Source code for the method named GM02.Vector3D.dot. |
|---|
|
Once again, the code is straightforward. All Vector3D objects instantiated from classes in the game-math library are represented by objects of the ColMatrix3D class. Listing 2 gets a reference to the two ColMatrix3D objects that represent the two vectors for which the dot product is needed. Then it calls the GM02.ColMatrix3D.dot method shown earlier in Listing 1 to get and return the value of the dot product of the two vectors.
Source code for the method named GM02.Vector3D.angle
Listing 3 shows the source code for the method named GM02.Vector3D.angle . This method computes and returns the angle between two Vector3D objects. The angle is returned in degrees as type double .
| Listing 3: Source code for the method named GM02.Vector3D.angle. |
|---|
|
You need to understand trigonometry here
If you understand trigonometry, you will find the code in Listing 3 straightforward. If not, simply take my word for it that the method shown in Listing 3 behaves as described above.
The method begins by calling the normalize method on each of the two vectors for which the angle between the vectors is needed. (See the definition of the normalize method in Listing 8 .)
Then Listing 3 computes the dot product of the two normalized vectors. The value of the dot product is the cosine of the angle between the two original vectors.
After that, Listing 3 calls the acos method of the Math class to get the arc (inverse) cosine (see Inverse Cosine ) of the dot product value. The acos method returns the angle in radians.
Finally, Listing 3 calls the toDegrees method of the Math class to convert the angle from radians to degrees and to return the angle in degrees as type double .
That completes the discussion of the updates to the game-math library resulting in the new library named GM02 .
To understand this program, you need to understand the material in the Kjell tutorial through Chapter 8 - Length, Orthogonality, and the Column Matrix Dot product .
The purpose of this program is simply to confirm proper operation of the GM02.ColMatrix2D.dot method. The program output is shown in Image 14 .
| Image 14: GUI for the program named DotProd2D01. |
|---|
![]() |
A graphical user interface
The program creates a GUI that allows the user to enter the first and second values for each of a pair of ColMatrix2D objects into four text fields. The GUI also provides a button labeled OK . When the user clicks the OK button, the dot product of the two ColMatrix2D objects is computed. The resulting value is formatted to four decimal digits and displayed in a text field in the lower left of the GUI.
Similar to previous programs
Much of the code in this program is similar to code that I have explained in earlier modules, so I won't repeat those explanations here. I will explain only the code contained in the actionPerformed method that is new to this module. A complete listing of this program is shown in Listing 9 .
The actionPerformed method in the program named DotProd2D01
The beginning of the actionPerformed method is shown in Listing 4 . This method is called to respond to a click on the OK button shown in Image 14 .
| Listing 4: The actionPerformed method in the program named DotProd2D01. |
|---|
|
Listing 4 begins by instantiating a pair of GM02.ColMatrix2D objects using data provided by the user in the top four fields shown in Image 14 .
Then Listing 4 calls the dot method on the object referred to by matrixA , passing the object referred to by matrixB as a parameter. The dot method computes the dot product of the two column matrix objects, returning the result as type double .
Format the dot product value for display in the GUI
In some cases, the format of the returned value is not very suitable for display in the lower-left field in Image 14 . For example, if the value is very small, it is returned in an exponential notation requiring a large number of digits for display. Similarly, sometimes the dot-product value is returned in a format something like 0.33333333 requiring a large number of digits to display.
Listing 5 formats the dot product value to make it suitable for display in the text field in Image 14 . (There may be an easier way to do this, but I didn't want to take the time to figure out what it is.)
| Listing 5: Format the dot product value for display in the GUI. |
|---|
|
Eliminate exponential format and format to four decimal digits
Listing 5 begins by simply setting small values that are less than 0.001 to 0.0 to eliminate the exponential format for very small, non-zero values.
Then Listing 5 executes some code that formats the dot product value to four decimal digits. I will leave it as an exercise for the student to decipher how this code does what it does.
I recommend that you try it
I recommend that you plug a few values into the input fields, click the OK button, and use your calculator to convince yourself that the program properly implements the 2D dot product equation shown earlier .
That concludes the discussion of the program named DotProd2D01 .
To understand this program, you need to understand the material in the Kjell tutorial through Chapter 9, The Angle Between Two Vectors .
This program allows the user to experiment with the dot product and the angle between a pair of GM02.Vector2D objects.
A screen shot of the output from this program is shown in Image 3 . The GUI shown in Image 3 is provided to allow the user to enter four double values that define each of two GM02.Vector2D objects. The GUI also provides an OK button as well as two text fields used for display of computed results.
In addition, the GUI provides a 2D drawing area. When the user clicks the OK button, the program draws the two vectors, (one in black and the other in magenta) , on the output screen with the tail of each vector located at the origin in 2D space. The program also displays the values of the dot product of the two vectors and the angle between the two vectors in degrees.
Once again, much of the code in this program is similar to code that I have explained before. I will explain only the method named actionPerformed , for which some of the code is new to this module. A complete listing of this program is provided in Listing 10 .
Beginning of the actionPerformed method in the program named DotProd2D02
This method is called to respond to a click on the OK button in Image 2 .
| Listing 6: Beginning of the actionPerformed method in the program named DotProd2D02. |
|---|
|
Listing 6 gets the four user input values that define the two vectors and draws them in black and magenta on the GUI shown in Image 3 . There is nothing new in the code in Listing 6 .
Compute the dot product and the angle between the two vectors
Listing 7 computes the dot product and the angle between the two vectors by first calling the dot method and then the angle method on the object referred to by vecA , passing the object referred to by vecB as a parameter.
| Listing 7: Compute the dot product and the angle between the two vectors. |
|---|
|
In both cases, the code in Listing 7 formats the returned double values to make them appropriate for display in the bottom two text fields in Image 3 . This code was deleted from Listing 7 for brevity.
Confirm that the results are correct
Because this is a 2D display, it is easy to make an eyeball comparison between the drawing of the two vectors and the reported angle between the two vectors to confirm agreement. However, I recommend that you use this program to define several vectors and then use your scientific calculator to confirm that the results shown are correct.
That concludes the explanation of the program named DotProd2D02 .
To understand this program, you need to understand the material in the Kjell tutorial through Chapter 8 - Length, Orthogonality, and the Column Matrix Dot product .
The purpose of this program is to confirm proper operation of the ColMatrix3D.dot method. The program creates a GUI that allows the user to enter three values for each of a pair of ColMatrix3D objects along with a button labeled OK . When the user clicks the OK button, the dot product of the two ColMatrix3D objects is computed and displayed.
A screen shot of the output from the program named DotProd3D01
A screen shot of the output from the program named DotProd3D01 is shown in Image 15 . (Compare Image 15 with Image 14 .)
| Image 15: A screen shot of the output from the program named DotProd3D01. |
|---|
![]() |
Very similar to a previous program
Except for the fact that this program calls the dot method on an object of the GM02.ColMatrix3D class instead calling the dot method on an object of the GM02.ColMatrix2D class, this program is essentially the same at the program named DotProd2D01 that I explained earlier. Therefore, you should have no trouble understanding this program without further explanation from me. A complete listing of this program is provided in Listing 11 .
That concludes the explanation of the program named DotProd3D01 .
You need to understand the material in the Kjell tutorial through Chapter 10, Angle between 3D Vectors to understand this program.
Program output
A screen shot of the output of this program is shown in Image 12 . This program allows the user to experiment with the dot product and the angle between a pair of GM02.Vector3D objects. A GUI is provided that allows the user to enter six double values that define each of two GM02.Vector3D objects. The GUI also provides an OK button as well as two text fields used for display of the computed results.
In addition, the GUI provides a 3D drawing area. When the user clicks the OK button, the program draws the two vectors, (one in black and the other in magenta) , on the output screen with the tail of each vector located at the origin in 3D space. The program also displays the values of the dot product of the two vectors and the angle between the two vectors in degrees. (Compare the output of this 3D program in Image 12 with the output from the 2D program in Image 3 .)
Program code
A complete listing of this program is provided in Listing 12 . Almost all of the new and interesting code in this program is in the method named actionPerformed .
Very similar to a previous program
If you compare the method named actionPerformed in Listing 12 with the actionPerformed method for the program named DotProd2D02 in Listing 10 , you will see that they are very similar. One calls 2D methods in the game-math library while the other calls 3D methods in the same library. Therefore, you should have no difficulty understanding this program without further explanation from me.
That concludes the explanation of the program named DotProd3D02 .
In effect, the dot product of two vectors provides a measure of the extent to which the two vectors have the same orientation. If the two vectors are parallel, the dot product of the two vectors has a maximum value of 1.0 multiplied by the product of the lengths of the two vectors. Normalizing the vectors before computing the dot product will eliminate the effect of the vector lengths and will cause the results to be somewhat easier to interpret.
If the normalized vectors are parallel and point in the same direction, the dot product of the normalized vectors will be 1.0. If the normalized vectors are parallel and point in opposite directions, the value of the dot product will be -1.0. If the vectors are perpendicular, the dot product of the two vectors will be 0.0 regardless of whether or not they are normalized.
For all orientations, the value of the dot product of normalized vectors will vary between -1.0 and +1.0.
You may have it in your mind that the use of mathematical concepts such as the vector dot product are limited to the three dimensions of width, height, and depth. If so, that is a false impression. The vector dot product is very useful for systems with more than three dimensions. In fact, engineers and scientists deal with systems every day that have more than three dimensions. While it usually isn't too difficult to handle the math involved in such systems, we have a very hard time drawing pictures of systems with more than three or four dimensions.
Digital convolution is a good example
I have spent much of my career in digital signal processing where I have routinely dealt with systems having thirty or forty dimensions. For example, in the module titled Convolution and Frequency Filtering in Java and some other related modules as well, I describe the process of digital convolution filtering .
One way to think of digital convolution is that it is a running dot product computation between one vector (the convolution filter) and a series of other vectors, each comprised of successive chunks of samples from the incoming data. In non-adaptive systems, the vector that represents the convolution filter usually has a set of fixed coordinate values, and there may be dozens and even hundreds of such coordinates. (For an adaptive system, the values that define the vector typically change as a function of time or some other parameter.)
Often, the input data will consist of samples taken from a channel consisting of signal plus noise. The objective is often to design a vector (the convolution filter) that is parallel to all of the signal components in the incoming data and is perpendicular to all of the noise components in the incoming data. If that objective is achieved, the noise will be suppressed while the signal will be passed through to the output.
Click here to download a zip file containing standard javadoc documentation for the library named GM02 . Extract the contents of the zip file into an empty folder and open the file named index.html in your browser to view the documentation.
Although the documentation doesn't provide much in the way of explanatory text (see Listing 8 and the explanations given above) , the documentation does provide a good overview of the organization and structure of the library. You may find it helpful in that regard.
The homework assignment for this module was to study the Kjell tutorial through Chapter 10, Angle between 3D Vectors .
The homework assignment for the next module is to continue studying that same material.
In addition to studying the Kjell material, you should read at least the next two modules in this collection and bring your questions about that material to the next classroom session.
Finally, you should have begun studying the physics material at the beginning of the semester and you should continue studying one physics module per week thereafter. You should also feel free to bring your questions about that material to the classroom for discussion.
I encourage you to copy the code from Listing 8 through Listing 12 . Compile the code and execute it in conjunction with the game-math library named GM02 provided in Listing 8 . Experiment with the code, making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.
In this module, you learned the fundamentals of the vector dot product in both 2D and 3D. You learned how to update the game-math library to support various aspects of the vector dot product, and you learned how to write 2D and 3D programs that use the vector dot product methods in the game-math library.
In the next module, which will be the second part of this two-part miniseries on the vector dot product, you will learn how to use the dot product to compute nine different angles of interest that a vector makes with various elements in 3D space.
You will learn how to use the dot product to find six of the infinite set of vectors that are perpendicular to a given vector as shown in Image 16 .
| Image 16: Six (magenta) vectors that are perpendicular to a given (black) vector. |
|---|
![]() |
You will also learn how to use the dot product to perform back-face culling as shown in Image 1 and Image 2 .
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
Complete listings of the programs discussed in this module are shown in Listing 8 through Listing 12 below
| Listing 8: Source code for the game-math library named GM02. |
|---|
|
.
| Listing 9: Source code for the program named DotProd2D01. |
|---|
|
.
| Listing 10: Source code for the program named DotProd2D02. |
|---|
|
.
| Listing 11: Source code for the program named DotProd3D01. |
|---|
|
.
| Listing 12: Source code for the program named DotProd3D02. |
|---|
|
Using Java and the game-math library named GM02 , or using a different programming environment of your choice, write a program that behaves as follows.
When the program starts running, an image similar to Image 17 appears on the screen. Each of the text fields is blank and the drawing area at the top is also blank.
Each time you click the Replot button, the program generates three random values in the general range of -128 to 127. The first two values are used as the x and y values for a vector. The two values are displayed in the fields labeled VectorAx and VectorAy . Also, the two values are used to create and draw a black vector with its tail at the origin as shown in Image 17 .
The third random value is used as the x-value for a second vector. It is displayed in the field labeled VectorBx . A y-value is computed that will cause that vector to be perpendicular to the black vector. That value is displayed in the field labeled VectorBy and the two values are used to draw the magenta vector shown in Image 17 .
The dot product between the two vectors is computed and displayed in the field labeled Dot Prod . The angle between the two vectors is computed and displayed in the field labeled Angle (deg) .
If the two vectors are perpendicular, the dot product should be close to zero and the angle should be very close to 90 degrees.
Cause your name to appear in the screen output in some manner.
| Image 17: Output from Exercise 1. |
|---|
![]() |
Using Java and the game-math library named GM02 , or using a different programming environment of your choice, write a program that behaves as follows.
When the program starts running, an image similar to Image 18 appears on the screen. Each of the text fields is blank and the drawing area at the top is also blank.
Each time you click the Plot button, the program generates three random values in the general range of -128 to 127. The first two values are used as the x and y values for a 3D vector. (Set the z-value for the vector to 0.0.) The three values are displayed in the fields labeled VecAx , VecAy , and VecAz . Also, the three values are used to create and draw a black 3D vector with its tail at the origin as shown in Image 18 .
The third random value is used as the x value for a second 3D vector. (Set the z-value for the vector to 0.0.) Those two values are displayed in the fields labeled VecBx and VecBz . A y-value is computed that will cause that vector to be perpendicular to the black vector. That value is displayed in the field labeled VecBy and the three values are used to draw the magenta vector shown in Image 18 .
The dot product between the two vectors is computed and displayed in the field labeled Dot Prod . The angle between the two vectors is computed and displayed in the field labeled Angle (deg) .
If the two vectors are perpendicular, the dot product should be close to zero and the angle should be very close to 90 degrees.
Cause your name to appear in the screen output in some manner.
| Image 18: Output from Exercise 2. |
|---|
![]() |
Using Java and the game-math library named GM02 , or using a different programming environment of your choice, write a program that behaves as follows.
When the program starts running, an image similar to Image 19 appears on the screen. Each of the text fields is blank and the drawing area at the top is also blank.
Each time you click the Plot button, the program generates five random values in the general range of -128 to 127. The first three values are used as the x, y, and z values for a vector. The three values are displayed in the fields labeled VecAx , VecAy , and VecAz . Also, the three values are used to create and draw a black vector with its tail at the origin as shown in Image 19 .
The fourth and fifth random values are used as the x and y values for a second vector. They are displayed in the fields labeled VecBx and VecBy . A z-value is computed that will cause that vector to be perpendicular to the black vector. That value is displayed in the field labeled VecBz and the three values are used to draw the magenta vector shown in Image 19 .
The dot product between the two vectors is computed and displayed in the field labeled Dot Prod . The angle between the two vectors is computed and displayed in the field labeled Angle (deg) .
If the two vectors are perpendicular, the dot product should be close to zero and the angle should be very close to 90 degrees.
Cause your name to appear in the screen output in some manner.
| Image 19: Output from Exercise 3. |
|---|
![]() |
-end-