<< Chapter < Page Chapter >> Page >

The drawOffScreen method begins by calling the setCoordinateFrame method to establish a new coordinate frame using offset values entered by the user, (or default offset values of 0.0 at startup) .

Define a point to position the vectors

Listing 15 instantiates a new GM2D04.Point object that will be used to locate the three vectors that form the closed polygon shown in Figure 2 . The location of the polygon relative to the world coordinate frame will remain thesame regardless of how the current coordinate frame is changed.

Listing 15 . Define a point to position the vectors.
double startPointX = (196 - xOffset); double startPointY = (165 - yOffset);GM2D04.Point startPoint = new GM2D04.Point( new GM2D04.ColMatrix(startPointX,startPointY));

The code in Listing 15 is probably the most important code in the entire program relative to the objective of the program. As I mentioned earlier, ifyou want the location of a point to remain the same relative to the world coordinate frame when you change the current coordinate frame, you must modify thevalues that represent that point whenever you cause the current coordinate frame to be different from the world coordinate frame. The code in Listing 15 makes that modification.

Remaining code in the drawOffScreen method

The remaining code in the drawOffScreen method is shown in Listing 16 .

Listing 16 . Remaining code in the drawOffScreen method.
//Instantiate three Vector objects that form a closed // polygon when drawn head-to-tail.double vecAx = 25; double vecAy = 50;GM2D04.Vector vecA = new GM2D04.Vector( new GM2D04.ColMatrix(vecAx,vecAy));double vecBx = 37; double vecBy = -12;GM2D04.Vector vecB = new GM2D04.Vector( new GM2D04.ColMatrix(37,12));//Define vecC as the sum of vecA and vecB. It will be// of the correct length and direction to close the // polygon when drawn such that its tail coincides// with the tail of vecA. GM2D04.Vector vecC = vecA.add(vecB);//Draw vecA in red with its tail at startPoint.g2D.setColor(Color.RED); vecA.draw(g2D,startPoint);//Compute the location of the head of vecA relative to// the current coordinate frame. double headX =vecA.getData(0) + startPoint.getData(0); double headY =vecA.getData(1) + startPoint.getData(1);//Draw vecB in GREEN with its tail at the head of // vecA.g2D.setColor(Color.GREEN); vecB.draw(g2D,new GM2D04.Point(new GM2D04.ColMatrix(headX,headY))); //Draw vecC in BLUE with its tail at startPoint,// coinciding with the tail of vecA. This forms a // closed polygon.g2D.setColor(Color.BLUE); vecC.draw(g2D,startPoint);}//end drawOffScreen

With one exception, there is nothing in Listing 16 that I haven't already explained in earlier programs in this or previous modules. Therefore, Iwon't repeat those explanations here.

The one exception

Recall from Figure 2 that we need to draw the green vector with its tail located at the head of the red vector. In order to do that, we must be able todetermine the location of the head of the red vector relative to the current coordinate frame.

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