<< Chapter < Page Chapter >> Page >

Figure 7 100 prey fish trying to occupy the same location in 3D space.

Missing image.

Interesting but also boring

While it may be interesting to watch the animation progress to this point, the animation becomes very boring when all of the prey fish cluster at thecenter. What we need is an additional algorithm that will cause each prey fish to attempt to maintain a respectable distance between itself and the other preyfish.

Prey fish maintaining a reasonable separation

For example, Figure 8 shows the result of temporarily making each prey fish immune to the presence of the predator, telling each prey fish to spiral towardthe center, and also telling each prey fish to maintain a separation of ten units (pixels) between itself and all of the other prey fish.

Figure 8 100 prey fish maintaining a reasonable separation in 3D space.

Missing image.

A 3D world

Remember, Figure 8 is a 3D world projected onto on a 2D screen display. Even if every prey fish is separated from every other prey fish by at least tenpixels (which is probably not the case as I will explain later) , the projection of the 3D world onto the 2D display can make it appear that two ormore prey fish occupy the same location.

Separate the prey fish

Listing 16 shows the beginning of a pair of nested for loops that attempt to keep the prey fish from colliding with oneanother by moving each prey fish object away from its close neighbors if necessary. In flockingterminology, this is often referred to as a separation algorithm.

Listing 16 . Separate the prey fish.
GM01.Point3D refPrey = null; GM01.Point3D testPrey= null;for(int row = 0;row<preyObjects.size();row++){ refPrey = preyObjects.get(row);//Compare the position of the reference prey // object with the positions of each of the// other prey objects. for(int col = 0;col<preyObjects.size();col++){ //Get another prey object for proximity test.testPrey = preyObjects.get(col);

This algorithm gets a reference to each prey fish object (primary object) and compares its position with the positions of all the other prey fish objects (secondary objects) . If the primary object is too close to a secondary object, the primary object is moved away from thesecondary object.

Not a perfect algorithm

This is not a perfect algorithm however. A primary object can be moved away from all of its neighbors early in the execution of the algorithm, but aneighbor could be moved closer to the primary object later in the execution of the algorithm. While not perfect, the algorithm does a pretty respectable job ofkeeping the prey fish separated as evidenced by comparing the positions of the prey fish in Figure 7 with the positions of the prey fish in Figure 8 . This separation algorithm was disabled in Figure 7 and was enabled in Figure 8 .

Gets two objects that will be compared

Listing 16 gets a reference to a primary prey fish object in the outer loop that iterates on the counter named row and gets a reference to one of the secondary prey fish objects to which it will be compared in the inner loop thatiterates on the counter named col .

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