Docsity
Docsity

Prepare-se para as provas
Prepare-se para as provas

Estude fácil! Tem muito documento disponível na Docsity


Ganhe pontos para baixar
Ganhe pontos para baixar

Ganhe pontos ajudando outros esrudantes ou compre um plano Premium


Guias e Dicas
Guias e Dicas


Tutorial Java3D Capitulo 4 - Interação, Notas de estudo de Análise de Sistemas de Engenharia

Tutorial Java3D - Interação - Usando mouse, teclado e eventos para interagir com cenas 3D. 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.

Tipologia: Notas de estudo

Antes de 2010

Compartilhado em 10/04/2010

alessandro-borges-10
alessandro-borges-10 🇧🇷

3

(2)

12 documentos

1 / 58

Toggle sidebar

Esta página não é visível na pré-visualização

Não perca as partes importantes!

bg1
Tutorial version 1.5 (Java 3D API v 1.1.2)
Getting Started with
the Java 3D API
Chapter 4
Interaction
BG
TG
View Platform
View
Canvas3D
Screen3D
Physical Body Physical Environment
S
TG
S
TG
S
TG
S
TG
S
TG
S
TG
TG
BG
B
Dennis J Bouvier
K Computing
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

Pré-visualização parcial do texto

Baixe Tutorial Java3D Capitulo 4 - Interação e outras Notas de estudo em PDF para Análise de Sistemas de Engenharia, somente na Docsity!

Tutorial version 1.5 (Java 3D API v 1.1.2)

Getting Started with

the Java 3D

API

Chapter 4

Interaction

BG

TG

View Platform

View Canvas3D Screen3D

S Physical Body^ Physical Environment

TG

S

TG

S

TG

S

TG

S

TG

S

TG

TG

BG

B

Dennis J Bouvier

K Computing

The Java 3D Tutorial

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

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

SUN MICROSYSTEMS PROVIDES THIS MATERIAL "AS IS" AND MAKES NO WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SUN MICROSYSTEMS SHALL NOT BE LIABLE FOR ERRORS CONTAINED HEREIN OR FOR INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS IN CONNECTION WITH THE FURNISHING, PERFORMANCE OR USE OF THIS MATERIAL, WHETHER BASED ON WARRANTY, CONTRACT, OR OTHER LEGAL THEORY).

THIS DOCUMENT COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS. CHANGES ARE PERIODICALLY MADE TO THE INFORMATION HEREIN; THESE CHANGES WILL BE INCORPORATED IN NEW EDITIONS OF THE PUBLICATION. SUN MICROSYSTEMS, INC. MAY MAKE IMPROVEMENTS AND/OR CHANGES IN THE PRODUCT(S) AND/OR PROGRAM(S) DESCRIBED IN THIS PUBLICATION AT ANY TIME.

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, www.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.

