< Zurück | Inhalt | Weiter >

11.6 Using keyboard behaviors

The Java 3D keyboard behavior responds to AWT key−press events (KeyEvent.KEY_ PRESSED and KeyEvent.KEY_RELEASED) and modifies the 4x4 transformation matrix within a TransformGroup. The changes to the TransformGroup can affect the size, position, and rotation of the TranformGroup’s child Nodes.


Keyboard navigation is typically used to simulate moving the viewer of a 3D scene, as opposed to manipulating individual objects within a scene. Mouse behaviors, described in section 11.7, are usually used for object manipulation, although there is considerable overlap between the two areas.


In terms of visual effect, there is no difference between moving the viewer (by attaching the keyboard behavior to a TransformGroup on the view side of the scenegraph) and moving the scene itself (by attaching the keyboard behavior to a root TransformGroup on the scene side of the scenegraph). Figure

11.2 shows a diagram of the typical scenegraph structure that illustrates these two approaches.


image


Figure 11.2 A Java 3D scenegraph. A keyboard behavior can be added either to the scene side of the scenegraph (left) or to the view side of the scenegraph (right)


There are advantages and disadvantages to both approaches. If the keyboard behavior is added on the scene side of the scenegraph, you really are moving the scene. For a single view and with a single ViewPlatform, this will not be noticeable; however, once there are multiple views of the scene or predefined views have been set up using numerous ViewPlatforms, the effects of moving the scene will be noticeable.


The advantage of attaching the behavior to the scene side of the scenegraph is that most keyboard behaviors

expect this to be the case (including the built−in behavior). Remember that the inverse of the multiplication of the Transform3Ds above the ViewPlatform is used to set the view projection matrix. If the keyboard behavior is added on the view side of the scenegraph, the motion and rotation keys are reversed.


If you need to support multiple views, add the keyboard behavior to the view side of the scenegraph and modify the behavior to account for the reversal in movement direction. Otherwise, it will probably be easier to add the behavior to a root TransformGroup on the scene side of the scenegraph.


 

KeyNavigatorBehavior
From AppearanceTest.java
Writing a simple keyboard behavior
From the AvatarTest example, CarSteering.java
Implementing DOOM and DOOM−style keyboard navigation
Loading and creating the world from a 2D map
From KeyNavigateTest.java
Implementing collision detection
From KeyCollisionBehavior.java
From KeyNavigateTest.java
Simple texture image animation for flaming torches
From Light.java
Conclusions