Use a transformation listener

From JReality Wiki
Revision as of 20:41, 6 March 2009 by Gunn (Talk | contribs)

(diff) ←Older revision | view current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

Source file: TransformationListenerExample

JavaDoc: TransformationListener


Run as Java webstart


This example shows how to attach a TransformationListener to a Transformation in the scene graph. The actionPerformed() method of this listener is then invoked whenever the transformation changes.


The example consists of two cubes. A rotate tool is placed in the scene graph component of the first cube. A transformation listener is also attached to the transformation of this component.

  • Whenever the mouse is dragged over the left cube (child1), the rotate tool there writes a new transformation matrix in child1 and the listener is notified. This listener guarantees that the right cube's (child2's) transformation is the inverse of the left one ( child1), modulo a translation.
  • Whenever the mouse is dragged over the right cube, the default rotate tool, located higher up in the scene graph, is activated and the whole scene rotates.


  final Matrix translate = new Matrix(MatrixBuilder.euclidean().translate(3, 0, 0).getArray());
...
child1.getTransformation().addTransformationListener(new TransformationListener() {
 
	public void transformationMatrixChanged(TransformationEvent ev) {
		Matrix childMatrix = new Matrix(child1.getTransformation().getMatrix());
		childMatrix.invert();
		childMatrix.multiplyOnLeft(translate);
		childMatrix.assignTo(child2);
	}
			
	});


Here's how the example should look after you've rotated the left cube a little:


TformListener.jpg
Previous: Use the MatrixBuilder class Developer Tutorial: Contents Next: Developer Tutorial