GameSpawn Site Map
GameSpawn Update mailing list
Lee's Lessons

Basic Behavior 2


Yet more on Behaviors! Whoo! This can be a complicated topic so I will delve just slightly more into it. Any more and it would be beyond the scope of a beginning example. I will get MUCH more into all the topics in later. These will include examples such as this, code snippits such as this, stand alone code niceties (ie: Highly reusable, useful classes/packages etc.).

The screen is the same one as yesterdays. It is just a green cube. But unlike yesterday's picking behavior, this one demonstrates pre-defined behavior. There are 2 aspects of behavior in Java3D; Interaction and animation. Yesterday was concerned with interaction, today is concerned with animation. Animation is basically just a pre-defined behavior. This usually consists of an Alpha and an Interpolator. The Alpha object controls the duration and such of the behavior. The Interpolator determines the type of behavior. Java3D has 5 types of Interpolators: RotationInterpolator, PositionInterpolator, ScaleInterpolator, ColorInterpolator, and TransparencyInterpolator. You can make your own Alpha and Interpolator type objects. I read through all the SUN Behavior API and tutorials and couldn't see a reason to do so. It seems, to me, that the classes Sun provides for Alpha and its Interpolators should be able to handle all the pre-defined Animations necessary for any 3D object or object group.

Two lines were taken out of this code:

objTrans.setCapability ( TransformGroup.ALLOW_TRANSFORM_READ );

objTrans.setCapability ( TransformGroup.ENABLE_PICK_REPORTING );

The ALLOW_TRANSFORM_WRITE still gives the objects in that transform group the ability to change at runtime. But, it does not allow pick reporting becuase that is interaction and we do not need to read information from the object such as mouse behavior. We are only telling the object what to do (Interpolator) and when (Alpha).

The key code in is:

// Set up Animation
// Set Alpha, or time increments
// -1 = loop forever, total loop duration in milliseconds

Alpha rotationTime = new Alpha(-1, 5000);

// Rotation Interpolator, you can change to the other 4 types here.

RotationInterpolator rotation = new RotationInterpolator(rotationTime, objTrans);

// Set the Bounding Sphere, determines when this Behavior is activated

BoundingSphere bounds = new BoundingSphere();



This is the Animation set-up block. As you can see, I create my Alpha object with parameters -1 and 5000. The -1 tells it to loop forever and the 5000 is 5000 miliseconds. The 5000 ms is how long each loop is supposed to last. I use a RotationInterpolator as my Interpolator. I give it the Alpha and the TransformGroup to perform on. The is pretty much the basic constructor for this Interpolator. It just rotates around the Y axis. You can control all aspects of rotation by digging into the API. I set the BoundingSphere also. Behaviors are VERY CPU intensive. The bounds help lower that. The behavior is only activated if the Java3D engine determines the view is within the SchedulingBounds. You then add your Interpolator to the TransformGroup. In our case this is the RotationInterpolator 'rotation'.

Michael C. Lee, Jr.