package geometry._theory { import away3d.materials.ColorMaterial; import away3d.primitives.Cylinder; import flash.events.Event; import flash.geom.Matrix3D; import flash.geom.Vector3D; import flash.utils.getTimer; import triga.shapes.Tick; import triga.shapes.XYZ; /** * @author Nicolas Barradeau * http://en.nicoptere.net */ public class H_LocalRotation extends BaseScene { private var target:Tick; private var tick:Tick; private var angle:Number = 0; public function H_LocalRotation() { setup(); target = new Tick( new Vector3D( 50, 35, 0 ), 10, BLUE ); tick = new Tick( null, 350 ); var discXZ:Cylinder = new Cylinder( new ColorMaterial( BLUE.color, .25 ), 100, 100, 1, 32, 1 ); discXZ.y = 35; addMesh( discXZ ); var discXY:Cylinder = new Cylinder( new ColorMaterial( WHITE.color, .25 ), 50, 50, 1, 32, 1 ); addMesh( discXY ); discXY.rotationX = 90; addMesh( new XYZ(100), tick, target ); addEventListener( Event.ENTER_FRAME, oef ); } private function oef(e:Event):void { var t:Number = ( getTimer() * .01 ) / 180 * Math.PI; angle += 2; //the target object will rotate on the XZ plane (blue disc) var p:Vector3D = new Vector3D( Math.cos( t ) * 100, 35, Math.sin( t ) * 100 ); target.position = p; //the tick will perform a circle on the XY plane (white disc) var tickPos:Vector3D = new Vector3D( Math.cos( t * 5 ) * 50, Math.sin( t * 5 ) * 50, 0 ); //create a new Matrix3D to calculate the rotation var m:Matrix3D = new Matrix3D(); //the matrix will "lookat" a given object m.pointAt( p.subtract( tickPos ), Vector3D.Y_AXIS ); //then the Local ( rotated ) axes can be retrieved like this var rawData:Vector. = m.rawData; var X_axis:Vector3D = new Vector3D( rawData[0], rawData[1], rawData[2] ); var Y_axis:Vector3D = new Vector3D( rawData[4], rawData[5], rawData[6] ); var Z_axis:Vector3D = new Vector3D( rawData[8], rawData[9], rawData[10] ); //or m.copyColumnTo( 0, X_axis ); m.copyColumnTo( 1, Y_axis ); m.copyColumnTo( 2, Z_axis ); //choose the axis on which to perform the local rotation //m.appendRotation( angle, X_axis); m.appendRotation( angle, Y_axis); //m.appendRotation( angle, Z_axis); //then translate the matrix m.appendTranslation( tickPos.x, tickPos.y, tickPos.z ); //and finally assign the transform matrix to the object tick.transform = m; } } }