< Zurück | Inhalt | Weiter >

6.3 SimpleUniverse

To simplify creating the View side of the scenegraph, Sun has provided the SimpleUniverse class (figure 6.2). SimpleUniverse is defined in the com.sun.j3d.utils package and as such should not be considered part of the core Java 3D API. The SimpleUniverse class hides some of the complexity of manually defining the View side of the scenegraph at the expense of the flexibility of using the core API classes only.

image


Figure 6.2 The SimpleUniverse class encapsulates the details of the view side of the scenegraph (lower branch). The ViewingPlatform has been highlighted and contains a MultiTransformGroup composed of two TransformGroups. Attached to the ViewPlatform are a PlatformGeometry Group and a ViewerAvatar Group. By attaching a Key behavior to one of the TransformGroups in the MultiTransformGroup, the View, PlatformGeometry, and ViewerAvatar can all be moved simultaneously and rendered into the Canvas3D attached to the view


SimpleUniverse is a bit of a misnomer since the class is anything but simple, and this can cause initial confusion because of the plethora of support classes that it relies upon. SimpleUniverse introduces five new (non−core−API) classes:


Viewer, a container class that keeps references to the following:


ViewerAvatar, a representation of the viewer of the scene.

Canvas3D, used for rendering the scene.

AWT Frame contains the Canvas3D used for rendering.

PhysicalBody references the view’s PhysicalBody. PhysicalEnvironment references the view’s PhysicalEnvironment. View is used for the viewer of the scene.

ViewingPlatform (extends BranchGroup) helps set up the View side of the scenegraph by creating a hierarchy of TransformGroups above the ViewPlatform where the view is attached to the scenegraph. The hierarchy of TransformGroups is encapsulated in a MultiTransformGroup. In this way a series of transformations can be applied to the view irrespective of transformations that are applied on the geometry side of the scenegraph.

ViewerAvatar (extends BranchGroup) contains the geometry used to render the viewer’s virtual self in the virtual environment.

MultiTransformGroup is a simple encapsulation of a Vector of TransformGroups.

PlatformGeometry (extends BranchGroup) contains the geometry associated with the viewer’s ViewingPlatform. For example, in a multiplayer gaming scenario, each player might be able to drive one of several vehicles. A ViewingPlatform would represent each vehicle in the scenegraph, and the geometry for the vehicle would be attached to the ViewingPlatform. The player would be represented by geometry attached to the ViewerAvatar, which in turn would govern another player’s view of the player, while the ViewingPlatform would contain the geometry to describe the internal characteristics of the vehicle the player was riding in. By attaching the view to different ViewingPlatforms, the player can move between vehicles.


In the PlatformTest example, two avatars (Dan and Jim) and three views are created: overhead, Dan’s, and Jim’s (figures 6.3–6.5). Each Avatar is assigned ViewerAvatar geometry (a simple Cone pointing in the direction of view) and PlatformGeometry (a text label to identify the avatar).

image


Figure 6.3 Overhead view of the virtual arena with two avatars: Dan and Jim


image


Figure 6.4 The view for avatar Jim—Jim can see Dan


image


Figure 6.5 The view for avatar Dan—Dan can see Jim


The example is too lengthy to be included in its entirety but some illustrative excerpts have been extracted:

 

From PlatformTest.java
From AvatarTest.java (see also PlatformTest.java)