The Java 3D Tutorial 4-ii

  • Interaction ...........................................................................................................................................4- Chapter 4:
    • 4.1 Behavior: the Base for Interaction and Animation ......................................................................4-
      • 4.1.1 Applications of Behavior ...................................................................................................4-
      • 4.1.2 Overview of Behavior Classes ...........................................................................................4-
    • 4.2 Behavior Basics ........................................................................................................................4-
      • 4.2.1 Writing a Behavior Class ..................................................................................................4-
      • 4.2.2 Using a Behavior Class .....................................................................................................4-
      • 4.2.3 Behavior Class API .........................................................................................................4-
    • 4.3 Wakeup Conditions: How Behaviors are Triggered ..................................................................4-
      • 4.3.1 WakeupCondition ...........................................................................................................4-
      • 4.3.2 WakeupCriterion .............................................................................................................4-
      • 4.3.3 Specific WakeupCriterion Classes ...................................................................................4-
      • 4.3.4 WakeupCondition Composition 4-
    • 4.4 Behavior Utility Classes for Keyboard Navigation ...................................................................4-
      • 4.4.1 Simple KeyNavigatorBehavior Example Program ............................................................4-
      • 4.4.2 KeyNavigatorBehavior and KeyNavigator Classes ...........................................................4-
    • 4.5 Utility Classes for Mouse Interaction ......................................................................................4-
      • 4.5.1 Using the Mouse Behavior Classes ..................................................................................4-
      • 4.5.2 Mouse Behavior Foundation............................................................................................ 4-
      • 4.5.3 Specific Mouse Behavior Classes ....................................................................................4-
      • 4.5.4 Mouse Navigation ...........................................................................................................4-
    • 4.6 Picking ...................................................................................................................................4-
      • 4.6.1 Using Picking Utility Classes 4-
      • 4.6.2 Java 3D API Core Picking Classes 4-
      • 4.6.3 General Picking Package Classes .....................................................................................4-
      • 4.6.4 Specific Picking Behavior Classes ...................................................................................4-
    • 4.7 Chapter Summary ...................................................................................................................4-
    • 4.8 Self Test .................................................................................................................................4-
  • Figure 4-1 Hierarchy of Subclasses of Behavior ....................................................................................4- List of Figures
  • Figure 4-2 Recipe for Writing a Custom Behavior Class........................................................................4-
  • Figure 4-3 Recipe for Using a Behavior Class........................................................................................4-
  • Figure 4-4 Scene Graph Diagram of the Content Branch Graph Created in SimpleBehaviorApp.java......4-
  • Figure 4-5 An Alternative Scene Graph Placement for the Behavior Object in SimpleBehaviorApp.......4-
  • Figure 4-6 API Class Hierarchy for Behavior ......................................................................................4-
  • Figure 4-7 The Java 3D API Class Hierarchy for WakeupCondition and Related Classes. ....................4-
  • Figure 4-10 The Basic View Branch Graph Showing the View Platform Transform .............................4-
  • Figure 4-11Recipe for Using the KeyNavigatorBehavior Utility Class..................................................4-
  • Figure 4-12 Recipe for Using Mouse Behavior Classes ........................................................................4-
  • Figure 4-13 Projection of PickRay in the Virtual World ......................................................................4-
  • Figure 4-14 Scene Graph Diagram for a Cube Composed of Discrete Shape3D Plane Objects..............4-
  • Figure 4-15 Recipe for Using Mouse Picking Utility Classes................................................................4-
  • Table 4-1 Applications of Behavior Categorized by Stimulus and Object of Change..............................4- List of Tables
  • Table 4-2 The 14 Specific WakeupCriterion Classes............................................................................4-
  • Table 4-3 KeyNavigatorBehavior Movements......................................................................................4-
  • Table 4-4 Summary of Specific MouseBehavior Classes......................................................................4-
  • Code Fragment 4-1 SimpleBehavior Class in SimpleBehaviorApp.java ..................................................4- List of Code Fragments
  • Code Fragment 4-2 CreateSceneGraph Method in SimpleBehaviorApp.java...........................................4-
  • Code Fragment 4-3 Outline of OpenBehavior Class, an Example of Coordinated Behavior Classes.......4-
  • Code Fragment 4-4 Code using OpenBehavior and CloseBehavior, Coordinated Behavior Classes........4-
  • Code Fragment 4-5 Using the KeyNavigatorBehavior Class (part 1) ....................................................4-
  • Code Fragment 4-6 Using the KeyNavigatorBehavior Class (part 2) ....................................................4-
  • Code Fragment 4-7 Using the MouseRotate Utility Class .....................................................................4-
  • Code Fragment 4-8 Using Mouse Behavior Classes for Interactive Navigation of the Virtual World. ....4-
  • Code Fragment 4-9 The createSceneGraph Method of the MousePickApp Example Program. ..............4-
  • Behavior Method Summary.................................................................................................................4- List of Reference Blocks
  • ViewPlatform Method Summary (partial list) ......................................................................................4-
  • WakeupCondition Method Summary ...................................................................................................4-
  • WakeupCriterion Method Summary ....................................................................................................4-
  • WakeupOnActivation Constructor Summary .......................................................................................4-
  • WakeupOnAWTEvent Constructor Summary......................................................................................4-
  • WakeupOnAWTEvent Method Summary ............................................................................................4-
  • WakeupOnBehaviorPost Constructor Summary...................................................................................4-
  • WakeupOnBehaviorPost Method Summary .........................................................................................4-
  • WakeupOnCollisionEntry Constructor Summary.................................................................................4-
  • WakeupOnCollisionExit Constructor Summary ...................................................................................4- The Java 3D Tutorial 4-iii
  • WakeupOnCollisionExit Method Summary .........................................................................................4-
  • WakeupOnCollisionMovement Constructor Summary .........................................................................4-
  • WakeupOnCollisionMovement Method Summary................................................................................4-
  • WakeupOnDeactivation Constructor Summary....................................................................................4-
  • WakeupOnElapsedFrames Constructor Summary................................................................................4-
  • WakeupOnElapsedFrames Method Summary ......................................................................................4-
  • WakeupOnElapsedTime Constructor Summary ...................................................................................4-
  • WakeupOnElapsedTime Method Summary..........................................................................................4-
  • WakeupOnSensorEntry Constructor Summary ....................................................................................4-
  • WakeupOnSensorEntry Method Summary...........................................................................................4-
  • WakeupOnSensorExit Constructor Summary ......................................................................................4-
  • WakeupOnSensorExit Method Summary.............................................................................................4-
  • WakeupOnTransformChange Constructor Summary ...........................................................................4-
  • WakeupOnTransformChange Method Summary..................................................................................4-
  • WakeupOnViewPlatformEntry Constructor Summary .........................................................................4-
  • WakeupOnViewPlatformEntry Method Summary................................................................................4-
  • WakeupOnViewPlatformExit Constructor Summary ...........................................................................4-
  • WakeupOnViewPlatformExit Method Summary..................................................................................4-
  • WakeupAnd Constructor Summary .....................................................................................................4-
  • WakeupOr Constructor Summary .......................................................................................................4-
  • WakeupAndOfOrs Constructor Summary............................................................................................4-
  • WakeupOrOfAnds Constructor Summary............................................................................................4-
  • KeyNavigatorBehavior Constructor Summary .....................................................................................4-
  • KeyNavigatorBehavior Method Summary............................................................................................4-
  • MouseBehavior Method Summary.......................................................................................................4-
  • Interface MouseBehaviorCallback Method Summary...........................................................................4-
  • MouseRotate Constructor Summary ....................................................................................................4-
  • MouseRotate Method Summary ..........................................................................................................4-
  • MouseTranslate Constructor Summary................................................................................................4-
  • MouseTranslate Method Summary ......................................................................................................4-
  • MouseZoom Constructor Summary .....................................................................................................4-
  • MouseZoom Method Summary ...........................................................................................................4-
  • Node Method (partial list) ...................................................................................................................4-
  • Node Capabilities Summary (partial list) .............................................................................................4-
  • PickShape...........................................................................................................................................4-
  • PickBounds Constructor Summary ......................................................................................................4-
  • Method Summary................................................................................................................................4-
  • PickPoint Constructor Summary..........................................................................................................4-
  • PickPoint Method Summary................................................................................................................4-
  • PickRay Constructor Summary ...........................................................................................................4-
  • PickRay Method Summary..................................................................................................................4-
  • PickSegment Constructor Summary.....................................................................................................4-
  • PickSegment Method Summary...........................................................................................................4-
  • SceneGraphPath Overview..................................................................................................................4-
  • SceneGraphPath Constructor Summary...............................................................................................4-
  • SceneGraphPath Method Summary (partial list) ..................................................................................4-
  • BranchGroup and Locale picking methods for use with PickShape .......................................................4-
  • PickMouseBehavior Method Summary................................................................................................4-

Module 2: Interaction and Animation

4

Interaction

Chapter Objectives

After reading this chapter, you’ll be able to:

  • Appreciate the Behavior Class as the foundation for interaction and animation
  • Create custom behavior classes
  • Incorporate behavior objects into virtual worlds to provide interaction
  • Use utility classes for keyboard navigation
  • Use utility classes for mouse interaction
  • Use utility picking classes

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.

4.1 Behavior: the Base for Interaction and Animation

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.

C H A P T E R

4.1.1 Applications of Behavior

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.

Table 4-1 Applications of Behavior Categorized by Stimulus and Object of Change

object of change

stimulus (reason for change)

TransformGroup (visual objects change orientation or location)

Geometry (visual objects change shape or color)

Scene Graph (adding, removing, or switching objects)

View (change viewing location or direction)

user interaction application specific application 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 location billboard^ level of detail (LOD)

application specific application 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.

The billboard approach is effective when the object to be represented by the texture is distant so that the individual parts of the visual object represented by the texture would not easily be distinguished. For 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

Behavior

Billboard

Interpolator

LOD

DistanceLOD

ColorInterpolator

RotPosPathScaleInterpolator

MouseBehavior

MouseRotate

MouseZoom

MouseTranslate

KeyNavigatorBehavior

PickRotateBehavior

PickZoomBehavior

PickMouseBehavior PickTranslateBehavior

Figure 4-1 Hierarchy of Subclasses of Behavior

4.2.1 Writing a Behavior Class

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.

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

Figure 4-2 Recipe for Writing a Custom Behavior Class

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.

The processStimulus method always increments the angle variable, then uses it to adjust the TransformGroup object of change. The last job of the processStimulus method is to reset the trigger. In this example, the trigger is always reset to a key press. Behaviors can change their trigger event over time

by class methods. The behavior class could be further customizable with a method for setting a specific key, or set of keys, that it will respond to.

Another definite improvement in the class would prevent overflow of the angle variable. In the current class, the value of angle could grow without bound even though values of 0.0 to 2Π are all that is necessary. Although unlikely, it is possible for this variable to overflow and cause a run time exception.

Programming Pitfalls of Writing Behavior Classes

In the three steps of the custom behavior class recipe, the two most likely programming mistakes are:

  • forgetting to set and reset the behavior trigger, and
  • not returning from the behavior class methods.

Obviously, if an initial trigger is not set in the initialization method, the behavior will never be invoked. A little less obvious is that the trigger must be set again in the processStimulus method if a repeat behavior is desired.

Since both the initialization and processStimulus methods are called by the Java 3D system, they must return to allow the rendering to continue. For example, if a spinning behavior were desired, the angle and the TransformGroup would need to be updated periodically. If your behavior implemented this behavior without spawning a thread, nothing further would be rendered. Also, there is a much better way to achieve this type of behavior^1.

4.2.2 Using a Behavior Class

Finding or writing the appropriate behavior class for your application is the beginning of writing an interactive Java 3D program. This section covers the programming issues in adding behavior objects to programs.

The first step in adding a behavior involves making sure the scene graph makes provisions for the behavior. For example, to use the SimpleBehavior class from the previous section there must be a TransformGroup in the scene graph above the object(s) to be rotated. Many behaviors need only a single TransformGroup object; however, scene graph requirements for a behavior is application and behavior dependent and may be more complex.

Having established the support for a behavior, an instance of the class must be added to the scene graph. Without being a part of a live scene graph, there is no way a behavior can be initialized. In fact, a behavior object that is not part of the scene graph will become garbage and be eliminated on the next garbage collection.

The last step for adding a behavior is to provide a scheduling bounds for the behavior. To improve efficiency, Java 3D uses the scheduling bounds to perform execution culling. Behavior is only active when its scheduling bounds intersects a ViewPlatform's activation volume. Only active behaviors are eligible to receive stimuli. In this way, stimuli can be ignored for some behaviors. The programmer has control over the execution culling through the selection of the scheduling bounds of the behavior.

Figure 4-3 summarizes the steps for using a behavior object in a recipe.

(^1) A behavior based on time alone is an animation, and as such is discussed in Chapter 5.

  1. prepare the scene graph (by adding a TransformGroup or other necessary objects)
  2. insert behavior object in the scene graph, referencing the object of change
  3. specify a scheduling bounds (or SchedulingBoundingLeaf)
  4. set write (and read) capabilities for the target object (as appropriate)

Figure 4-3 Recipe for Using a Behavior Class

The following code fragment, Code Fragment 4-2, is annotated with the step numbers from the recipe. This code fragment is an except from the SimpleBehaviorApp example program found in the examples/Interaction directory. In this same application the SimpleBehavior class, found in Code Fragment 4-2, is defined. Code Fragment 4-2 continues where Code Fragment 4-1 ended and the line numbers are sequential for the two code fragments.

  1. public BranchGroup createSceneGraph() {
  2. // Create the root of the branch graph
  3. BranchGroup objRoot = new BranchGroup();
  4. TransformGroup objRotate = new TransformGroup();

42. è objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

  1. å
  2. objRoot.addChild(objRotate);
  3. objRotate.addChild(new ColorCube(0.4));
  4. SimpleBehavior myRotationBehavior = new SimpleBehavior(objRotate);

48. é myRotationBehavior.setSchedulingBounds(new BoundingSphere());

49. ç objRoot.addChild(myRotationBehavior);

  1. // Let Java 3D perform optimizations on this scene graph.

  2. objRoot.compile();

  3. return objRoot;

  4. } // end of CreateSceneGraph method of SimpleBehaviorApp

Code Fragment 4-2 CreateSceneGraph Method in SimpleBehaviorApp.java

Very little code is needed to complete the program started in Code Fragment 4-1 and 4-2. The complete program, SimpleBehaviorApp, is found in the examples/Interaction directory. The complete application renders a ColorCube object in a static scene until a keyboard key is pressed. In response to any key press, the ColorCube rotates 0.1 radians (about 6°). Figure 4-4 shows the scene graph diagram for the content branch graph of this application.

BG

TG

B

ColorCube

objRoot

objRotate

myRotationBehavior

Figure 4-4 Scene Graph Diagram of the Content Branch Graph Created in SimpleBehaviorApp.java.

solution uses a BoundingLeaf object for the scheduling bounds. Consult Section 3.7 or the Java 3D API Specification for information on the BoundingLeaf class.

BG

TG B

ColorCube

objRoot

objRotate myRotationBehavior

Figure 4-5 An Alternative Scene Graph Placement for the Behavior Object in SimpleBehaviorApp.

Behavior Class Design Recommendations

The mechanics of writing a custom behavior are simple. However, you should be aware that a poorly written behavior can degrade rendering performance^3. While there are other considerations in writing a behavior, two things to avoid are: memory burn and unnecessary trigger conditions.

'Memory burn' is the term for unnecessarily creating objects in Java. Excessive memory burn will cause garbage collections. Occasional pauses in rendering is typical of memory burn since during the garbage collection, the rendering will stop^45.

Behavior class methods are often responsible for creating memory burn problems. For example, in Code Fragment 4-1 the processStimulus uses a 'new' in the invocation of wakeupOn (line 24). This causes a new object to be created each time the method is invoked. That object becomes garbage each time the behavior is triggered.

Potential memory burn problems are normally easy to identify and avoid. Look for any use of 'new' in the code to find the source of memory burn problems. Whenever possible, replace the use of the new with code that reuses an object.

Later, in Section 4.3,the classes and methods used in setting the trigger conditions for a behavior object are discussed. In that section, you will see it is possible to set a trigger condition that will wake a behavior every frame of the rendering. If there is nothing for the behavior to do, this is an unnecessary waste of processor power invoking the behavior's processStimulus method. Not to say that there isn't a good reason to trigger a behavior on every frame, just make sure you have the reason.

4.2.3 Behavior Class API

This section presents the detail of the Behavior class API. Figure 4-6 shows the Java 3D API class hierarchy including the Behavior class. As an abstract class, Behavior must be extended before a behavior

(^3) The amount of performance degradation depends heavily on the execution environment. If you plan to distribute

your applications, consider users with software rendering environments. (^4) How often and how regular the pause depends on the execution environment.

(^5) You can diagnose a memory burn problem by invoking the Java virtual machine with the -verbose:gc command

line option. If memory burn is the cause for rendering pauses, then the garbage collection report produced to the console will coinside with the pauses.

object can be instantiated. Of course, you can write your own custom behavior classes. In addition, there are many existing behaviors in the Java 3D API utility packages. As an extension of Leaf, instances of classes that extend Behavior can be children of a group in a scene graph.

java.lang.Object

javax.media.j3d.SceneGraphObject

javax.media.j3d.Node

javax.media.j3d.Leaf

javax.media.j3d.Behavior

Figure 4-6 API Class Hierarchy for Behavior

As documented in Section 4.2.1, the processStimulus and initialize methods provide the interface Java 3D uses to incorporate behaviors in the virtual universe. The other Behavior class methods are discussed below. All Behavior class methods are listed in the Behavior Method Summary reference block on the next page.

The wakeupOn method is used in the initialize and processStimulus methods to set the trigger for the behavior. The parameter to this method is a WakeupCondition object. WakeupCondition, and related classes, are presented in Section 4.3.

The postId method allows a behavior to communicate with another method. One of the wakeup conditions is WakeupOnBehaviorPost. Behavior objects can be coordinated to create complex collaborations using the postId method in conjunction with appropriate WakeupOnBehaviorPost conditions. See page 4-16 for information on the WakeupOnBehaviorPost class

The setEnable method provides a way to disable a behavior even when the bounds makes it active. The default is true (i.e., the behavior object is enabled).

A behavior object is active only when its scheduling bounds intersects the activation volume of a View. Since it is possible to have multiple views in a virtual universe, a behavior can be made active by more than one view.

The getView method is useful with behaviors that rely on per-View information (e.g., Billboard, LOD) and with behaviors in general in regards to scheduling. This method returns a reference to the primary View object currently associated with the behavior. There is no corresponding setView method. The "primary" view is defined to be the first View attached to a live ViewPlatform, if there is more than one active View. So, for instance, Billboard behaviors would be oriented toward this primary view, in the case of multiple active views into the same scene graph.

allow the composition of multiple wakeup conditions in a single wakeup condition. Figure 4-7 shows the class hierarchy for these classes.

java.lang.Object

javax.media.j3d.WakeupCondition

WakeupOr

WakeupAnd

WakeupAndOfOrs

WakeupOrOfAnds

WakeupCriterion

WakeupOnActivation WakeupOnAWTEvent WakeupOnBehaviorPost WakeupOnCollisionEntry WakeupOnCollisionExit WakeupOnCollisionMovement WakeupOnDeactivation WakeupOnElapsedFrames WakeupOnElapsedTime WakeupOnSensorEntry WakeupOnSensorExit WakeupOnTransformChange WakeupOnViewPlatformEntry WakeupOnViewPlatformExit

Figure 4-7 The Java 3D API Class Hierarchy for WakeupCondition and Related Classes.

A behavior object's wakeup condition can be specified as one of the specific wakeup criterion or as a combination of criteria using the wakeup composition classes. The following sections describe WakeupCondition and its descendant classes.

4.3.1 WakeupCondition

The WakeupCondition class provides two methods. The first method, allElements, returns the enumeration list of all wakeup criterion for the WakeupCondition object. The other method, triggeredElements, enumerates which of the wakeup criterion has caused the behavior to be triggered. This method may be useful in the processStimulus method of a Behavior object.

WakeupCondition Method Summary

The WakeupCondition abstract class is the base for all wakeup classes. It provides the following two methods.

Enumeration allElements() Returns an enumeration of all WakeupCriterion objects in this Condition.

Enumeration triggeredElements() Returns an enumeration of all triggered WakeupCriterion objects in this Condition.

4.3.2 WakeupCriterion

WakeupCriterion is an abstract method for the 14 specific wakeup criterion classes. WakeupCondition provides only one method: hasTriggered. You probably don't need to use this method as the triggeredElements method of WakeupCondition performs this operation for you.

WakeupCriterion Method Summary

boolean hasTriggered() Returns true if this criterion triggered the wakeup.

4.3.3 Specific WakeupCriterion Classes

Table 4-2 presents the 14 specific WakeupCriterion classes. These classes are used to specify the wakeup conditions for behavior objects. Instances of these classes are used individually or in combinations when using the wakeup condition composition classes presented in Section 4.3.4.

Table 4-2 The 14 Specific WakeupCriterion Classes

Wakeup Criterion Trigger page

WakeupOnActivation on first detection of a ViewPlatform's activation volume intersecting with this object's scheduling region.

WakeupOnAWTEvent when a specific AWT event occurs 4-

WakeupOnBehaviorPost when a specific behavior object posts a specific event 4-

WakeupOnCollisionEntry on the first detection of the specified object colliding with any other object in the scene graph

WakeupOnCollisionExit when the specified object no longer collides with any other object in the scene graph

WakeupOnCollisionMovement when the specified object moves while in collision with any other object in the scene graph

WakeupOnDeactivation when a ViewPlatform's activation volume no longer intersects with this object's scheduling region

WakeupOnElapsedFrames when a specific number of frames have elapsed 4-

WakeupOnElapsedTime when a specific number of milliseconds have elapsed 4-

WakeupOnSensorEntry on first detection of any sensor intersecting the specified boundary

WakeupOnSensorExit when a sensor previously intersecting the specified boundary no longer intersects the specified boundary

WakeupOnTransformChange when the transform within a specified TransformGroup changes

WakeupOnViewPlatformEntry on first detection of a ViewPlatform activation volume intersecting with the specified boundary

WakeupOnViewPlatformExit when a View activation volume no longer intersects the specified boundary

Reference blocks for the individual WakeupCriterion classes appear on the next ten pages. Some WakeupCriterion classes have very simple APIs. For example, the WakeupOnActivation class has just one constructor. The scheduling region, a parameter of this wakeup condition, is specified in the behavior object that uses this criterion.