<< Chapter < Page Chapter >> Page >

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:

  • DotProd2D01
  • DotProd2D02
  • DotProd3D01
  • DotProd3D02

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 named GM02

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 wasto 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 .

  • GM02.ColMatrix2D.dot - computes dot product of two ColMatrix2D objects.
  • GM02.Vector2D.dot - computes dot product of two Vector2D objects.
  • GM02.Vector2D.angle - computes angle between two Vector2D objects.
  • GM02.ColMatrix3D.dot - computes dot product of two ColMatrix3D objects
  • GM02.Vector3D.dot - computes dot product of two Vector3D objects.
  • GM02.Vector3D.angle - computes angle between two Vector3D objects.

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 willexplain 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 tothe 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.
public double dot(GM02.ColMatrix3D matrix){ return getData(0) * matrix.getData(0)+ getData(1) * matrix.getData(1) + getData(2) * matrix.getData(2);}//end 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.
public double dot(GM02.Vector3D vec){ GM02.ColMatrix3D matrixA = getColMatrix();GM02.ColMatrix3D matrixB = vec.getColMatrix(); return matrixA.dot(matrixB);}//end dot

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