The Floor


Floating Spheres

Sphere is a utility class from Java 3D's com.sun.j3d.utils.geometry package, a subclass of the Primitive class, which is a Group node with a Shape3D child (see Figure 15-3). Its geometry is stored in a Java 3D TRiangleStripArray, which specifies the sphere as an array of connected triangles. I don't have to adjust this geometry, but the sphere's appearance and position do require changes.

The Appearance node is a container for references of to much information, including coloring, line, point, polygon, rendering, transparency, and texture attributes.

ColouringAttributes fixes the color of a shape and is unaffected by scene lighting. For a shape requiring interaction between color and light, the Material component is employed. For light to affect a shape's color, three conditions must be met:

  • The shape's geometry must include normals.

  • The shape's Appearance node must have a Material component.

  • The Material component must have enabled lighting with setLightingEnable( ).

The utility Sphere class can automatically creates normals, so the first condition is easily satisfied.

Coloring the Spheres

The Java 3D Material component controls what color a shape exhibits when lit by different kinds of lights:

     Material mat = new Material(ambientColor, emissiveColor,                                 diffuseColor, specularColor, shininess);

The ambient color argument specifies the shape's color when lit by ambient light: this gives the object a uniform color. The emissive color contributes the color that the shape produces (as for a light bulb); frequently, this argument is set to black (equivalent to off). The diffuse color is the color of the object when lit, with its intensity depending on the angle the light beams make with the shape's surface.

The diffuse and ambient colors are often set to be the same, which matches the way real-world objects are colored when lit.


The intensity of the specular color parameter is related to how much the shape reflects from its shiny areas. This is combined with the shininess argument, which controls the size of the reflective highlights.

The specular color is often set to white, matching the specular color produced by most objects in the real world.


In Checkers3D, there are two directional lights, which create two shiny patches on the top of the floating sphere (see Figure 15-1). The floor tiles are unlit since their color is set in the shape's geometry (more on this later in the chapter).

The code in floatingSphere( ) that handles the sphere's appearance is shown here:

     Color3f black = new Color3f(0.0f, 0.0f, 0.0f);     Color3f blue = new Color3f(0.3f, 0.3f, 0.8f);     Color3f specular = new Color3f(0.9f, 0.9f, 0.9f); // near white     Material blueMat= new Material(blue, black, blue, specular, 25.0f);     blueMat.setLightingEnable(true);     Appearance blueApp = new Appearance( );     blueApp.setMaterial(blueMat);

Positioning the Spheres

Positioning a shape is almost always done by placing its scene graph node below a TRansformGroup (see the sphere Group in Figure 15-3). A TRansformGroup can be used to position, rotate, and scale the nodes which lie beneath it, with the transformations defined with Java 3D transform3D objects:

     Transform3D t3d = new Transform3D( );     t3d.set( new Vector3f(0,4,0));     // place at (0,4,0)     TransformGroup tg = new TransformGroup(t3d);     tg.addChild(new Sphere(2.0f, blueApp));            // set the sphere's radius and appearance            // and its normals by default     sceneBG.addChild(tg);

The set( ) method positions the sphere's center at (0, 4, 0) and resets any previous rotations or scalings. set( ) can be used to scale and rotate while resetting the other transformations. The methods setTranslation( ), setScale( ), and setRotation( ) only affect the given transformation.

Unlike some 3D drawing packages, the y-axis in Java 3D is in the vertical direction, while the ground is being defined by the XZ plane, as shown in Figure 15-4.

The position of the sphere is Checkers3D is set to be (0, 4, 0), which places its center four units above the XZ plane.

Figure 15-4. Axes in Java 3D




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