Write an animation tool

From JReality Wiki
Jump to: navigation, search


Source file: AnimationExample

Run as Java webstart

The following example shows how to use a Tool for animating parts of the jReality scene graph. This is done by a tool which is always active and triggered by the InputSlot SystemTime.

The tool implemented in this example moves the matheon bear (or an icosahedron, if the bear geometry cannot be found) on an elliptic curve in the x-z-plane.

The complete code is a little bit more complex, since it includes

  • File loading (buddy bear)
  • computation of the point on the ellipse and the corresponding tangent vector
  • the VR viewer setup, in the main method.

To operate the example:

  • Use the WASD-navigation to walk arround, and press the right mouse button for mouse look

Things to notice:

  • The tool defines no activation slot and is thus always active.
  • The only current slot of the tool is InputSlot.SYSTEM_TIME, but we do not query the corresponding value of the slot (which always gives the time since its last update in ms). Instead, we use the method ToolContext.getTime() which gives the current system time in ms. Whe might also use the SystemTime slot (int dt = tc.getAxisState(InputSlot.SYSTEM_TIME)) and summing up: t += 0.001*dt.

Since the tool is always active, we only implement its perform methiod: The code of the tool is

Tool animator = new AbstractTool() {
	long startTime=-1;
		addCurrentSlot(InputSlot.SYSTEM_TIME, "animation trigger");
	public void perform(ToolContext tc) {
		// remember the startup time:
		if (startTime==-1) startTime = tc.getTime();
		// compute time in seconds since startup;
		double t = 0.001*(tc.getTime()-startTime);
		... // compute a transformation matrix from t
		... // and assign it to the component where
		... // the tool is attached

The corresponding component (where the tool is attached), is obtained by

SceneGraphComponent myCmp = tc.getRootToToolComponent().getLastComponent();
Previous: Use the mouse-over input slot Developer Tutorial: Contents Next: Developer Tutorial