<< Chapter < Page Chapter >> Page >

Once again, the code is straightforward. All Vector3D objects instantiated from classes in the game-math library are representedby 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.
public double angle(GM02.Vector3D vec){ GM02.Vector3D normA = normalize();GM02.Vector3D normB = vec.normalize(); double normDotProd = normA.dot(normB);return Math.toDegrees(Math.acos(normDotProd)); }//end 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 shownin 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 originalvectors.

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 degreesas type double .

That completes the discussion of the updates to the game-math library resulting in the new library named GM02 .

The program named DotProd2D01

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 Figure 14 .

Figure 14 GUI for the program named DotProd2D01.

Missing image.

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 anddisplayed 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 explainonly the code contained in the actionPerformed method that is new to this module. A complete listing of this program is shown in Listing 9 .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask