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

1.11 Game 2302-0130: putting the game-math library to work  (Page 7/20)

(Go to Google and search for the following keywords to learn more about this topic: baldwin java eligible garbage collection.)

Change the drawing color to RED

After the BLACK geometric object shown in Figure 1 has been drawn on the off-screen image, it is time to change the drawing color to RED in preparationfor translating the object and drawing the translated version of the object. This is accomplished by the single statement in Listing 5 .

Listing 5 . Change the drawing color to RED.
g2D.setColor(Color.RED);//Change drawing color to RED.

Translate the geometric object

That brings us to the most important block of code in the entire program, insofar as achieving the main learning objective of the program is concerned.

Listing 6 . Translate the geometric object.
for(int cnt = 0;cnt<numberPoints;cnt++){ newPoints[cnt]= points[cnt].addVectorToPoint(vector); if(drawPoints){//Draw points if true.newPoints[cnt].draw(g2D);}//end if }//end for loop

Listing 6 uses a call to the addVectorToPoint method of the GM2D04.Point class embedded in a for loop to translate each of the vertices (points) that define the original geometric object to a second set of vertices that define a geometric object having thesame shape in a different location.

(The initial vector that was used to translate the points in Listing 6 was instantiated in Listing 1 with values of 50,50. That vector may be modified later using the values in the fields named Vector X and Vector Y in the GUI shown in Figure 1 when the user clicks the Replot button.)

The new vertices were saved in a different array object referred to by the reference variable named newPoints . It is important to note that in this case, the original geometric object was not moved. Rather, it was replicated ina new location with the new location being defined by a displacement vector added to the value of each original point.

Make it appear to move

In many cases in game programming, you will want to make it appear that the object has actually moved instead of being replicated. That appearance could beachieved by saving the reference to the new GM2D04.Point object back into the same array, thereby replacing the reference that was previously there. Makeno mistake about it, however, when using this approach, the translated geometric object is a different object, defined by a new set of GM2D04.Point objects that define the vertices of the geometric object in the new location.

A different approach

A different approach would be to call the setData method of the GM2D04.Point class to modify the coordinate values stored in the existing object that define the location of the point. Inthat case, it would not be necessary to instantiate a new object, and I suppose it could be argued that such an approach would actually move each vertex, thereby moving the geometric object. If execution speed is an importantfactor (particularly in animation code) it would probably be useful to run some benchmarks to determine which approach would be best. (I will show you how to implement this approach in the program named VectorAdd05a later in this module.)

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