Preview
In this module, I will present and explain several updates to the game-math library. In addition, I will present and explain five sampleprograms that illustrate the use of the new features of the 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 learnedin this module and previous modules.
Discussion and sample code
The game-math library was updated and the name was changed to GM2D04 in preparation for this module. Much of the code in the updated library remains unchanged. I explained that code in the previousmodules and I won't repeat that explanation in this module. I will concentrate on explaining the modifications that I made to the library inthis module.
The game-math library named GM2D04
A complete listing of the library program is provided in Listing 19 near the end of the module.
This update added the following new capabilities to the library:
- GM2D04.Vector.add - Adds this Vector object to a Vector object received as an incoming parameter and returns the sum as a new Vector object.
- GM2D04.Vector.getLength - Returns the length of a Vector as type double .
- GM2D04.Point.addVectorToPoint - Adds a Vector object to a Point object returning a new Point object.
These three new methods are presented in Listing 1 , Listing 2 , and Listing 3 below. These methods are so simple that no explanation should be required foryou to understand them.
Listing 1 . The add method of the GM2D04.Vector class. |
---|
//Adds this vector to a vector received as an incoming
// parameter and returns the sum as a vector.public GM2D04.Vector add(GM2D04.Vector vec){
return new GM2D04.Vector(new ColMatrix(vec.getData(0)+vector.getData(0),
vec.getData(1)+vector.getData(1)));}//end add |
Listing 2 . The getLength method of the GM2D04.Vector class. |
---|
//Returns the length of a Vector object.
public double getLength(){return Math.sqrt(
getData(0)*getData(0) + getData(1)*getData(1));}//end getLength |
Listing 3 . The addVectorToPoint method of the GM2D04 class. |
---|
//Adds a Vector to a Point producing a new Point.
public GM2D04.Point addVectorToPoint(GM2D04.Vector vec){
return new GM2D04.Point(new GM2D04.ColMatrix(getData(0) + vec.getData(0),
getData(1) + vec.getData(1)));}//end addVectorToPoint |
Simple but powerful
Although these new methods are individually very simple, when combined with the other methods in the library, they significantly increase the power of thelibrary. For example, in the next module I will show you that the library in its current form supports translation and animation.
I will illustrate the use of these new methods in the sample programs that follow.
The program named VectorAdd01
This program illustrates the addition of two and then three vectors. It also illustrates the head-to-tail rule described by Kjell. A complete listing of the program is provided in Listing 20 near the end of the module.
Screen output from the program named VectorAdd01