PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

/src/away3d/controllers/LookAtController.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 136 lines | 95 code | 21 blank | 20 comment | 16 complexity | 9586a43b460595d9fb19185df685ca9c MD5 | raw file
  1package away3d.controllers
  2{
  3	import away3d.containers.*;
  4	import away3d.core.math.Matrix3DUtils;
  5	import away3d.entities.*;
  6	import away3d.events.*;
  7
  8	import flash.geom.Matrix3D;
  9
 10	import flash.geom.Vector3D;
 11	
 12	/**
 13	 * Extended camera used to automatically look at a specified target object.
 14	 *
 15	 * @see away3d.containers.View3D
 16	 */
 17	public class LookAtController extends ControllerBase
 18	{
 19		protected var _lookAtPosition:Vector3D;
 20		protected var _lookAtObject:ObjectContainer3D;
 21		protected var _origin:Vector3D = new Vector3D(0.0, 0.0, 0.0);
 22		protected var _upAxis:Vector3D = Vector3D.Y_AXIS;
 23		private var _pos:Vector3D = new Vector3D();
 24		/**
 25		 * Creates a new <code>LookAtController</code> object.
 26		 */
 27		public function LookAtController(targetObject:Entity = null, lookAtObject:ObjectContainer3D = null)
 28		{
 29			super(targetObject);
 30			
 31			if (lookAtObject)
 32				this.lookAtObject = lookAtObject;
 33			else
 34				this.lookAtPosition = new Vector3D();
 35		}
 36		
 37		/**
 38        * The vector representing the up direction of the target object.
 39        */
 40		public function get upAxis():Vector3D
 41		{
 42			return _upAxis;
 43		}
 44		
 45		public function set upAxis(upAxis:Vector3D):void
 46		{
 47			_upAxis = upAxis;
 48			
 49			notifyUpdate();
 50		}
 51
 52		/**
 53		 * The Vector3D object that the target looks at.
 54		 */
 55		public function get lookAtPosition():Vector3D
 56		{
 57			return _lookAtPosition;
 58		}
 59		
 60		public function set lookAtPosition(val:Vector3D):void
 61		{
 62			if (_lookAtObject) {
 63				_lookAtObject.removeEventListener(Object3DEvent.SCENETRANSFORM_CHANGED, onLookAtObjectChanged);
 64				_lookAtObject = null;
 65			}
 66			
 67			_lookAtPosition = val;
 68			
 69			notifyUpdate();
 70		}
 71		
 72		/**
 73		 * The 3d object that the target looks at.
 74		 */
 75		public function get lookAtObject():ObjectContainer3D
 76		{
 77			return _lookAtObject;
 78		}
 79		
 80		public function set lookAtObject(val:ObjectContainer3D):void
 81		{
 82			if (_lookAtPosition)
 83				_lookAtPosition = null;
 84			
 85			if (_lookAtObject == val)
 86				return;
 87			
 88			if (_lookAtObject)
 89				_lookAtObject.removeEventListener(Object3DEvent.SCENETRANSFORM_CHANGED, onLookAtObjectChanged);
 90			
 91			_lookAtObject = val;
 92			
 93			if (_lookAtObject)
 94				_lookAtObject.addEventListener(Object3DEvent.SCENETRANSFORM_CHANGED, onLookAtObjectChanged);
 95			
 96			notifyUpdate();
 97		}
 98		
 99		/**
100		 * @inheritDoc
101		 */
102		public override function update(interpolate:Boolean = true):void
103		{
104			interpolate = interpolate; // prevents unused warning
105			
106			if (_targetObject) {
107				if (_lookAtPosition) {
108					_targetObject.lookAt(_lookAtPosition, _upAxis);
109				} else if (_lookAtObject) {
110					if(_targetObject.parent && _lookAtObject.parent) {
111						if(_targetObject.parent != _lookAtObject.parent) {// different spaces
112							_pos.x = _lookAtObject.scenePosition.x;
113							_pos.y = _lookAtObject.scenePosition.y;
114							_pos.z = _lookAtObject.scenePosition.z;
115							Matrix3DUtils.transformVector(_targetObject.parent.inverseSceneTransform, _pos, _pos);
116						}else{//one parent
117							Matrix3DUtils.getTranslation(_lookAtObject.transform, _pos);
118						}
119					}else if(_lookAtObject.scene){
120						_pos.x = _lookAtObject.scenePosition.x;
121						_pos.y = _lookAtObject.scenePosition.y;
122						_pos.z = _lookAtObject.scenePosition.z;
123					}else{
124						Matrix3DUtils.getTranslation(_lookAtObject.transform, _pos);
125					}
126					_targetObject.lookAt(_pos, _upAxis);
127				}
128			}
129		}
130		
131		private function onLookAtObjectChanged(event:Object3DEvent):void
132		{
133			notifyUpdate();
134		}
135	}
136}