- 最后登录
- 2013-9-29
- 注册时间
- 2012-8-20
- 阅读权限
- 90
- 积分
- 6371
- 纳金币
- 6372
- 精华
- 0
|
3d图形在原有的平面x,y的基础上加入深度z轴,使用几何***法重新计算物体x,y坐标。
坐标系统
flash 3d中默认使用右手坐标系统,x坐标从左到右,y坐标从上到下,z从外到里指向屏幕.
例子,首先创建一个ball类。
package
{
import flash.display.ColorCorrection;
import flash.display.Sprite;
/**
* @author:Gaara
* 2012-3-5
*
**/
public class Ball extends Sprite
{
public function Ball(x:Number,y:Number,z:Number,color:int=0x00FF00,radius:Number=5)
{
super();
this._x = x;
this._y = y;
this._z = z;
graphics.beginFill(color);
graphics.drawCircle(0,0,radius);
graphics.endFill();
}
public var _x:Number;
public var _y:Number;
public var _z:Number;
}
}
创建主类
package
{
import flash.display.Sprite;
import flash.events.Event;
/**
* @author:Gaara
* 2012-3-5
*
**/
[SWF(width="800", height="600" ,frameRate="12", backgroundColor="#000000")]
public class TestCos extends Sprite
{
/** 绘制小球 **/
private var ballVec:Vector.<Ball> = new Vector.<Ball>;
/**观察者与屏幕距离 **/
private const D:Number = 200;
/**旋转角度 **/
private var angelT:Number = 0.001;
private var radian:Number = angelT * 180 /Math.PI;
private var vpX:Number = 100;
private var vpY:Number = -50;
public function TestCos()
{
this.x = stage.stageWidth / 2;
this.y = stage.stageHeight / 2;
var ball0:Ball = new Ball(100,100,0);
var ball1:Ball = new Ball(0,100,0);
var ball2:Ball = new Ball(0,0,0);
var ball3:Ball = new Ball(100,0,0);
var ball4:Ball = new Ball(100,100,100,0xFF0000);
var ball5:Ball = new Ball(0,100,100,0xFF0000);
var ball6:Ball = new Ball(0,0,100,0xFF0000);
var ball7:Ball = new Ball(100,0,100,0xFF0000);
ballVec.push(ball0);
ballVec.push(ball1);
ballVec.push(ball2);
ballVec.push(ball3);
ballVec.push(ball4);
ballVec.push(ball5);
ballVec.push(ball6);
ballVec.push(ball7);
for each (var ball:Ball in ballVec)
{
addChild(ball);
}
this.addEventListener(Event.ENTER_FRAME,onAdd);
}
protected function onAdd(event:Event):void
{
for each (var ball:Ball in ballVec)
{
var _x:Number = ball._x*Math.cos(radian) - ball._y*Math.sin(radian);
var _y:Number = ball._y*Math.cos(radian) + ball._x*Math.sin(radian);
ball._x = _x ;
ball._y = _y ;
var myRadio:Number = D/(D + ball._z);
var pX:Number = (_x - vpX) * myRadio;
var pY:Number = (_y - vpY) * myRadio;
ball.x = vpX + pX;
ball.y = vpY + pY;
trace("myRadio:" + myRadio+" px:" + pX +" py:" + pY);
ball.scaleX = ball.scaleY = myRadio;
ball.alpha = 100 * myRadio;
}
}
}
}
|
|