In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

3D Object rotation by Quaternion

3Dオブジェクトをマウスの向きに合わせて回転させる
* 軸回転ではなく、クォータニオンを使って回転を制御
* [参考したサイト]
* http://yamasv.blog92.fc2.com/blog-entry-129.html
Get Adobe Flash player
by fukusin 09 Oct 2009
/**
 * Copyright fukusin ( http://wonderfl.net/user/fukusin )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/lYsE
 */

/*
 * 3Dオブジェクトをマウスの向きに合わせて回転させる
 * 軸回転ではなく、クォータニオンを使って回転を制御
 * [参考したサイト]
 * http://yamasv.blog92.fc2.com/blog-entry-129.html
 */
package {
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    //import fl.controls.*;
    //
    //import org.papervision3d.core.*;
    import org.papervision3d.core.geom.*;
    import org.papervision3d.core.math.*;
    import org.papervision3d.scenes.* ;
    import org.papervision3d.objects.*;
    import org.papervision3d.objects.primitives.*;
    import org.papervision3d.cameras.*;
    import org.papervision3d.materials.*;
    import org.papervision3d.materials.utils.*;
    import org.papervision3d.view.*;
    import org.papervision3d.render.*;
    import org.papervision3d.materials.special.* ;
	//
	import caurina.transitions.Tweener;
	import net.hires.debug.Stats;
	//
	public class Main extends BasicView {
		//
		//
		//==========================
		//var
		//==========================
		private var mouseDown:Boolean = false;
		private var mousePos:Point;
		private var pose:org.papervision3d.core.math.Matrix3D;
		private var cube:Cube;
		private var reset_button:Sprite;
		private var rot:Object;
		private var vector:Object;
		//
		//
		//==========================
		//コンストラクタ
		//==========================
		public function Main():void
		{
		    rot = new Object();
		    rot.x = 0;
		    rot.y = 0;
		    vector = new Object();
		    vector.x = 0;
		    vector.y = 0;
			//リセットボタン
			reset_button = new Sprite();
			reset_button.width = 50; 
			//reset_button.label = "reset"; 
			reset_button.x = 10;
			reset_button.y = 10;
			addChild(reset_button);
			//reset_button.addEventListener(MouseEvent.CLICK, onButtonClick); 
			//
			//addChild( new Stats() );
			//
			init3D();
		}
		//
		//
		//==========================
		//init3D
		//==========================
		private function init3D():void
		{
			camera.x = 0;
			camera.y = 0;
			camera.z = -2000;
			camera.focus = 1100;
			camera.zoom = 1;
			//
			var cm:ColorMaterial = new ColorMaterial(0x0099ff);
			var wm:WireframeMaterial = new WireframeMaterial(0x33ffff);
			var compo_material:CompositeMaterial = new CompositeMaterial();
			compo_material.addMaterial(cm);
			compo_material.addMaterial(wm);
			cube = new Cube(new MaterialsList( { all:compo_material } ), 200, 100, 300);
			scene.addChild(cube);
			//
			stage.addEventListener(MouseEvent.MOUSE_DOWN, mousedown);
					
			startRendering();
		}
          //
         //
         //==========================
         //mousedown
         //==========================            
         private function mousedown(e:MouseEvent):void 
         {
             mouseDown = true;
             mousePos = new Point(stage.mouseX, stage.mouseY);
             pose = org.papervision3d.core.math.Matrix3D.clone(cube.transform);
             //
             removeEventListener(Event.ENTER_FRAME, throwing);
             //
             stage.addEventListener(MouseEvent.MOUSE_MOVE, mousemove);
             stage.addEventListener(MouseEvent.MOUSE_UP, mouseup);
         }
         //
         //
         //==========================
         //mousemove
         //==========================
         private function mousemove(e:MouseEvent):void{
             if (mouseDown) {
                 var rot_buffer:Object = Object({x:rot.x, y: rot.y});
                 //
                 rot.x = stage.mouseX -mousePos.x;
                 rot.y = stage.mouseY - mousePos.y;
                 //
                 vector.x = rot.x - rot_buffer.x;
                 vector.y = rot.y - rot_buffer.y;
                 //
                 rotate();
             }
         }
         //
         //
         //==========================
         //mouseup
         //==========================
         private function mouseup(e:MouseEvent):void 
         {
             mouseDown = false;
             addEventListener(Event.ENTER_FRAME, throwing);
             //
             stage.removeEventListener(MouseEvent.MOUSE_MOVE, mousemove);
             stage.removeEventListener(MouseEvent.MOUSE_UP, mouseup);
         }
         //
         //
         //==========================
         //throwing
         //==========================
         private function throwing(e:Event):void 
         {
             vector.x *= 0.9;
             vector.y *= 0.9;
             //
             if (Math.abs(vector.x) < 0.01 && Math.abs(vector.y) < 0.01) {
                 vector.x = 0;
                 vector.y = 0;
                 removeEventListener(Event.ENTER_FRAME, throwing);
             }else {
                 rot.x += vector.x;
                 rot.y += vector.y;
             }
             //
             rotate();
         }
         //
         //
         //==========================
         //rotate
         //==========================
         private function rotate():void 
         {
             // X方向(Y軸周り)に回転する回転行列
             var my:org.papervision3d.core.math.Matrix3D = org.papervision3d.core.math.Matrix3D.rotationMatrix(0, 1, 0, -0.02 * rot.x);
             // Y方向(X軸周り)に回転する回転行列
             var mx:org.papervision3d.core.math.Matrix3D = org.papervision3d.core.math.Matrix3D.rotationMatrix(1, 0, 0, -0.02 * rot.y);
             // 回転の合成
             var m:org.papervision3d.core.math.Matrix3D = org.papervision3d.core.math.Matrix3D.multiply(mx, my);
             cube.transform = org.papervision3d.core.math.Matrix3D.multiply(m, pose);
         }
     }
	//
	//
}