矩阵基础知识(五)
[SWF(backgroundColor = 0x0,width = 550,height = 400)]
var sp:Sprite =new Sprite();
addChild(sp);
sp.x = stage.stageWidth / 2;
sp.y = stage.stageHeight / 2;
var ball:Sprite=new Sprite();
sp.addChild(ball);
ball.z = 650;
var speed:Number=10;
var a = 10; trace(a=a*a,a/2,a);
function createRect(com:Sprite,c:uint=0xff0000,w:Number=50,h:Number=40):Sprite
{
var mc:Sprite=new Sprite();
mc.z = 0;
com.addChild(mc);
mc.graphics.beginFill(c);
mc.graphics.drawRect(-w/2,-h/2,w,h);
return mc;
}
var ballR:Number = 200;
var pics:Array = [];
var numPics:Array = [1,6,10,12,10,6,1];
setBall();
function setBall():void
{
var rStep:Number=Math.PI/(numPics.length-1);
for (var i:int=0; i<7; i++)
{
var tStep:Number = 2 * Math.PI / numPics[i];
for (var j:int =0; j<numPics[i]; j++)
{
var image:Sprite = createRect(ball,0xFFFFFF * Math.random());
image.width = 80;
image.height = 60;
image.x=ballR*Math.sin(i*rStep)*Math.cos(j*tStep+Math.PI/2);
image.y=ballR*Math.cos(i*rStep);
image.z=ballR*Math.sin(i*rStep)*Math.sin(j*tStep+Math.PI/2);
image.transform.matrix3D.pointAt(new Vector3D(1,0,0),Vector3D.Z_AXIS,Vector3D.Y_AXIS);
pics.push(image);
}
}
setDepth();
}
addEventListener(Event.ENTER_FRAME,rotate);
function setDepth():void
{
var depthArray:Array = [];
var tempMatrix:Matrix3D = ball.transform.matrix3D.clone();
for (var n:int=0; n<pics.length; n++)
{
//var tempVector3D:Vector3D = tempMatrix.deltaTransformVector(new Vector3D(pics[n].x,pics[n].y,pics[n].z));
var tempVector3D:Vector3D = tempMatrix.transformVector(new Vector3D(pics[n].x,pics[n].y,pics[n].z));
depthArray.push({d:tempVector3D.z,n:n});
}
depthArray.sortOn("d",Array.NUMERIC|Array.DESCENDING);
for (var k:int=0; k<depthArray.length; k++)
{
ball.setChildIndex(pics[depthArray[k].n],k);
}
}
function rotate(e:Event):void
{
//ball.z = 0;
//ball.transform.matrix3D.appendRotation(-sp.mouseX/100,Vector3D.Y_AXIS);
//ball.transform.matrix3D.appendRotation(sp.mouseY/100,Vector3D.X_AXIS);
ball.transform.matrix3D.prependRotation(-sp.mouseX/100,Vector3D.Y_AXIS);
ball.transform.matrix3D.prependRotation(sp.mouseY/100,Vector3D.X_AXIS);
if(ball.transform.matrix3D.position.x>100||ball.transform.matrix3D.position.x<-100)speed*=-1;
ball.transform.matrix3D.appendTranslation(speed,0,0)
//ball.z = 650;
setDepth();
}
var sp:Sprite =new Sprite();
addChild(sp);
sp.x = stage.stageWidth / 2;
sp.y = stage.stageHeight / 2;
var ball:Sprite=new Sprite();
sp.addChild(ball);
ball.z = 650;
var speed:Number=10;
var a = 10; trace(a=a*a,a/2,a);
function createRect(com:Sprite,c:uint=0xff0000,w:Number=50,h:Number=40):Sprite
{
var mc:Sprite=new Sprite();
mc.z = 0;
com.addChild(mc);
mc.graphics.beginFill(c);
mc.graphics.drawRect(-w/2,-h/2,w,h);
return mc;
}
var ballR:Number = 200;
var pics:Array = [];
var numPics:Array = [1,6,10,12,10,6,1];
setBall();
function setBall():void
{
var rStep:Number=Math.PI/(numPics.length-1);
for (var i:int=0; i<7; i++)
{
var tStep:Number = 2 * Math.PI / numPics[i];
for (var j:int =0; j<numPics[i]; j++)
{
var image:Sprite = createRect(ball,0xFFFFFF * Math.random());
image.width = 80;
image.height = 60;
image.x=ballR*Math.sin(i*rStep)*Math.cos(j*tStep+Math.PI/2);
image.y=ballR*Math.cos(i*rStep);
image.z=ballR*Math.sin(i*rStep)*Math.sin(j*tStep+Math.PI/2);
image.transform.matrix3D.pointAt(new Vector3D(1,0,0),Vector3D.Z_AXIS,Vector3D.Y_AXIS);
pics.push(image);
}
}
setDepth();
}
addEventListener(Event.ENTER_FRAME,rotate);
function setDepth():void
{
var depthArray:Array = [];
var tempMatrix:Matrix3D = ball.transform.matrix3D.clone();
for (var n:int=0; n<pics.length; n++)
{
//var tempVector3D:Vector3D = tempMatrix.deltaTransformVector(new Vector3D(pics[n].x,pics[n].y,pics[n].z));
var tempVector3D:Vector3D = tempMatrix.transformVector(new Vector3D(pics[n].x,pics[n].y,pics[n].z));
depthArray.push({d:tempVector3D.z,n:n});
}
depthArray.sortOn("d",Array.NUMERIC|Array.DESCENDING);
for (var k:int=0; k<depthArray.length; k++)
{
ball.setChildIndex(pics[depthArray[k].n],k);
}
}
function rotate(e:Event):void
{
//ball.z = 0;
//ball.transform.matrix3D.appendRotation(-sp.mouseX/100,Vector3D.Y_AXIS);
//ball.transform.matrix3D.appendRotation(sp.mouseY/100,Vector3D.X_AXIS);
ball.transform.matrix3D.prependRotation(-sp.mouseX/100,Vector3D.Y_AXIS);
ball.transform.matrix3D.prependRotation(sp.mouseY/100,Vector3D.X_AXIS);
if(ball.transform.matrix3D.position.x>100||ball.transform.matrix3D.position.x<-100)speed*=-1;
ball.transform.matrix3D.appendTranslation(speed,0,0)
//ball.z = 650;
setDepth();
}