<< Chapter < Page Chapter >> Page >

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 previoustwo 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 learnedin this module and previous modules.

Discussion and sample code

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.

The game-math library named GM2D03

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:

  • Compare two ColMatrix objects for equality by implementing Kjell's rules for equality given in his Chapter 1, topic "Column Matrix Equality." The equality test does not test for absolute equality. Rather, It compares the values stored in two matrices and returns true ifthe values are equal or almost equal and returns false otherwise.
  • Get a reference to the ColMatrix object that defines a Point object.
  • Compare two Point objects for equality based on a comparison of the ColMatrix objects that define them.
  • Get a reference to the ColMatrix object that defines a Vector object.
  • Compare two Vector objects for equality based on a comparison of the ColMatrix objects that define them.
  • Add one ColMatrix object to a second ColMatrix object, returning a ColMatrix object.
  • Subtract one ColMatrix object from a second ColMatrix object, returning a ColMatrix object.
  • Get a displacement vector from one Point object to a second Point object. The vector points from the object on which the getDisplacementVector method is called to the object passed as a parameter to the method.

I will explain these updates in conjunction with the discussions of the programs that follow.

The sample program named ColMatrixEquals01

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 selectingthe 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.
public boolean equals(Object obj){ if(obj instanceof GM2D03.ColMatrix&&Math.abs(((GM2D03.ColMatrix)obj).getData(0) - getData(0))<= 0.00001&&Math.abs(((GM2D03.ColMatrix)obj).getData(1) - getData(1))<= 0.00001){ return true;}else{ return false;}//end else}//end overridden equals method

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