<< Chapter < Page Chapter >> Page >

The sample program named DisplacementVector01

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.
public GM2D03.Vector getDisplacementVector( GM2D03.Point point){return new GM2D03.Vector(new GM2D03.ColMatrix( point.getData(0)-getData(0),point.getData(1)-getData(1))); }//end getDisplacementVector

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 wouldhave 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 cutand 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 thismodule.)

Listing 7 . The program named DisplacementVector01.
public class DisplacementVector01{ public static void main(String[]args){ GM2D03.Point pointA = new GM2D03.Point(new GM2D03.ColMatrix(6.5,-9.7)); GM2D03.Point pointB = new GM2D03.Point(new GM2D03.ColMatrix(-6.0,9.0));System.out.println(pointA.getDisplacementVector( pointB));System.out.println(pointB.getDisplacementVector( pointA));}//end main }//end DisplacementVector01 class

The purpose of this program is to confirm the behavior of the getDisplacementVector method of the GM2D03.Point class. The screen output shown in Figure 2 provides that confirmation.

Screen output from the program named DisplacementVector01

The screen output from the program named DisplacementVector01 is shown in Figure 2 .

Figure 2 . Screen output from the program named DisplacementVector01.
-12.5,18.7 12.5,-18.7

You should be able to correlate the results shown in Figure 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 togreat lengths to modularize the library into a set of simple and easily understood methods. Taken as a whole, however, the library is becomingquite powerful, and will become even more powerful as we progress through additional modules in this collection.

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