Constructing the Ground


Building the Fractal Land

WrapFractalLand3D is like previous Wrap classes: it creates the 3D scene inside a JPanel. The createSceneGraph( ) method brings the various elements of the scene together:

     void createSceneGraph(double flatness)     {       sceneBG = new BranchGroup(  );       bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE);           lightScene(  );     // add the lights       addBackground(  );  // add the sky       // addFog(  );      // ade fog             // create the landscape: the floor and walls       land = new Landscape(flatness);       sceneBG.addChild( land.getLandBG(  ) );             sceneBG.compile(  );   // fix the scene     } 

lightScene( ) creates a single directional light, with no ambient backup, which makes the scene dark (and hopefully mysterious). The sky is set to be dark blue, which is the color used for the fog.

Linear Fog

The examples in Figure 26-1 use the createSceneGraph( ) method shown above, with the call to addFog( ) commented out. When uncommented, the scene looks something like Figure 26-3.

Figure 26-3. A foggy scene


The fog adds to the sinister nature of the landscape. I've commented it out in the example code to allow you to see what the entire landscape looks like.

The addFog( ) method is:

     private void addFog(  )     { LinearFog fogLinear = new LinearFog( skyColor, 15.0f, 30.0f);       fogLinear.setInfluencingBounds( bounds );       sceneBG.addChild( fogLinear );     } 

The LinearFog node makes the scene start to fade at 15.0f world units from the user and be totally obscured at 30.0f. My choice of numbers is based on the sides of the scene being 64.0f units long, so the fog makes it impossible to see much more than a quarter of the environment at any time. The fog color, skyColour, is also used for the background, creating an effect of things fading away because of their distance from the user.

Java 3D has an ExponentialFog node which makes a fog that seems heavier and closer, supposedly more akin to real-world fog. When I tried it, the fog seemed too heavy, so I moved back to the linear form.


Though my reason for using fog is to make the terrain seem a bit spookier, there is a good technical reason for employing it: Java 3D culls fog-bound objects (i.e., objects invisible to the user) from the scene, thereby speeding up the rendering of the scene.

User Controls

As I mentioned in the introduction, the user controls are keyboard-based with their processing handled by a variant of the trusty KeyBehavior class. The user can move forward, backward, slide left or right, float up or down but can't descend through the ground. A minor change from KeyBehavior in Chapter 25 is that rotations are in steps of 5 degrees left or right rather than 90 degrees. More importantly, the viewpoint stays a fixed distance above the ground as it moves over the landscape. This greatly enhances the impression that the ground is uneven.

WrapFractalLand3D calls createUserControls( ) to adjust the clip distances and to set up the KeyBehavior object:

     private void createUserControls(  )     {       // original clips are 10 and 0.1; keep ratio between 100-1000       View view = su.getViewer(  ).getView(  );       view.setBackClipDistance(20);      // can see a long way       view.setFrontClipDistance(0.05);   // can see close things           ViewingPlatform vp = su.getViewingPlatform(  );       TransformGroup steerTG = vp.getViewPlatformTransform(  );           // set up keyboard controls (and position viewpoint)       KeyBehavior keybeh = new KeyBehavior(land, steerTG);       keybeh.setSchedulingBounds(bounds);       vp.setViewPlatformBehavior(keybeh);     } 

The clip distances are adjusted in the same way, and for the same reasons, as in Maze3D in Chapter 25. As you may recall, I reduced the front clip distance so the user could move close to a block or cylinder without seeing through it. I extended the back clip distance so the full extent of the scene could be seen without far parts of it being clipped. In FractalLand3D, if the front clip is left at 0.1 units (the default), then the user can often see through the side of a hill when the viewpoint is positioned directly in front of it and the terrain is steep.

KeyBehavior handles the processing triggered by key presses and sets up the initial viewpoint position; in previous examples, the starting point was managed by the Wrap class itself.



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