In Chapter VII, we discussed how animation can be applied in Java 3D to increase the visual impact of a virtual 3D world and illustrate the dynamic of the various 3D objects to the user (Tate, Moreland, & Bourne, 2001). In this chapter, we will continue this process to make the virtual 3D universe even more interesting and appealing by adding the ability for the user to interact with the 3D objects being rendered. In Java 3D, both animation and interaction can be accomplished through the use of the behavior class. Having discussed how this class helps to carry out animation in the last chapter, we will now concentrate on the mechanism of using behavior class to achieve interaction. Technically, the behavior class is an abstract class with mechanisms for the scene graph to be changed. Being an extension of the leaf class, it can also be a part of a normal scene. In particular, it may be a leaf node in the scene graph and can be placed in the same way as geometry is placed. For instance, in an application where it is necessary to render and control a rotating cube, the rotation behavior for the animation and interaction can be placed under the same transform group as the geometry object for rendering the cube. The main objective of adding a behavior object in a scene graph is of course to change the scene graph in response to a stimulus in the form of, say, pressing a key, moving a mouse, colliding objects, or a combination of these and other events. The change in the virtual 3D world may consist of translating or rotating objects, removing some objects, changing the attributes of others, or any other desirable outcomes in the specific application.
In Java 3D, all behavior must be based on extending the behavior base class. Specifically, to create an animation or interaction behavior, this base class must first be appropriately extended before it is added to the appropriate position in the scene graph for changing the associated visual objects.
The object that a behavior will act upon is called the object of change. It is through this object that the behavior will be able to change the virtual world. Specifically, the following shows the general code development procedure for creating a custom behavior class.
Create the relevant constructors with references to the object of change. Any behavior created will need a reference to an object of change so that the system will know the 3D objects that will be affected by the behavior.
Specify the initial wakeup criteria using the initialization() method. The system will invoke this method when the scene graph containing the behavior class becomes live. It sets the initial conditions or triggers under which the behavior should be executed.
Decode the trigger condition using the processStimulus() method, and act upon this appropriately. In other words, we need to determine the condition or event, be it a key stroke, mouse click, mouse movement, or other relevant events, being activated and then carry out appropriate processing.
The processStimulus() method will be invoked during runtime by the system when the appropriate trigger conditions have occurred. Initially, the trigger conditions set in the initialization() method will be used. Subsequently, the trigger conditions may be changed in the processStimulus() method itself after it has been invoked.
Note that the calling of processStimulus() depends on the stimulus and many events may be encoded as a single trigger condition. For example, since different mouse actions such as clicking or moving are taken as a single WakeupOnAWTEvent, processStimulus() must carry out some appropriate decoding to find out the specific user input, say, right click or left click. After this, relevant changes to the object of change can be made, resulting in changes in the objects in the virtual 3D world.
With valid stimulus, the invocation of processStimulus() will be invoked by the system between the rendering of adjacent frames. Since the rendering of complicated 3D objects may be compute and system intensive, careful consideration must be taken to ensure that the processing will not result in a substantial reduction of the frame rate or animation speed.
The code fragment in Figure 1 gives an example where a custom behavior class is created for moving a cube upwards when a mouse button is clicked. The example includes the three basic steps outlined in the previous code development procedure.