Advertisement
durss

Fuckin' camera

Sep 9th, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package camera {
  2.  
  3.     import flash.display.Stage;
  4.     import flash.events.Event;
  5.     import flash.events.KeyboardEvent;
  6.     import flash.events.MouseEvent;
  7.     import flash.filters.DropShadowFilter;
  8.     import flash.geom.Matrix3D;
  9.     import flash.geom.Point;
  10.     import flash.geom.Vector3D;
  11.     import flash.text.TextField;
  12.     import flash.text.TextFieldAutoSize;
  13.     import flash.ui.Keyboard;
  14.    
  15.     /**
  16.      * @author L'aut con
  17.      * @date 7 sept. 2011;
  18.      */
  19.     public class SimpleFreeCamera {
  20.        
  21.         private var _stage:Stage;
  22.         private var _pressed:Boolean;
  23.         private var _goForward:Boolean;
  24.         private var _offsetDrag:Point;
  25.         private var _phi:Number;
  26.         private var _theta:Number;
  27.         private var _speed:Number;
  28.         private var _roll:Number;
  29.         private var _up:Vector3D;
  30.         private var _forward:Vector3D;
  31.         private var _position:Vector3D;
  32.         private var _lookAt:Vector3D;
  33.         private var _right:Vector3D;
  34.         private var _rotationMatrix:Matrix3D;
  35.         private var _tf:TextField;
  36.        
  37.        
  38.        
  39.  
  40.         /* *********** *
  41.          * CONSTRUCTOR *
  42.          * *********** */
  43.         /**
  44.          * Creates an instance of <code>SimpleFreeCamera</code>.
  45.          */
  46.         public function SimpleFreeCamera(stage:Stage) {
  47.             _stage = stage;
  48.             initialize();
  49.         }
  50.  
  51.        
  52.        
  53.         /* ***************** *
  54.          * GETTERS / SETTERS *
  55.          * ***************** */
  56.  
  57.  
  58.  
  59.         /* ****** *
  60.          * PUBLIC *
  61.          * ****** */
  62.        
  63.         /**
  64.          * Creates the projection matrix.
  65.          *
  66.          * @param viewWidth     viewport width
  67.          * @param viewHeight    viewport height
  68.          * @param orthogonal    get an orthogonal projection (true) or a perspective projection (false)
  69.          */
  70.         public function createProjectionMatrix(viewWidth:Number, viewHeight:Number, orthogonal:Boolean = false):Matrix3D {
  71.             var zNear:Number = 1000;
  72.             var zFar:int = 1;
  73.             if(!orthogonal) {
  74.                 var fov:Number = .2;//Math.PI */ 3;
  75.                 var w1:Number = (2 * zNear / (zNear * Math.atan(fov) - viewWidth)) / 1.33333;
  76.                 var h1:Number = (2 * zNear / (zNear * Math.atan(fov) + viewHeight));
  77.                 var q1:Number = -1 * (zFar + zNear) / (zFar - zNear);
  78.                 var q2:Number = -2 * (zFar * zNear) / (zFar - zNear);
  79.    
  80.                 return new Matrix3D(Vector.<Number>([w1, 0, 0, 0, 0, h1, 0, 0, 0, 0, q1, q2, 0, 0, -1, 0]));
  81.             }else{
  82.                 zNear = 2000;
  83.                 zFar = -2000;
  84.                 return new Matrix3D(Vector.<Number>
  85.                 ([
  86.                 2/viewWidth, 0  ,       0,        0,
  87.                 0  , 2/viewHeight,       0,        0,
  88.                 0  , 0  ,       1/(zFar-zNear),           0,
  89.                 0  , 0  ,       zNear/(zNear-zFar),        1
  90.                 ]));
  91.             }
  92.         }
  93.        
  94.         public function getProjectionMatrix(viewWidth:Number, viewHeight:Number, orthogonal:Boolean = false):Matrix3D {
  95.             var m:Matrix3D = new Matrix3D();
  96.             m.appendTranslation(_position.x, _position.y, _position.z);
  97.             m.append(_rotationMatrix);
  98. //          m.appendRotation(_phi / Math.PI * 180, Vector3D.X_AXIS);
  99. //          m.appendRotation(_theta / Math.PI * 180, Vector3D.Z_AXIS);
  100. //          m.append(createProjectionMatrix(viewWidth, viewHeight, orthogonal));
  101.             m.append(createProjectionMatrix(viewWidth, viewHeight, orthogonal));
  102.             return m;
  103.         }
  104.  
  105.  
  106.        
  107.        
  108.         /* ******* *
  109.          * PRIVATE *
  110.          * ******* */
  111.         /**
  112.          * Initialize the class.
  113.          */
  114.         private function initialize():void {
  115.             _roll = 0;
  116.             _position = new Vector3D(-50,-50,6);
  117.             _up = new Vector3D();
  118.             _forward = new Vector3D();
  119.             _lookAt = new Vector3D(0,0,0);
  120.            
  121.             _tf = _stage.addChild(new TextField()) as TextField;
  122.             _tf.autoSize = TextFieldAutoSize.LEFT;
  123.             _tf.textColor = 0xffffff;
  124.            
  125.             _speed = 2;
  126.             _phi = 2*Math.PI;
  127.             _theta = 0;
  128.             _stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseEventHandler);
  129.             _stage.addEventListener(MouseEvent.MOUSE_UP, mouseEventHandler);
  130.             _stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardEventHandler);
  131.             _stage.addEventListener(KeyboardEvent.KEY_UP, keyboardEventHandler);
  132.             _stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  133.         }
  134.        
  135.         /**
  136.          * Called on a mouse event.
  137.          */
  138.         private function mouseEventHandler(event:MouseEvent):void {
  139.             if(event.type == MouseEvent.MOUSE_DOWN) {
  140.                 _pressed = true;
  141.                 _offsetDrag = new Point(_stage.mouseX, _stage.mouseY);
  142.             }else{
  143.                 _pressed = false;
  144.             }
  145.         }
  146.        
  147.         /**
  148.          * Called on a keyboard event.
  149.          */
  150.         private function keyboardEventHandler(event:KeyboardEvent):void {
  151.             if(event.type == KeyboardEvent.KEY_DOWN) {
  152.                 if(event.keyCode == Keyboard.UP) _goForward = true;
  153.             }else{
  154.                 if(event.keyCode == Keyboard.UP) _goForward = false;
  155.             }
  156.            
  157.             switch(event.keyCode){
  158.                 case Keyboard.LEFT: _position.x ++; break;
  159.                 case Keyboard.RIGHT: _position.x --; break;
  160.                 case Keyboard.UP: _position.y ++; break;
  161.                 case Keyboard.DOWN: _position.y --; break;
  162.                 case Keyboard.Q: _lookAt.z ++; break;
  163.                 case Keyboard.D: _lookAt.z --; break;
  164.                 case Keyboard.Z: _lookAt.y ++; break;
  165.                 case Keyboard.S: _lookAt.y --; break;
  166.                 case Keyboard.W: _lookAt.x ++; break;
  167.                 case Keyboard.X: _lookAt.x --; break;
  168.                 case Keyboard.PAGE_UP: _roll += .05; break;
  169.                 case Keyboard.PAGE_DOWN: _roll -= .05; break;
  170.                 default:
  171.             }
  172.         }
  173.        
  174.         /**
  175.          * Called on ENTER_FRAME event
  176.          *
  177.          * Formulas source : http://mathworld.wolfram.com/SphericalCoordinates.html
  178.          */
  179.         private function enterFrameHandler(event:Event):void {
  180.             if(_pressed) {
  181.                 _phi += Math.min((_stage.mouseX - _offsetDrag.x), 600) / 600 * Math.PI * .1;
  182.                 _theta += Math.min((_stage.mouseY - _offsetDrag.y), 600) / 600 * Math.PI * .1;
  183.                 _roll += Math.min((_stage.mouseX - _offsetDrag.x), 600) / 600 * Math.PI * .1;
  184. //              _position.z += Math.min((_stage.mouseY - _offsetDrag.y), 200) / 200 * 2;
  185. //              _theta += Math.min((_stage.mouseX - _offsetDrag.x), 600) / 200 * Math.PI * .01;
  186. //              _theta = _theta % (Math.PI * 2);
  187. //              _phi += Math.min((_stage.mouseY - _offsetDrag.y), 600) / 200 * Math.PI * .01;
  188. //              _phi = _phi % Math.PI;
  189.             }
  190.             _lookAt.x = _position.x - Math.cos(_phi) * 200;
  191.             _lookAt.y = _position.y - Math.sin(_phi) * 200;
  192.             _lookAt.z = _position.z + Math.sin(Math.max(Math.min(_theta, Math.PI), -Math.PI)) * 200;
  193.             if(_goForward) {
  194. //              _x += Math.cos(_theta) * Math.sin(_phi) * _speed;
  195. //              _y += Math.sin(_theta) * Math.cos(_phi) * _speed;
  196. //              _z += Math.cos(_phi) * _speed;
  197.             }
  198. //          _lookAt.x = _position.x + Math.sin(_roll) * 10;
  199. //          _lookAt.y = _position.y - Math.cos(_roll) * 10;
  200.            
  201.             _up.x = Math.sin(_roll);
  202.             _up.z = -Math.cos(_roll);
  203.             _up.y = 0;
  204.            
  205.             _forward.x = _lookAt.x - _position.x;
  206.             _forward.y = _lookAt.y - _position.y;
  207.             _forward.z = _lookAt.z - _position.z;
  208.             _forward.normalize();
  209.            
  210.             _right = _up.crossProduct(_forward);
  211.             _right.normalize();
  212.            
  213.             _up = _right.crossProduct(_forward);
  214.             _up.normalize();
  215.            
  216.             _rotationMatrix = new Matrix3D();
  217. //          _rotationMatrix.pointAt(_position, _lookAt, _up);
  218.             _rotationMatrix = new Matrix3D(Vector.<Number>([_up.x, _up.y, _up.z, 0,
  219.                                                             _right.x, _right.y, _right.z, 0,
  220.                                                             _forward.x, _forward.y, _forward.z, 0,
  221.                                                             0, 0, 0, 1
  222.                                                             ]));
  223.         }
  224.        
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement