< Zurück | Inhalt | Weiter >

From InterpolatorTest.java

//Quat4f createQuaternionFromAxisAndAngle

//( Vector3d axis, double angle )


double sin_a = Math.sin( angle / 2 ); double cos_a = Math.cos( angle / 2 );

//use a vector so we can call normalize Vector4f q = new Vector4f();

q.x = (float) (axis.x * sin_a);

q.y = (float) (axis.y * sin_a);

q.z = (float) (axis.z * sin_a);

q.w = (float) cos_a;

//It is best to normalize the quaternion

//so that only rotation information is used q.normalize();

//convert to a Quat4f and return return new Quat4f( q );


//Quat4f createQuaternionFromEuler( double angleX,

//double angleY, double angleZ )


//simply call createQuaternionFromAxisAndAngle for each axis

//and multiply the results

Quat4f qx = createQuaternionFromAxisAndAngle( new Vector3d(1,0,0), angleX );

Quat4f qy = createQuaternionFromAxisAndAngle( new Vector3d(0,1,0), angleY );

Quat4f qz = createQuaternionFromAxisAndAngle( new Vector3d(0,0,1), angleZ );

//qx = qx * qy qx.mul( qy );

//qx = qx * qz qx.mul( qz );

return qx;



12.3.1 Design of the InterpolatorTest example

The InterpolatorTest example creates a Switch Node and attaches a SwitchInterpolator. A custom Alpha class (RandomAlpha) is used to generate Alpha values for the SwitchInterpolator. The RandomAlpha generates a random Alpha value (between 0 and 1) every 10 seconds. This causes the SwitchInterpolator to randomly switch between child Nodes every 10 seconds.

The Switch Node has six child Nodes:

1. Group containing a ColorInterpolator: Operates on an Appearance.

2. Group containing a PositionInterpolator: Operates on a TransformGroup.

3. Group containing a RotationInterpolator: Operates on a TransformGroup.

4. Group containing a ScaleInterpolator: Operates on a TransformGroup.

5. Group containing a TransparencyInterpolator: Operates on an Appearance.

6. Group containing a RotPosScaleInterpolator: Operates on a TransformGroup.

Each Group also contains a Link Node to a common SharedGroup. The SharedGroup contains a TransformGroup with a child Shape3D. The parent TransformGroup is passed to each Interpolator that requires a TransformGroup, while the Shape3D’s Appearance is passed to each Interpolator that requires an Appearance. A Text2D is also created with the name of the Interpolator as text so that the current active Interpolator can be seen. See figure 12.4.


Figure 12.4 The basic scenegraph design for the InterpolatorTest example

The InterpolatorTest example exploits a useful feature of Behaviors: when the Switch changes its active child Node, the Behaviors of the inactive child Nodes are no longer scheduled. In this way, the SwitchInterpolator that randomly selects child Nodes (using the RandomAlpha class) also activates a single Behavior—inactive Behaviors are no longer processed even though they are still enabled.

The RandomAlpha class is very simple:

//This class defines an Alpha class that returns

//a random value every N milliseconds.

public class RandomAlpha extends Alpha


protected long m_LastQueryTime = 0; protected float m_Alpha = 0;

protected final int m_kUpdateInterval = 10000;

public RandomAlpha()



//core method override returns the Alpha value for a given time public float value( long time )


if( System.currentTimeMillis()

− m_LastQueryTime > m_kUpdateInterval )


m_LastQueryTime = System.currentTimeMillis(); m_Alpha = (float) Math.random();


return m_Alpha;