Adding Obstacles


A Flocking Application

My Flocking3D application is shown in Figure 22-3. It involves the interaction of two different groups of boids: the yellow flock are the predators, and the orange ones the prey. Over time, the boids in the orange flock are slowly eaten though they try their best to avoid it. Both flocks must avoid obstacles and stay within the bounds of the scene.

Figure 22-3. Predator and prey flocks in Flocking3D


The lefthand image shows an early stage in the system, when the predators are chasing prey. The righthand image was taken after all the prey have been eaten, and the predators are flying in groups.

The code in this chapter was developed by one of my students, Miss Sirinart Sakarin, and me; the code can be found in Flocking3D/.


Boid behavior includes:

  • Reynolds' steering rules for separation, alignment, and cohesion.

  • Perching on the ground occasionally.

  • Avoiding static obstacles.

  • Staying within the scene volume.

  • A maximum speed (the prey boids have a higher maximum than the predators).

  • When a predator is hungry, it chases prey boids. If it gets close enough to a prey boid, it will eat the prey.

  • Prey boids try to avoid predators (they have an aversion to being eaten).

The implementation techniques illustrated in this example include:


Inheritance

Inheritance is used to define the boids (as subclasses of Boid) and their behaviors (as subclasses of FlockingBehavior).


Geometry building

The boid shape (a sort of arrowhead) is built using Java 3D's IndexedTriangleArray.


Synchronized updates

Updates to the boids must be controlled, so changes to the flock in each time interval only become visible when every boid in the flock has been changed.


Scene graph detaching

When a boid is eaten, it's removed from the scene graph by having its BranchGroup detached from its parent node.

Figure 22-4 shows the class diagrams for the application. Only the class names are shown; superclasses that are a standard part of Java or Java 3D (e.g., JPanel, Shape3D) are omitted.

Flocking3D's Ancestor

Flocking3D was influenced by Anthony Steed's Java 3D flocking program, developed back in 1998 as part of a comparison with VRML (http://www.cs.ucl.ac.uk/staff/A.Steed/3ddesktop/). Each boid in his work is represented as a transformGroup and utilizes a BoidSet object (a BranchGroup subclass) for a flock. An interesting feature is his use of morphing for wing flapping, implemented as a transition between three triangleArrays. A FlockBehavior class uses WakeupOnFramesElapsed(0) to trigger boid updates, and a FlapBehavior object for the wings is triggered by WakeupOnTransformChanged events when the boid moves. Flock dynamics include perching, speed limiting, proximity detection, and inertia. There's only one kind of boid, and no obstacles in the scene.


Flocking3D is the top-level JFrame. WrapFlocking3D creates the 3D scene, the predator and prey behaviors, and the obstacles. PreyBehavior and PredatorBehavior can be thought of as flock managers: they initialize the boids that make up their flock and handle rule evaluation at runtime; they're subclasses of the FlockBehavior class.

Figure 22-4. The Flocking3D classes


PreyBoid and PredatorBoid represent individual boids and are subclasses of the Boid class. BoidShape is a subclass of Shape3D and manages the boid shape. CheckerFloor and ColouredTiles are the same as in previous examples.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net