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 16/27)

When all of the prey fish have been eaten, the final time that is displayed is the time that was required for the predator to eat all of the fish in thepopulation and the final kill count that is displayed is the same of the original number of prey fish in the population. (Also note that the player must click the Stop button before starting a new game. Fixing this inconvenience will be a good exercise for the student.)

When the escape attempt is successful...

When the escape attempt is successful, a clone of the prey fish in its new location is stored in the preyObjects container and a normalized versionof the fish's direction vector is stored in the displayVectors container as shown in Listing 22 .

Listing 22 . Restore the prey fish to the population.
}else{ //The escape attempt was successful. Restore// a clone of the prey object in its new // location along with its direction vector// to the population. preyObjects.set(cnt,tempPrey.clone());//Save a normalized version of the direction // vector for drawing later. It will// overwrite the previously saved vector and // if you watch closely it will show the// prey object running away from the // predator.displayVectors.set( cnt,tempVectorA.normalize().scale(15.0));}//end else }//end if distance<50 }//end for loop on population

Listing 22 also signals the end of the for loop that began in Listing 19 .

Keep the prey fish in the playing field

I have one more section of code to explain that deals with the behavior of the prey fish during each iteration of the animation loop. The code in Listing 23 causes prey fish that stray outside the playing field to return to the playing field.

Listing 23 . Keep the prey fish in the playing field.
for(int cnt = 0;cnt<preyObjects.size();cnt++){ tempPrey = preyObjects.get(cnt);if(tempPrey.getDisplacementVector(origin).getLength()>0.5*osiHeight){ tempVectorA =tempPrey.getDisplacementVector(origin); tempPrey = tempPrey.addVectorToPoint(tempVectorA.scale(0.1)); preyObjects.set(cnt,tempPrey.clone());}//end if prey object is out of bounds }//end for loop to process each prey object

The playing field is the interior of a sphere

The playing field is defined to be the interior of a sphere that is centered on the origin with a radius that is one-half the height of the off-screen image.The for loop in Listing 23 iterates once for each prey fish remaining in the population. The if statement in Listing 23 tests to determine if a prey fish is outside of the sphere.

If the prey fish is outside the sphere, the code in Listing 23 gives that prey fish a nudge back toward the origin and saves a clone of the prey fish inits new location.

Erase the off-screen image and draw the large circle

Listing 24 erases the off-screen image and draws the large circle that defines the playing field shown in Figure 1 .

Listing 24 . Erase the off-screen image and draw the large circle.
//Erase the screen g2D.setColor(Color.WHITE);GM01.fillRect(g2D,-osiWidth/2,osiHeight/2, osiWidth,osiHeight);//Draw a broken-line circle that represents the // intersection of the spherical boundary with the// x-y plane. Although the prey objects and the // predator can stray outside this sphere, they// prefer to be inside the sphere. GM01.Point3D tempPointA = new GM01.Point3D(new GM01.ColMatrix3D(osiHeight/2,0,0)); GM01.Point3D tempPointB;//The circle is defined by 39 points around the // circumference.g2D.setColor(Color.BLACK); for(int cnt = 0;cnt<39;cnt++){ tempPointB =new GM01.Point3D(new GM01.ColMatrix3D( osiHeight/2*Math.cos((cnt*360/39)*Math.PI/180),osiHeight/2*Math.sin((cnt*360/39)*Math.PI/180), 0));//Connect every third pair of points with a lineif(cnt%3 == 0){ new GM01.Line3D(tempPointA,tempPointB).draw(g2D); }//end if//Save the old point. tempPointA = tempPointB;}//end for loop//Draw the final line required to close the circle new GM01.Line3D(tempPointA,new GM01.Point3D(new GM01.ColMatrix3D( osiHeight/2,0,0))).draw(g2D);
<< 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.