⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 displayobject3d.as.svn-base

📁 一个2D基于verlet的Flash物理引擎。它用AS3编写而成。Fisix的目标是应用到游戏等计算量很大的实时应用中。尽管flash比c/c++要慢,很棒的物理引擎
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
		*
	 	* @param	parent	The DisplayObject3D object that contains this display object.
		* @param	renderSessionData Data for the current render.
		*/
		public function project( parent :DisplayObject3D, renderSessionData:RenderSessionData):Number
		{
			if( this._transformDirty ) updateTransform();
	
			this.world.calculateMultiply( parent.world, this.transform );
			
			if( renderSessionData.camera is IObjectCuller )
			{
				if( this === renderSessionData.camera )
					this.culled = true;
				else
					this.culled = (IObjectCuller(renderSessionData.camera).testObject(this) < 0);
				if( this.culled ) return 0;
				if( parent !== renderSessionData.camera )
					this.view.calculateMultiply4x4( parent.view, this.transform );
			}
			else if( parent !== renderSessionData.camera )
				this.view.calculateMultiply( parent.view, this.transform );
					
			calculateScreenCoords( renderSessionData.camera );
	
			var screenZs :Number = 0;
			var children :Number = 0;

			for each( var child:DisplayObject3D in this._childrenByName )
			{
				if( child.visible )
				{
					screenZs += child.project( this, renderSessionData );
					children++;
				}
			}
	
			return this.screenZ = screenZs / children;
		}
	
		private function calculateScreenCoords( camera :CameraObject3D ):void
		{
			var persp:Number = (camera.focus * camera.zoom) / (camera.focus + view.n34);
			screen.x = view.n14 * persp;
			screen.y = view.n24 * persp;
			screen.z = view.n34;
		}
		// ___________________________________________________________________________________________________
		//                                                                     L O C A L   T R A N S F O R M S
		// LL      OOOO   CCCC    AA   LL
		// LL     OO  OO CC  CC  AAAA  LL
		// LL     OO  OO CC     AA  AA LL
		// LL     OO  OO CC  CC AAAAAA LL
		// LLLLLL  OOOO   CCCC  AA  AA LLLLLL
	
		/**
		* Translate the display object in the direction it is facing, i.e. it's positive Z axis.
		*
		* @param	distance	The distance that the object should move forward.
		*/
		public function moveForward  ( distance:Number ):void { translate( distance, FORWARD  ); }
	
		/**
		* Translate the display object in the opposite direction it is facing, i.e. it's negative Z axis.
		*
		* @param	distance	The distance that the object should move backward.
		*/
		public function moveBackward ( distance:Number ):void { translate( distance, BACKWARD ); }
	
		/**
		* Translate the display object lateraly, to the left of the direction it is facing, i.e. it's negative X axis.
		*
		* @param	distance	The distance that the object should move left.
		*/
		public function moveLeft     ( distance:Number ):void { translate( distance, LEFT     ); }
	
		/**
		* Translate the display object lateraly, to the right of the direction it is facing, i.e. it's positive X axis.
		*
		* @param	distance	The distance that the object should move right.
		*/
		public function moveRight    ( distance:Number ):void { translate( distance, RIGHT    ); }
	
		/**
		* Translate the display object upwards, with respect to the direction it is facing, i.e. it's positive Y axis.
		*
		* @param	distance	The distance that the object should move up.
		*/
		public function moveUp       ( distance:Number ):void { translate( distance, UP       ); }
	
		/**
		* Translate the display object downwards, with respect to the direction it is facing, i.e. it's negative Y axis.
		*
		* @param	distance	The distance that the object should move down.
		*/
		public function moveDown     ( distance:Number ):void { translate( distance, DOWN     ); }
	
		// ___________________________________________________________________________________________________
		//                                                                   L O C A L   T R A N S L A T I O N
	
		/**
		* Move the object along a given direction.
		*
		* @param	distance	The distance that the object should travel.
		* @param	axis		The direction that the object should move towards.
		*/
		public function translate( distance:Number, axis:Number3D ):void
		{
			var vector:Number3D = axis.clone();
	
			if( this._transformDirty ) updateTransform();
	
			Matrix3D.rotateAxis( transform, vector )
	
			this.x += distance * vector.x;
			this.y += distance * vector.y;
			this.z += distance * vector.z;
		}
	
		// ___________________________________________________________________________________________________
		//                                                                         L O C A L   R O T A T I O N
	
		/**
		* Rotate the display object around its lateral or transverse axis —an axis running from the pilot's left to right in piloted aircraft, and parallel to the wings of a winged aircraft; thus the nose pitches up and the tail down, or vice-versa.
		*
		* @param	angle	The angle to rotate.
		*/
		public function pitch( angle:Number ):void
		{
			angle = Papervision3D.useDEGREES? angle * toRADIANS : angle;
	
			var vector:Number3D = RIGHT.clone();
	
			if( this._transformDirty ) updateTransform();
	
			Matrix3D.rotateAxis( transform, vector );
			var m:Matrix3D = Matrix3D.rotationMatrix( vector.x, vector.y, vector.z, angle );
	
	//		this.transform.copy3x3( Matrix3D.multiply3x3( m ,transform ) );
			this.transform.calculateMultiply3x3( m ,transform );
	
			this._rotationDirty = true;
		}
	
	
		/**
		* Rotate the display object around about the vertical axis —an axis drawn from top to bottom.
		*
		* @param	angle	The angle to rotate.
		*/
		public function yaw( angle:Number ):void
		{
			angle = Papervision3D.useDEGREES? angle * toRADIANS : angle;
	
			var vector:Number3D = UP.clone();
	
			if( this._transformDirty ) updateTransform();
	
			Matrix3D.rotateAxis( transform, vector );
			var m:Matrix3D = Matrix3D.rotationMatrix( vector.x, vector.y, vector.z, angle );
	
			this.transform.calculateMultiply3x3( m ,transform );
	
			this._rotationDirty = true;
		}
	
	
		/**
		* Rotate the display object around the longitudinal axis —an axis drawn through the body of the vehicle from tail to nose in the normal direction of flight, or the direction the object is facing.
		*
		* @param	angle
		*/
		public function roll( angle:Number ):void
		{
			angle = Papervision3D.useDEGREES? angle * toRADIANS : angle;
	
			var vector:Number3D = FORWARD.clone();
	
			if( this._transformDirty ) updateTransform();
	
			Matrix3D.rotateAxis( transform, vector );
			var m:Matrix3D = Matrix3D.rotationMatrix( vector.x, vector.y, vector.z, angle );
	
			this.transform.calculateMultiply3x3( m ,transform );
	
			this._rotationDirty = true;
		}
	
	
		/**
		* Make the object look at a specific position.
		*
		* @param	targetObject	Object to look at.
		* @param	upAxis			The vertical axis of the universe. Normally the positive Y axis.
		*/
		public function lookAt( targetObject:DisplayObject3D, upAxis:Number3D=null ):void
		{
			var position :Number3D = new Number3D( this.x, this.y, this.z );
			var target   :Number3D = new Number3D( targetObject.x, targetObject.y, targetObject.z );
	
			var zAxis    :Number3D = Number3D.sub( target, position );
			zAxis.normalize();
	
			if( zAxis.modulo > 0.1 )
			{
				var xAxis :Number3D = Number3D.cross( zAxis, upAxis || UP );
				xAxis.normalize();
	
				var yAxis :Number3D = Number3D.cross( zAxis, xAxis );
				yAxis.normalize();
	
				var look  :Matrix3D = this.transform;
				// scale fix for lookAt()
				look.n11 =  xAxis.x * _scaleX;
				look.n21 =  xAxis.y * _scaleX;
				look.n31 =  xAxis.z * _scaleX;
				
				look.n12 = -yAxis.x * _scaleY;
				look.n22 = -yAxis.y * _scaleY;
				look.n32 = -yAxis.z * _scaleY;
				
				look.n13 =  zAxis.x * _scaleZ;
				look.n23 =  zAxis.y * _scaleZ;
				look.n33 =  zAxis.z * _scaleZ;
	
				this._transformDirty = false;
				this._rotationDirty = true;
				
			}
			else
			{
				var log:XrayLog = new XrayLog();
				log.debug( "lookAt Error" );
			}
		}
	
		// ___________________________________________________________________________________________________
		//                                                                                   T R A N S F O R M
		// TTTTTT RRRRR    AA   NN  NN  SSSSS FFFFFF OOOO  RRRRR  MM   MM
		//   TT   RR  RR  AAAA  NNN NN SS     FF    OO  OO RR  RR MMM MMM
		//   TT   RRRRR  AA  AA NNNNNN  SSSS  FFFF  OO  OO RRRRR  MMMMMMM
		//   TT   RR  RR AAAAAA NN NNN     SS FF    OO  OO RR  RR MM M MM
		//   TT   RR  RR AA  AA NN  NN SSSSS  FF     OOOO  RR  RR MM   MM
	
		/**
		* Copies the position information (x, y and z coordinates) from another object or Matrix3D.
		*
		* @param	reference	A DisplayObject3D or Matrix3D object to copy the position from.
		*/
		public function copyPosition( reference:* ):void
		{
			var trans  :Matrix3D = this.transform;
			var matrix :Matrix3D = (reference is DisplayObject3D)? reference.transform : reference;
	
			trans.n14 = matrix.n14;
			trans.n24 = matrix.n24;
			trans.n34 = matrix.n34;
		}
	
		/**
		* Copies the transformation information (position, rotation and scale) from another object or Matrix3D.
		*
		* @param	reference	A DisplayObject3D or Matrix3D object to copy the position from.
		*/
		public function copyTransform( reference:* ):void
		{
			var trans  :Matrix3D = this.transform;
			var matrix :Matrix3D = (reference is DisplayObject3D)? reference.transform : reference;
	
			trans.n11 = matrix.n11;		trans.n12 = matrix.n12;
			trans.n13 = matrix.n13;		trans.n14 = matrix.n14;
	
			trans.n21 = matrix.n21;		trans.n22 = matrix.n22;
			trans.n23 = matrix.n23;		trans.n24 = matrix.n24;
	
			trans.n31 = matrix.n31;		trans.n32 = matrix.n32;
			trans.n33 = matrix.n33;		trans.n34 = matrix.n34;
	
			this._transformDirty = false;
			this._rotationDirty  = true;
		}
	
	
		/**
		* [internal-use] Updates the transform Matrix3D with the current rotation and scale values.
		*/
		// TODO OPTIMIZE (HIGH)
		protected function updateTransform():void
		{
			var q:Object = Matrix3D.euler2quaternion( -this._rotationY, -this._rotationZ, this._rotationX ); // Swapped

			var m:Matrix3D = Matrix3D.quaternion2matrix( q.x, q.y, q.z, q.w );
			
			//var q:Quaternion = Quaternion.createFromEuler( -this._rotationY, -this._rotationZ, this._rotationX );
			//var m:Matrix3D = q.toMatrix();
			
			var transform:Matrix3D = this.transform;
	
			m.n14 = transform.n14;
			m.n24 = transform.n24;
			m.n34 = transform.n34;
	
			transform.copy( m );
	
			// Scale
			var scaleM:Matrix3D = Matrix3D.IDENTITY;
			scaleM.n11 = this._scaleX;
			scaleM.n22 = this._scaleY;
			scaleM.n33 = this._scaleZ;
	
			this.transform.calculateMultiply( transform, scaleM );
	
			this._transformDirty = false;
		}
	
	
		// ___________________________________________________________________________________________________
	
		/**
		* Returns a string value representing the three-dimensional position values of the display object instance.
		*
		* @return	A string.
		*/
		public override function toString(): String
		{
			return this.name + ': x:' + Math.round(this.x) + ' y:' + Math.round(this.y) + ' z:' + Math.round(this.z);
		}
	
		// ___________________________________________________________________________________________________
		//                                                                                       P R I V A T E
	
		/**
		* [internal-use]
		*/
		protected var _transformDirty :Boolean = false;
	
		private var _rotationX      :Number;
		private var _rotationY      :Number;
		private var _rotationZ      :Number;
		private var _rotationDirty  :Boolean = false;
	
		private var _scaleX         :Number;
		private var _scaleY         :Number;
		private var _scaleZ         :Number;
		private var _scaleDirty     :Boolean = false;
	
		protected var _sorted       :Array;
		
		private var _material:MaterialObject3D;
		
		static private var _totalDisplayObjects :int = 0;
	
		static private var toDEGREES :Number = 180/Math.PI;
		static private var toRADIANS :Number = Math.PI/180;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -