Summary: Learn how to compare column matrices for equality, compare two points for equality, compare two vectors for equality, add one column matrix to another, subtract one column matrix from another, and get a displacement vector from one point to another.
This module is one in a series 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 update the game-math library to provide new capabilities including the addition of graphics and set methods for column matrices, points, vectors, and lines. You also learned how to draw on off-screen images.
What you will learn
In this module, you will learn how to compare column matrices for equality, compare two points for equality, compare two vectors for equality, add one column matrix to another, subtract one column matrix from another, and get a displacement vector from one point to another.
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.
As mentioned earlier, in this module you will learn how to:
To assist you in this quest, I will present and explain modifications that were made to update the game-math library that you learned about in the previous two modules. In addition, I will present and explain three sample programs that illustrate the new features of the 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.
Much of the code in the library remains unchanged. I explained that code in previous modules and I won't repeat that explanation in this module. Rather, in this module, I will concentrate on explaining the modifications that I made to the library.
A complete listing of the library program is provided in Listing 11 near the end of the module.
This update added the following new capabilities:
I will explain these updates in conjunction with the discussions of the programs that follow.
A complete listing of this program is provided in Listing 12 near the end of the module. I will explain the program in fragments. In selecting the portions of the program that I will explain, I will skip over material that is very similar to code that I have previously explained.
The purpose of this program is to confirm the behavior of the equals methods of the GM2D03.ColMatrix , Point , and Vector classes.
Overridden equals method of the GM2D03.ColMatrix class
I will begin by explaining some of the equals methods in the updated GM2D03 game-math library.
The first fragment in Listing 1 shows the new equals method of the ColMatrix class.
| Listing 1: Overridden equals method of the GM2D03.ColMatrix class. |
|---|
|
This method overrides the equals method inherited from the Object class. It compares the double values stored in two matrices and returns true if the values are equal or almost equal and returns false otherwise.
An inherent problem when comparing doubles and floats
There is always a problem when comparing two double or float values for equality. If you perform a series of computations twice, using a different computational order for each set of computations, you are likely to end up with two values that are not absolutely equal even if they should be equal. Arithmetic inaccuracies along the way may cause the two results to differ ever so slightly. However, they may be equal from a practical viewpoint.
See Image 3 where the a value that should be 0.0 is actually given by -1.7763568394002505E-15
Therefore, when comparing two double or float values for equality, it is customary to subtract one from the other, convert the difference to an absolute value, and compare that absolute value with an arbitrarily small positive value. If the difference is less than that the test value, the two original values are declared to be equal. Otherwise, they are declared to be unequal.
This is the logic that is implemented in Listing 1 , which shouldn't require further explanation.
Overridden equals method of the GM2D03.Point class
Listing 2 presents the overridden equals method of the GM2D03.Point class.
| Listing 2: Overridden equals method of the GM2D03.Point class. |
|---|
|
This method also overrides the equals method inherited from the Object class. It compares the values stored in the ColMatrix objects that define two Point objects and returns true if they are equal and false otherwise.
One possible point of confusion
The one thing that can be confusing in Listing 2 has to do with the variable named point . This is a reference to an object of type ColMatrix that defines the location of the Point object. Listing 2 calls another new method named getColMatrix to get access to the ColMatrix object that defines the incoming Point object, and uses that object for the comparison. In effect, this method actually compares two objects of the ColMatrix class by calling the method that I explained in Listing 1 . This illustrates the advantages of building up the library classes using objects of the fundamental ColMatrix class.
The getColMatrix method of the GM2D03.Point class
This extremely simple new method, which is called by the code in Listing 2 , is presented in Listing 3 .
| Listing 3: The getColMatrix method of the GM2D03.Point class. |
|---|
|
Listing 3 shouldn't require any explanation beyond the embedded comments.
Overridden equals method of the GM2D03.Vector class
The overridden equals method of the GM2D03.Vector class is essentially the same as the code shown for the Point class in Listing 2 so I won't bother to show and explain it. You van view this new method in Listing 11 .
Beginning of the ColMatrixEquals01 program class
At this point, I will put the discussion of the updated GM2D03 library on hold and explain the ColMatrixEquals01 program.
Listing 4 presents the beginning of the ColMatrixEquals01 program class including the beginning of the main method.
| Listing 4: Beginning of the ColMatrixEquals01 class. |
|---|
|
Listing 4 instantiates four different ColMatrix objects and then compares them in different ways, displaying the results of the comparisons on the command-line screen.
Screen output from the program named ColMatrixEquals01
The first four lines of text in Image 1 were produced by the code in Listing 4. (The remaining output shown in Image 1 was produced by the code in Listing 5 , which I will explain shortly.)
| Image 1: Screen output from the program named ColMatrixEquals01. |
|---|
|
Because of the simplicity of the code in Listing 4 , you shouldn't need any help in understanding why the code in Listing 4 produced the first four lines of output in Image 1 .
The third and fourth lines of output in Image 1 are the result of comparing two matrices whose values are almost equal but not absolutely equal.
Remainder of the ColMatrixEquals01 class
The remainder of the program named ColMatrixEquals01 is shown in Listing 5 . The output produced by this code is shown in the last six lines of text in Image 1 .
| Listing 5: Remainder of the ColMatrixEquals01 class. |
|---|
|
Once again, the code in Listing 5 is very straightforward and shouldn't require further explanation. The screen output shown in Image 1 verifies that the library methods called by this program behave appropriately.
A displacement vector describes the distance and direction that you would have to move to get from one point in space to another point in space.
The getDisplacementVector method of the GM2D03.Point class
Returning to the discussion of the updated GM2D03 library, Listing 6 presents the new getDisplacementVector method of the GM2D03.Point class
| Listing 6: The getDisplacementVector method of the GM2D03.Point class. |
|---|
|
This method gets and returns a displacement vector from one Point object to a second Point object and returns the result as a reference to a new object of the class GM2D03.Vector .
The direction of the vector
The displacement vector points from the Point object on which the method is called to the Point object passed as a parameter to the method. Kjell describes the component parts of the new vector as the distance you would have to walk, first along the x-axis and then along the y-axis to get from the first point to the second point. Of course, you could take the short cut and walk directly from the first point to the second point but that's often not how we do it in programming.
The code in Listing 6 is straightforward and shouldn't require further explanation.
The program named DisplacementVector01
Once again, I will put the discussion of the updated GM2D03 library on hold and explain the program named DisplacementVector01 .
Listing 7 shows the program named DisplacementVector01 in its entirety.
(For convenience, a second copy of this program is provided in Listing 13 near the end of the module along with the other three programs discussed in this module.)
| Listing 7: The program named DisplacementVector01. |
|---|
|
The purpose of this program is to confirm the behavior of the getDisplacementVector method of the GM2D03.Point class. The screen output shown in Image 2 provides that confirmation.
Screen output from the program named DisplacementVector01
The screen output from the program named DisplacementVector01 is shown in Image 2 .
| Image 2: Screen output from the program named DisplacementVector01. |
|---|
|
You should be able to correlate the results shown in Image 2 with the code in Listing 6 and Listing 7 without further explanation.
Very simple methods
By now you may be thinking that the code in the game-math library is very simple and easy to understand. I hope that is the case. I went to great lengths to modularize the library into a set of simple and easily understood methods. Taken as a whole, however, the library is becoming quite powerful, and will become even more powerful as we progress through additional modules in this collection.
The source code for this program is shown in its entirety in Listing 8 .
(The source code is also provided in Listing 14 near the end of the module for convenience.)
| Listing 8: Source code for the program named ColMatrixAddSubtract01. |
|---|
|
The purpose of this program is to confirm the behavior of the new add and subtract methods of the GM2D03.ColMatrix class.
Source code for the add method of the GM2D03.ColMatrix class
Returning once more to a discussion of the updated GM2D03 library, the source code for this method is shown in Listing 9 . The method adds one ColMatrix object to another ColMatrix object, returning a ColMatrix object. As you should have learned from your studies of the Kjell tutorial, the order in which the two objects are added doesn't matter. The result is the same either way.
| Listing 9: Source code for the add method of the GM2D03.ColMatrix class. |
|---|
|
The code in the method is straightforward and shouldn't require an explanation.
Source code for the subtract method of the GM2D03.ColMatrix class
Continuing with the discussion of the updated GM2D03 library, the source code for this method is shown in Listing 10 . This method subtracts one ColMatrix object from another ColMatrix object, returning a ColMatrix object. Also as you should have learned from the Kjell tutorial, in this case, the order of the operation does matter. The object that is received as an incoming parameter is subtracted from the object on which the method is called. Reversing the order of operations produces different results.
| Listing 10: Source code for the subtract method of the GM2D03.ColMatrix class. |
|---|
|
Once again, the code in the method is straightforward and shouldn't require an explanation.
Program output for program named ColMatrixAddSubtract01
Now, lets get back to the program named ColMatrixAddSubtract01 that is shown in Listing 8 . This program produces the output shown in Image 3 .
| Image 3: Screen output from the program named ColMatrixAddSubtract01. |
|---|
|
By now, you should have no difficulty understanding how the output shown in Image 3 was produced by the code shown in Listing 8 . Note, however, that the last value on the third row should be 0.0 instead of the extremely small value shown. This is an example of an inherent problem having to do with comparing double or float values with other double or float values for absolute equality.
Click here to download a zip file containing standard javadoc documentation for the library named GM2D03 . 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 11 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.
Your homework assignment for this module was to study Kjell's tutorial through Chapter 2 - Column and Row Matrix Addition .
The homework assignment for the next module is to study the Kjell tutorial through Chapter 3 - Vector Addition .
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 12 , Listing 13 , and Listing 14 . Compile the code and execute it in conjunction with the game-math library provided in Listing 11 . 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 how to compare column matrices for equality, compare two points for equality, compare two vectors for equality, add one column matrix to another, subtract one column matrix from another, and get a displacement vector from one point to another.
The next module in the collection will show one way for you to visualize column matrices in graphical form.
In the module following that one, you will learn:
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 11 through Listing 14 below.
| Listing 11: Source code for game-math library named GM2D03. |
|---|
|
.
| Listing 12: Source code for the program named ColMatrixEquals01. |
|---|
|
.
| Listing 13: Source code for the program named DisplacementVector01. |
|---|
|
.
| Listing 14: Source code for the program named ColMatrixAddSubtract01. |
|---|
|
Using Java and the game-math library named GM2D03 , or using a different programming environment of your choice, write a program that creates four column matrix objects using the following names and values:
Then use matrix addition and subtraction to compute and display the values of the following matrices:
Finally, test matE and matF for equality and display the result, replacing the question mark with either true or false..
Cause the program to display your name in some manner.
My version of the program produces the text output shown in Image 4 , and you should use that as a display template.
|
Using Java and the game-math library named GM2D03 , or using a different programming environment of your choice, write a program that creates and displays (in text form) three vectors having the names and values shown in Image 5 .
Cause your program to test vecA for equality, first against vecB , and then against vecC , and display the results of the tests in place of the question marks shown in Image 5 .
| Image 5: Text output from Exercise 2. |
|---|
|
Using Java and the game-math library named GM2D03 , or using a different programming environment of your choice, write a program that creates and displays four column matrices having the names and values shown in Image 6 .
Use the first two matrices to create a displacement vector named dispVecE and display its value in place of the question marks in Image 6 .
Use the last two matrices to create another displacement vector named dispVecF and display its value in place of the question marks in Image 6 .
Test dispVecE and dispVecF for equality and display the result of the test in place of the final question mark in Image 6 .
| Image 6: Text output from Exercise 3. |
|---|
|
-end-