(Parte 1 de 6)

Tutorial version 1.5 (Java 3D API v 1.1.2)

Getting Started with the Java 3D™ API

Chapter 4 Interaction

View Platform View Canvas3D Screen3D

Physical Body Physical EnvironmentSTG STG STG STG STG S TG

Dennis J Bouvier K Computing

Module2: Interaction and AnimationChapter 4. Interaction

The Java 3D Tutorial

© Sun Microsystems, Inc. 2550 Garcia Avenue, Mountain View, California 94043-10 U.S.A All Rights Reserved.

The information contained in this document is subject to change without notice.

Some states do not allow the exclusion of implied warranties or the limitations or exclusion of liability for incidental or consequential damages, so the above limitations and exclusion may not apply to you. This warranty gives you specific legal rights, and you also may have other rights which vary from state to state.

Permission to use, copy, modify, and distribute this documentation for NON-COMMERCIAL purposes and without fee is hereby granted provided that this copyright notice appears in all copies.

This documentation was prepared for Sun Microsystems by K Computing (530 Showers Drive, Suite 7-225, Mountain View, CA 94040, 770-982-7881, w.kcomputing.com). For further information about course development or course delivery, please contact either Sun Microsystems or K Computing.

Java, JavaScript, Java 3D, HotJava, Sun, Sun Microsystems, and the Sun logo are trademarks or registered trademarks of Sun Microsystems, Inc. All other product names mentioned herein are the trademarks of their respective owners.

Module 2: Interaction and AnimationChapter 4. Interaction

The Java 3D Tutorial4-i

Table of Contents

4.1Behavior: the Base for Interaction and Animation4-1
4.1.1 Applications of Behavior4-2
4.1.2 Overview of Behavior Classes4-3
4.2 Behavior Basics4-3
4.2.1 Writing a Behavior Class4-4
4.2.2 Using a Behavior Class4-7
4.2.3 Behavior Class API4-10
4.3 Wakeup Conditions: How Behaviors are Triggered4-12
4.3.1 WakeupCondition4-13
4.3.2 WakeupCriterion4-13
4.3.3 Specific WakeupCriterion Classes4-14
4.3.4 WakeupCondition Composition4-24
4.4 Behavior Utility Classes for Keyboard Navigation4-25
4.4.1 Simple KeyNavigatorBehavior Example Program4-26
4.4.2 KeyNavigatorBehavior and KeyNavigator Classes4-28
4.5 Utility Classes for Mouse Interaction4-29
4.5.1 Using the Mouse Behavior Classes4-29
4.5.2 Mouse Behavior Foundation4-31
4.5.3 Specific Mouse Behavior Classes4-32
4.5.4 Mouse Navigation4-34
4.6 Picking4-35
4.6.1 Using Picking Utility Classes4-38
4.6.2 Java 3D API Core Picking Classes4-40
4.6.3 General Picking Package Classes4-4
4.6.4 Specific Picking Behavior Classes4-48
4.7 Chapter Summary4-51

Module2: Interaction and AnimationChapter 4. Interaction

The Java 3D Tutorial4-i

Figure 4-1 Hierarchy of Subclasses of Behavior4-4
Figure 4-2 Recipe for Writing a Custom Behavior Class4-5
Figure 4-3 Recipe for Using a Behavior Class4-8
Figure 4-4 Scene Graph Diagram of the Content Branch Graph Created in SimpleBehaviorApp.java4-8
Figure 4-5 An Alternative Scene Graph Placement for the Behavior Object in SimpleBehaviorApp4-10
Figure 4-6 API Class Hierarchy for Behavior4-1
Figure 4-7 The Java 3D API Class Hierarchy for WakeupCondition and Related Classes4-13
Figure 4-10 The Basic View Branch Graph Showing the View Platform Transform4-26
Figure 4-11Recipe for Using the KeyNavigatorBehavior Utility Class4-26
Figure 4-12 Recipe for Using Mouse Behavior Classes4-30
Figure 4-13 Projection of PickRay in the Virtual World4-36
Figure 4-14 Scene Graph Diagram for a Cube Composed of Discrete Shape3D Plane Objects4-36
Figure 4-15 Recipe for Using Mouse Picking Utility Classes4-38

List of Figures

Table 4-1 Applications of Behavior Categorized by Stimulus and Object of Change4-2
Table 4-2 The 14 Specific WakeupCriterion Classes4-14
Table 4-3 KeyNavigatorBehavior Movements4-28
Table 4-4 Summary of Specific MouseBehavior Classes4-29

List of Tables

Code Fragment 4-1 SimpleBehavior Class in SimpleBehaviorApp.java4-6
Code Fragment 4-2 CreateSceneGraph Method in SimpleBehaviorApp.java4-8
Code Fragment 4-3 Outline of OpenBehavior Class, an Example of Coordinated Behavior Classes4-17
Code Fragment 4-4 Code using OpenBehavior and CloseBehavior, Coordinated Behavior Classes4-17
Code Fragment 4-5 Using the KeyNavigatorBehavior Class (part 1)4-27
Code Fragment 4-6 Using the KeyNavigatorBehavior Class (part 2)4-28
Code Fragment 4-7 Using the MouseRotate Utility Class4-30
Code Fragment 4-8 Using Mouse Behavior Classes for Interactive Navigation of the Virtual World4-35
Code Fragment 4-9 The createSceneGraph Method of the MousePickApp Example Program4-39

List of Code Fragments

Behavior Method Summary4-12
ViewPlatform Method Summary (partial list)4-12
WakeupCondition Method Summary4-13
WakeupCriterion Method Summary4-14
WakeupOnActivation Constructor Summary4-15
WakeupOnAWTEvent Constructor Summary4-15
WakeupOnAWTEvent Method Summary4-16
WakeupOnBehaviorPost Constructor Summary4-16
WakeupOnBehaviorPost Method Summary4-16

Module2: Interaction and AnimationChapter 4. Interaction

WakeupOnCollisionExit Constructor Summary4-19
WakeupOnCollisionExit Method Summary4-19
WakeupOnCollisionMovement Constructor Summary4-20
WakeupOnCollisionMovement Method Summary4-20
WakeupOnDeactivation Constructor Summary4-21
WakeupOnElapsedFrames Constructor Summary4-21
WakeupOnElapsedFrames Method Summary4-21
WakeupOnElapsedTime Constructor Summary4-21
WakeupOnElapsedTime Method Summary4-2
WakeupOnSensorEntry Constructor Summary4-2
WakeupOnSensorEntry Method Summary4-2
WakeupOnSensorExit Constructor Summary4-2
WakeupOnSensorExit Method Summary4-2
WakeupOnTransformChange Constructor Summary4-23
WakeupOnTransformChange Method Summary4-23
WakeupOnViewPlatformEntry Constructor Summary4-23
WakeupOnViewPlatformEntry Method Summary4-23
WakeupOnViewPlatformExit Constructor Summary4-24
WakeupOnViewPlatformExit Method Summary4-24
WakeupAnd Constructor Summary4-24
WakeupOr Constructor Summary4-24
WakeupAndOfOrs Constructor Summary4-25
WakeupOrOfAnds Constructor Summary4-25
KeyNavigatorBehavior Constructor Summary4-29
KeyNavigatorBehavior Method Summary4-29
MouseBehavior Method Summary4-31
Interface MouseBehaviorCallback Method Summary4-31
MouseRotate Constructor Summary4-32
MouseRotate Method Summary4-32
MouseTranslate Constructor Summary4-3
MouseTranslate Method Summary4-3
MouseZoom Constructor Summary4-34
MouseZoom Method Summary4-34
Node Method (partial list)4-37
Node Capabilities Summary (partial list)4-37
PickBounds Constructor Summary4-40
Method Summary4-40
PickPoint Constructor Summary4-41
PickPoint Method Summary4-41
PickRay Constructor Summary4-41
PickRay Method Summary4-41
PickSegment Constructor Summary4-42
PickSegment Method Summary4-42
SceneGraphPath Overview4-42
SceneGraphPath Constructor Summary4-43
SceneGraphPath Method Summary (partial list)4-43
BranchGroup and Locale picking methods for use with PickShape4-4

Module2: Interaction and AnimationChapter 4. Interaction

PickObject Constructor Summary4-45
PickObject Method Summary (partial list)4-45
Interface PickingCallback Method Summary4-46
Intersect Constructor Summary4-47
Intersect Method Summary (partial list)4-47
PickRotateBehavior Constructor Summary4-48
PickRotateBehavior Method Summary4-49
PickTranslateBehavior Constructor Summary4-49
PickTranslateBehavior Method Summary4-50
PickZoomBehavior Constructor Summary4-50
PickZoomBehavior Method Summary4-51

The Java 3D Tutorial4-iv

Preface to Chapter 4

This document is one part of a tutorial on using the Java 3D API. You should be familiar with Java 3D API basics to fully appreciate the material presented in this Chapter. Additional chapters and the full preface to this material are presented in the Module 0 document available at: http://java.sun.com/products/javamedia/3d/collateral

Cover Image

The cover image represents the interaction possible in Java 3D through the use of the mouse. The mouse appears to be connected to the window with a visual, the cube, but the wire proceeds to the scene graph diagram to the Behavior object. The scene graph diagram represents a cube created with six individual shape objects (each of the six faces of the cube is a plane – of course you don't have to do this). The image of the application is from an early version of MouseRotateApp.java, an example program included in the examples jar available with this tutorial. The image of the mouse is from the clip art distributed with Microsoft Office 97.

Module 2: Interaction and Animation

The Java 3D Tutorial4-1

Chapter Objectives After reading this chapter, you’l be able to:

In the previous chapters of the tutorial, the Java 3D virtual universes are almost all static. For Java 3D worlds to be more interesting, and more useful, interaction and animation are necessary. Interaction is when the imagery changes in response to user action. Animation is defined as changes in the imagery without direct user action, and usually corresponds with the passage of time.

In Java 3D, both interaction and animations are specified through the use of the Behavior class. This chapter introduces the Behavior class and explains its use in interactive programs. The next chapter, Animation, continues with animation examples and explanations.

Both interaction and animation are specified with Behavior objects. The Behavior class is an abstract class that provides the mechanism to include code to change the scene graph. The Behavior class, and its descendants, are links to user code providing changes to the graphics and sounds of the virtual universe.

The purpose of a Behavior object in a scene graph is to change the scene graph, or objects in the scene graph, in response to some stimulus. A stimulus can be the press of a key, a mouse movement, the collision of objects, the passage of time, some other event, or a combination of these. Changes produced include adding objects to the scene graph, removing objects from the scene graph, changing attributes of objects in the scene graph, rearranging objects in the scene graph, or a combination of these. The possibilities are only limited by the capabilities of the scene graph objects.

Module2: Interaction and AnimationChapter 4. Interaction

The Java 3D Tutorial4-2

Since a behavior is a link between a stimulus and an action, considering all the combinations of possible stimuli and possible actions is to consider the many applications of Behavior objects. The following table surveys the realm of possibilities with Behavior, listing possible stimuli down the left column and possible changes across the top.

The table does not list all possible applications of Behavior, only the simple ones (one stimulus results in one change). Some combinations of stimulus and change only make sense in a specific setting; these are listed as 'application specific'. Furthermore, combinations of stimuli and combinations of actions are possible.

object of change stimulus (reason for change)


(visual objects change orientation or location)


(visual objects change shape or color)

Scene Graph

(adding, removing, or switching objects)


(change viewing location or direction) user interaction application specificapplication specific navigation collisions visual objects change orientation or location visual objects change appearance in collision visual objects disappear in collision

View changes with collision time animation animation animation animation

View locationbillboardlevel of detail(LOD) application specificapplication specific

In Table 4-1 some of the possible behaviors are spelled out. For example, collision actions are described. Others, such as billboard or level of detail (LOD) behaviors, may not be familiar to you. Below are some quick explanations.

The chart does not include all applications of Behavior; combinations of stimuli and/or changes are not shown. Picking is also implemented using behaviors but is not listed in the table. Although listed in Table 4-1 and implemented in Java 3D API, collision detection is not addressed in this tutorial.

Natural things, such as trees, take a tremendous amount of geometry to accurately represent all of the branches, leaves and bark structure. One alternative is to use a textured polygon instead of the geometry. This technique is sometime referred to as the billboard approach. This is especially true when a behavior is used to automatically orient the textured polygon orthogonal to the viewer such that only the front textured face is viewed. This orienting behavior is called billboard behavior.

individual parts of the visual object represented by the texture would not easily be distinguishedFor the

The billboard approach is effective when the object to be represented by the texture is distant so that the tree example, if the viewer is so distant that branches are hardly distinguishable, it is hardly worth the memory and computation requirements to represent each leaf of the tree. This technique is recommended for any application requiring visually complex objects in a distance. However, if the viewer were able to

Module2: Interaction and AnimationChapter 4. Interaction

The Java 3D Tutorial4-3 approach the billboard, at some distance the lack of depth of the textured polygon would be detected by the viewer.

The level of detail (LOD) behavior has a related application. With LOD, visually complex objects are represented by multiple visual objects of varying levels of detail (hence the name). The visual object representation with the least detail is used when the viewer is far away. The most detailed representation is used when the viewer is close. The LOD behavior automatically switches between the representations based on the objects distance to the viewer.

Both the billboard and level of detail behaviors correspond to classes extended from Behavior which implement these common applications. Other specializations of behavior are possible and several are listed in Figure 4-1. For example, there are several MouseBehavior classes that manipulate a transform in response to mouse movements. Normally the view transform is changed by the mouse behavior to change the view in response to mouse actions.

Also note how the behaviors can chain. For example, mouse movements or key strokes can be used to change the view. In response to the movement of the view, billboard, level of detail, and/or other behaviors may take place. Fortunately, each behavior is specified separately.

Animation Versus Interaction

Since the distinction between animation and interaction used in this tutorial is fairly fine, here is an example to help clarify this distinction. If a user navigates in a program where such a behavior is provided, the view platform will move in response to the keyboard and/or mouse events. The motion of the view platform is an interaction because it is the direct result of the user action. However, other things may change as a result of the view platform motion (e.g., billboard and LOD behaviors). Changes as a result of the view platform motion are indirectly caused by the user and are therefore animations.

The following figure, Figure 4-1, shows specializations of behavior made in the Java 3D API core and utility packages. User defined specializations of Behavior are also possible and are only limited in functionality by the programmer's imagination. This module of the tutorial covers each of the classes in Figure 4-1. This chapter covers the shaded classes in the figure; Chapter 5 covers the remaining classes. This figure does not present the total coverage of the Java 3D API in Chapters 4 and 5; each chapter presents more than the classes in this figure.

As explained in the previous section, Behavior classes are used in many Java 3D applications and in many ways. It is important to understand the workings and programming considerations of the behavior class. This section explains the Behavior class, gives a recipe for programming a custom behavior class, and gives a simple example application using a behavior class.

Module2: Interaction and AnimationChapter 4. Interaction

The Java 3D Tutorial4-4


Billboard Interpolator LOD DistanceLOD

ColorInterpolator RotPosPathScaleInterpolator

MouseBehavior MouseRotate

MouseZoom MouseTranslate KeyNavigatorBehavior


PickZoomBehavior PickTranslateBehaviorPickMouseBehavior

This section explains how to write a custom behavior class. You know from Section 4.1 that there are behavior classes you can use without writing a class. However, in seeing how to create a Behavior class you learn how behaviors work. So even if you only plan to use a behavior class, you might want to read this section. Also, the class written in this section is used in the next section. (If you don't plan to write a Behavior class you can skip this section for now.)

Mechanics of Behaviors

A custom behavior class implements the initialization and processStimulus methods from the abstract Behavior class. Of course, the custom behavior class also has at least one constructor and may have other methods as well.

Most behaviors will act on a scene graph object to affect the behavior. In Table 4-1, the object a behavior acts upon is refered to as the object of change. It is through this object, or objects, that the behavior affects the virtual world. While it is possible to have a behavior that does not have an object of change, most do.

The behavior needs a reference to its object(s) of change to be able to make the behavioral changes. The constructor can be used to set the reference to the object of change. If it does not, another method in the custom behavior class must store this information. In either case, the reference is made at the time the scene graph is being constructed, which is the first computation of the behavior.

Module2: Interaction and AnimationChapter 4. Interaction

The Java 3D Tutorial4-5

The initialization method is invoked when the scene graph containing the behavior class becomes live. The initialization method is responsible for setting the initial trigger event for the behavior and setting the initial condition of the state variables for the behavior. The trigger is specified as a WakeupCondition object, or a combination of WakeupCondition objects.

The processStimulus method is invoked when the trigger event specified for the behavior occurs. The processStimulus method is responsible for responding to the event. As many events may be encoded into a single WakeupCondition object (e.g., a variety of keyboard actions may be encoded in a WakeupOnAWTEvent), this includes decoding the event. The processStimulus method responds to the stimulus, usually by changing its object of change, and, when appropriate, resets the trigger.

The information in this section, Mechanics of Behaviors, is summarized in a recipe for writing custom behavior classes. Figure 4-2 presents this recipe.

1.write (at least one) constructor store a reference to the object of change 2.override public void initialization() specify initial wakeup criteria (trigger) 3.override public void processStimulus() decode the trigger condition act according to the trigger condition reset trigger as appropriate

The recipe of Figure 4-2 shows the basic steps for creating a custom behavior class. Complex behaviors may require more programming than is described in the recipe. Using a behavior object is another issue and is discussed in Section 4.2.2. But before using a behavior, this recipe is used in creating the following example custom behavior.

Example Custom Behavior Class: SimpleBehavior

As an example of using the custom behavior class recipe of Figure 4-2, this section goes through the process of writing a custom behavior class. For the example custom behavior, the class will implement a simple behavior of making something rotate in response to a keyboard key press.

To create such a behavior class, all that is needed is a reference to a TransformGroup (the object of change for this class), and an angle variable. In response to a key press, the angle variable is changed and the angle of the target TransformGroup is set to the angle. Since the behavior will act on a TransformGroup object, what is being rotated is not an issue.

To create this class nothing more than the three essential programming ingredients listed in the recipe are needed: a constructor, the initialization method, and the processStimulus method. The constructor will store a reference to the TransformGroup object of change. The initialization method sets the initial trigger to WakeOnAWTEvent, and sets the rotation angle to zero. As mentioned above, the stimulus to a behavior is specified as a WakeupCondition object. Section 4.3 introduces WakeupCondition classes.

Since there is only one possible triggering wakeup condition, the processStimulus method does not decode the triggering wakeup condition. It is possible to further decode the key press event to determine which key, or combination of keys, was pressed.

(Parte 1 de 6)