This page is optimized for mobile devices, if you would prefer the desktop version just click here

1.9 Game 2302-0125: vector addition  (Page 7/18)

Figure 3 Screen output from CoordinateFrame01 after changes to the coordinate frame.

The new location of the origin

In Figure 3 , the origin of the coordinate frame has been moved to the right by an amount that is equal to 40-percent of the width of the off-screen image,and has been moved down by an amount that is 60-percent of the height of the off-screen image. Then a pair of orthogonal axes was drawn, intersecting at thenew location of the origin.

A point is simply a location in space

Although a vector doesn't have a location property and therefore is immune to changes in the coordinate frame, a point does have a location property. In factthat is the only property that a point does have, because a point is simply a location in space. Furthermore, that location is always specified relative tosome coordinate frame. In order to cause the point to remain in the same location relative to world coordinates, (which was the objective here) , its values relative to the current coordinate frame must be modified each timethe coordinate frame is modified.

After the values representing the point were modified appropriately, the three vectors were drawn in Figure 3 with the tails of the red and blue vectors located at the point. This caused the geometric object described by the threevectors to remain in the same location relative to world coordinates. However, when the current coordinate frame no longer matched the world coordinate frame,the location of the geometric object changed relative to the current coordinate frame. The geometric object is closer to the origin of the current coordinateframe in Figure 3 than was the case in Figure 2 .

More complicated code

The code in this program is somewhat more complicated than the code in the previous program, mainly due to the inclusion of an interactive GUI in theprogram. I have published many previous tutorials that explain how to write interactive programs in Java, and I won't repeat that explanation here. Instead,I will concentrate on the use of the game-math library in my explanation of this program. What this really means is that I am only going to explain the followingthree methods in this program:

  • actionPerformed
  • setCoordinateFrame
  • drawOffScreen

You can view the remaining program code in Listing 21 near the end of the module.

The actionPerformed method

This method must be defined in the class named GUI because the GUI class implements the ActionListener interface. Because an object of the GUI class is registered as a listener on the Replot button shown in Figure 2 , the actionPerformed method is called each time the userclicks the button.

Listing 12 . The actionPerformed method.
public void actionPerformed(ActionEvent e){ //Reset the coordinate frame to world coordinates by// reversing the most recent translation. setCoordinateFrame(g2D,-xOffset,-yOffset);//Compute new translation offsets based on user input. xOffsetFactor =Double.parseDouble(xOffsetField.getText()); yOffsetFactor =Double.parseDouble(yOffsetField.getText()); xOffset = osiWidth*xOffsetFactor;yOffset = osiHeight*yOffsetFactor; //Draw a new off-screen image based on user inputs// and copy it to the canvas. Note that the // drawOffScreen method will call the// setCoordinateFrame method again with the new // offset values for the origin of the coordinate// frame. drawOffScreen(g2D);myCanvas.repaint(); }//end actionPerformed
<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.