Animation in your scene

We've now met the basic nodes for creating simple scenes - object, group and scene. Another set of nodes, action nodes, allow for animation and interaction inside a scene. Perhaps the most useful is the transition node.

The transition node

This node takes a parameter of another node, and changes it from one set of values to another through time.

Change the SimpleScene.xml file to read:

<scene>
	<object id="myplane" type="primitives://plane.aro" position="-1 0 0"/>
	
	<transition object="myplane" what="position" end="1 0 0" length="2000"/>
</scene>

The first thing to notice is that we've given the object an id. This allows us to refer to this object from other nodes, such as the transition. The transition node takes a number of parameters:

  • The object parameter gives the id of the object with the parameter we want to change.

  • The what parameter says which parameter to change. You can choose position, rotation, scale, or color here.

  • The end parameter specifies the value that the target parameter will change towards.

  • The length parameter determines how long, in milliseconds, the change will take.

See the transition node reference page for a complete list of the parameters that transition accepts.

Viewing SimpleScene.xml shows that the plane moves from x = -1 to x = 1 over 2 seconds.

Grouping action nodes

Action nodes that are directly inside the scene node, as in the example above, begin when the scene is first viewed. Consider this example:

<scene>
	<object id="myplane" type="primitives://plane.aro" position="-1 0 0"/>
	
	<transition object="myplane" what="position" end="1 0 0" length="2000"/>
	<transition object="myplane" what="scale" end="2 2 2" length="2000"/>
</scene>

The myplane object will move and simultaneously increase in size when the scene is viewed.

It is possible to make actions nodes occur one after another using the sequential group node.

<scene>
	<object id="myplane" type="primitives://plane.aro" position="-1 0 0"/>
	
	<sequential>
		<transition object="myplane" what="position" end="1 0 0" length="2000"/>
		<transition object="myplane" what="scale" end="2 2 2" length="2000"/>
	</sequential>
</scene>

Here, the plane moves from x = -1 to x = 1 and, once the move is finished, scales to size 2.

In the case that you want to have a several actions nodes occur at the same time, but only after some have already occurred, the concurrent group node can be used:

<scene>
	<object id="myplane" type="primitives://plane.aro" position="-1 0 0"/>
	
	<sequential>
		<transition object="myplane" what="position" end="1 0 0" length="2000"/>
		<concurrent>
			<transition object="myplane" what="scale" end="2 2 2" length="2000"/>
			<transition object="myplane" what="rotation" end="360 0 0" length="2000"/>
		</concurrent>
	</sequential>
</scene>

Now the plane moves, and then both scales and rotates a the same time.

It is possible to create moderately complex animated scenes using combinations of sequential and concurrent nodes with groups and objects.

The event node

Another node that groups action nodes is event. The nodes inside an event don't start when the scene is first viewed. Instead, they start when the event is triggered. Events can be triggered in a number of ways:

  • as a result of user interaction (see the next section of the tutorial, User interaction);

  • using the trigger action node (see below); or

  • from a script (see Scripting).

Change the contents of SimpleScene.xml to the following:

<scene>
	<object id="myplane" type="primitives://plane.aro" position="-1 0 0"/>
	
	<event id="myevent">
		<transition object="myplane" what="position" end="1 0 0" length="2000"/>
		<transition object="myplane" what="scale" end="2 2 2" length="2000"/>
	</event>
</scene>

When this scene is viewed nothing happens - the plane remains at x = -1. This is because the event isn't being triggered. Modify the file to include a trigger node, like this:

<scene>
	<object id="myplane" type="primitives://plane.aro" position="-1 0 0"/>
	
	<event id="myevent">
		<transition object="myplane" what="position" end="1 0 0" length="2000"/>
		<transition object="myplane" what="scale" end="2 2 2" length="2000"/>
	</event>
	
	<trigger event="myevent"/>
</scene>

Now the plane moves and scales when the scene is viewed. This is because the trigger action node occurs when the scene is viewed (its parent node is scene) and it triggers the event node. Notice that once triggered, the event acts just like a concurrent node - all of the action nodes inside it occur at the same time.