ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Killer Game Programming in Java: A 3D Checkerboard, Part 2
Pages: 1, 2, 3, 4

Viewer Positioning

The scene graph in Figure 15-3 doesn't include the view branch graph; that branch is shown in Figure 15-7.



Figure 15-7
Figure 15-7. The view branch graph

The branch is created by a call to the SimpleUniverse constructor in the WrapCheckers3D( ) constructor:


    su = new SimpleUniverse(canvas3D);

SimpleUniverse offers simplified access to the view branch graph via the ViewingPlatform and Viewer classes, which are mapped to the graph (shown as dotted rectangles in Figure 15-7).

ViewingPlatform is used in initUserPosition( ) to access the TransformGroup above the ViewPlatform node:


    ViewingPlatform vp = su.getViewingPlatform( );
    TransformGroup steerTG = vp.getViewPlatformTransform( );

steerTG corresponds to the TG node in Figure 15-7. Its Transform3D component is extracted and changed with the lookAt( ) and invert( ) methods:


    Transform3D t3d = new Transform3D( );
    steerTG.getTransform(t3d);


    t3d.lookAt( USERPOSN, new Point3d(0,0,0), new Vector3d(0,1,0));
    t3d.invert( );

    steerTG.setTransform(t3d);

lookAt( ) is a convenient way to set the viewer's position in the virtual world. The method requires the viewer's intended position, the point that she is looking at, and a vector specifying the upward direction. In this application, the viewer's position is USERPOSN (the (0, 5, 20) coordinate); she is looking toward the origin (0, 0, 0), and "up" is along the positive y-axis. This is illustrated by Figure 15-8.

Figure 15-8
Figure 15-8. lookAt( ) depicted graphically

invert( ) is required since the position is relative to the viewer rather than an object in the scene.

Viewer Movement

The user is able to move through the scene by utilizing the Java 3D OrbitBehavior utility class in the view graph. A combination of control keys and mouse button presses move and rotate (or orbits) the viewer's position.

The behavior is set up in orbitControls( ) in WrapCheckers3D:


    OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
    orbit.setSchedulingBounds(bounds);
    ViewingPlatform vp = su.getViewingPlatform( );
    vp.setViewPlatformBehavior(orbit);

The REVERSE_ALL flag ensures that the viewpoint moves in the same direction as the mouse.

TIP: Numerous other flags and methods affect the rotation, translation, and zooming characteristics, explained in the OrbitBehavior class documentation.

MouseRotate, MouseTranslate, and MouseZoom are similar behavior classes that appear in many Java 3D examples; their principal difference from OrbitBehavior is that they affect the objects in the scene rather than the viewer.

TIP: Most games, such as first-person shooters (FPS), require greater control over the viewer's movements than these utility behaviors can offer, so I'll be implementing my own behaviors in later chapters.

Pages: 1, 2, 3, 4

Next Pagearrow