PageRenderTime 364ms CodeModel.GetById 231ms app.highlight 8ms RepoModel.GetById 123ms app.codeStats 0ms

/src/away3d/cameras/SpringCam.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 132 lines | 69 code | 28 blank | 35 comment | 4 complexity | 64ef20d47808d9d3bf836eba0169522a MD5 | raw file
  1package away3d.cameras
  2{
  3	import away3d.cameras.lenses.LensBase;
  4	import away3d.core.base.Object3D;
  5
  6	import flash.geom.Matrix3D;
  7	import flash.geom.Vector3D;
  8
  9	/**
 10   * v1 - 2009-01-21 b at turbulent dot ca - http://agit8.turbulent.ca
 11   * v2 - 2011-03-06 Ringo - http://www.ringo.nl/en/
 12   **/
 13   
 14  /**
 15   * A 1st and 3d person camera(depending on positionOffset!), hooked on a physical spring on an optional target.
 16   */
 17  public class SpringCam extends Camera3D
 18  {
 19    /**
 20     * [optional] Target object3d that camera should follow. If target is null, camera behaves just like a normal Camera3D.
 21     */
 22    public var target:Object3D;
 23    
 24    //spring stiffness
 25    /**
 26     * Stiffness of the spring, how hard is it to extend. The higher it is, the more "fixed" the cam will be.
 27     * A number between 1 and 20 is recommended.
 28     */
 29    public var stiffness:Number = 1;
 30    
 31    /**
 32     * Damping is the spring internal friction, or how much it resists the "boinggggg" effect. Too high and you'll lose it!
 33     * A number between 1 and 20 is recommended.
 34     */
 35    public var damping:Number = 4;
 36    
 37    /**
 38     * Mass of the camera, if over 120 and it'll be very heavy to move.
 39     */
 40    public var mass:Number = 40;
 41    
 42    /**
 43     * Offset of spring center from target in target object space, ie: Where the camera should ideally be in the target object space.
 44     */
 45    public var positionOffset:Vector3D = new Vector3D(0,5,-50);
 46    
 47    /**
 48     * offset of facing in target object space, ie: where in the target object space should the camera look.
 49     */
 50    public var lookOffset:Vector3D = new Vector3D(0,2,10);
 51    
 52    //zrot to apply to the cam
 53    private var _zrot:Number = 0;
 54    
 55    //private physics members
 56    private var _velocity:Vector3D = new Vector3D();
 57    private var _dv:Vector3D = new Vector3D();
 58    private var _stretch:Vector3D = new Vector3D();
 59    private var _force:Vector3D = new Vector3D();
 60    private var _acceleration:Vector3D = new Vector3D();
 61    
 62    //private target members
 63    private var _desiredPosition:Vector3D = new Vector3D();
 64    private var _lookAtPosition:Vector3D = new Vector3D();
 65    
 66    //private transformed members
 67    private var _xPositionOffset:Vector3D = new Vector3D();
 68    private var _xLookOffset:Vector3D = new Vector3D();
 69    private var _xPosition:Vector3D = new Vector3D();
 70 
 71 	private var _viewProjection : Matrix3D = new Matrix3D();
 72    
 73    public function SpringCam(lens : LensBase = null)
 74    {
 75    	super(lens);
 76    }
 77    
 78    /**
 79     * Rotation in degrees along the camera Z vector to apply to the camera after it turns towards the target .
 80     */
 81    public function set zrot(n:Number):void
 82    {
 83      _zrot = n;
 84      if(_zrot < 0.001) n = 0;
 85    }
 86    public function get zrot():Number
 87    {
 88      return _zrot;
 89    }
 90    
 91    
 92    public override function get viewProjection():Matrix3D
 93    {
 94      if(target != null)
 95      {
 96      	
 97      	_xPositionOffset = target.transform.deltaTransformVector(positionOffset);
 98      	_xLookOffset = target.transform.deltaTransformVector(lookOffset);
 99      	
100      	_desiredPosition = target.position.add(_xPositionOffset);
101      	_lookAtPosition = target.position.add(_xLookOffset);
102      	
103      	_stretch = this.position.subtract(_desiredPosition);
104      	_stretch.scaleBy(-stiffness);
105      	_dv = _velocity.clone();
106      	_dv.scaleBy(damping);
107      	_force = _stretch.subtract(_dv);
108      	
109      	_acceleration = _force.clone();
110      	_acceleration.scaleBy(1/mass);
111      	_velocity = _velocity.add(_acceleration);
112      	
113      	_xPosition = position.add(_velocity);
114      	x = _xPosition.x;
115      	y = _xPosition.y;
116      	z = _xPosition.z;
117      	
118      	lookAt(_lookAtPosition);
119      	
120      	if(Math.abs(_zrot) > 0)
121			rotate(Vector3D.Z_AXIS, _zrot);
122			
123		_viewProjection.copyFrom(inverseSceneTransform);
124		_viewProjection.append(this.lens.matrix);
125				
126      }
127      
128      return this._viewProjection;
129    }
130    
131  }
132}