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

1.14 Game 2302-0140: our first 3d game program  (Page 12/27)

Then Listing 12 calls the GM01.Point3D.addVectorToPoint method to relocate the prey fish object to a new position based on the scaled sum ofthe two vectors. The bottom line is that this will cause the prey fish object to spiral toward the geometric center that was computed in Listing 9 as the animation progresses.

Another scale factor is applied to the sum vector before using it to relocate the prey fish object. This scale factor controls the overall speed of the preyfish. Increasing the scale factor causes the prey fish to spiral faster. (Increasing the scale factor also causes some interesting patterns to appear and if you increase it too much, the prey fish will all leave the playing field.)

Save a clone of the relocated prey fish object

Listing 13 creates a clone of the relocated prey fish object and uses it to replace the original prey fish object in the container.

Listing 13 . Save a clone of the relocated prey fish object.
preyObjects.set(cnt,tempPrey.clone());

Creating and saving a reference to a clone instead of saving a reference to the relocated prey fish object may be overkill. However, I simply wantedto guard against the possibility of ending up with a corrupted object later due to the repeated use of the reference variable. I will leave it up to thestudent to think about this and to decide if this was a good or a bad decision.

Save a normalized direction vector

Listing 14 calls the GM01.Vector3D.normalize method to create a vector having a length of 15 units and the same direction as the vector that was usedto relocate the prey fish object.

Listing 14 . Save a normalized direction vector.
displayVectors.set( cnt,tempVectorA.normalize().scale(15.0));

This vector is saved and used for drawing later. Drawing this vector in conjunction with the point that represents the prey fish object produces thetails shown on the prey fish objects in Figure 1 .

Code for when the population contains only one prey fish

Listing 15 shows the else clause that matches up with the if statement in Listing 10 . This code is executed when the population has been reduced to only one prey fish object.

Listing 15 . Code for when the population contains only one prey fish.
}else{ displayVectors.set(cnt,new GM01.Vector3D( new GM01.ColMatrix3D(10,10,0)));}//end else}//end loop to spiral prey objects toward center.

Listing 15 saves a dummy direction vector for drawing later. This is necessary to prevent a null pointer exception when the user specifies onlyone prey fish object and then clicks the Start button.

Not the end of the story

Listing 15 also signals the end of the for loop that causes the prey fish to spiral toward the center. The code in this for loop is sometimes referred to as a cohesion algorithm in flocking terminology. It causes the prey fish to stay together as a group.

This is not the end of the story, however. If the code in this cohesion algorithm were the only code controlling the behavior of the prey fish in thisanimation, they would simply continue spiraling toward the center, eventually producing a cluster that looks something like that shown in Figure 7 where 100 prey fish are trying to occupy the same location in 3D space.

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