3D rotation 01
/**
* Copyright katsumi ( http://wonderfl.net/user/katsumi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2GYT
*/
// forked from katsumi's forked from: flash on 2009-6-19
// forked from katsumi's flash on 2009-6-19
package {
import flash.display.*;
import flash.events.*;
import flash.text.*;
public class FlashTest extends Sprite {
public var mc:Array;
public var mcNum:int;
public var p:Point3D;
public var p2:Point3D;
public var op:Point3D;
public var screenZ:Number;
public var screen:Sprite;
private var SW:Number;
private var SH:Number;
private var theta:Number=0;
private var psi:Number=0;
public function FlashTest() {
// write as3 code here..
mcNum=10;
mc=new Array();
SW = this.stage.stageWidth;
SH = this.stage.stageHeight;
screen=new Sprite();
screen.x=SW/2;
screen.y=SH/2;
this.addChild(screen);
for(var i:int=0;i<mcNum;i++){
mc.push(new Sprite());
mc[i].graphics.clear();
mc[i].graphics.lineStyle(0,0,0);
mc[i].graphics.beginFill(0xff00ff);
mc[i].graphics.drawRect(-50,-50,100,100);
mc[i].graphics.endFill();
var textfield:TextField = new TextField();
textfield.multiline = false;
textfield.defaultTextFormat = new TextFormat("_sans", 100 ,0xffffff);
textfield.autoSize = TextFieldAutoSize.LEFT;
textfield.text = (i+1).toString();
textfield.y = -textfield.height/2;
textfield.x = -textfield.width/2;
mc[i].addChild( textfield );
screen.addChild(mc[i]);
}
screenZ=100;
p=new Point3D(0,0,100);
p2=new Point3D(0,0,100);
op=new Point3D(0,0,300);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(e:Event):void {
var zsort_array:Array = new Array();
psi=Math.PI/4*this.screen.mouseX/(SW/2);
var dtheta:Number=this.screen.mouseY/SH/10;
for(var i:int=0;i<mc.length;i++){
p.y=op.y+250*Math.cos(theta-2*Math.PI/mc.length*i);
p.z=op.z+250*Math.sin(theta-2*Math.PI/mc.length*i);
p2.x = Math.cos(psi)*(p.x-op.x)-Math.sin(psi)
*(p.z-op.z)+op.x;
p2.y = p.y;
p2.z = Math.sin(psi)*(p.x-op.x)+Math.cos(psi)
*(p.z-op.z)+op.z;
mc[i].scaleX = mc[i].scaleY = screenZ/p2.z;
//this.screen.setChildIndex(mc[i],);
mc[i].y=p2.y*screenZ/p2.z;
mc[i].x=p2.x*screenZ/p2.z;
zsort_array.push( {face:mc[i] , depth:p2.z} );
}
zsort_array.sortOn
( "depth", Array.NUMERIC | Array.DESCENDING);
for (var idx:int=0;idx<mc.length;idx++) {
this.screen.setChildIndex( zsort_array[idx].face , idx );
}
theta+=dtheta;
}
}
}
internal class Point3D
{
public var x:Number = 0;
public var y:Number = 0;
public var z:Number = 0;
public function Point3D(x:Number = 0, y:Number = 0, z:Number = 0) {
this.x = x;
this.y = y;
this.z = z;
}
}