2009-12-21 112 views
2

我有一張2D圖像,我想以真3D繪製,並圍繞其中心旋轉。Flash中的透視圖

我使用的是Actionscript 3代碼(實際上是Haxe,沒有IDE),我正努力通過實驗發現這些值。

我有一個DisplayObject。如果我使用rotateZ = 45,則對象圍繞它的左上角旋轉,而不是居中;更復雜Display.transform.matrixrotate(Math.PI/4)這樣的工作方式相同。如何繞着DisplayObject的XY中心在Z軸上旋轉?

然後我該如何獲得視角才能工作?相對於父對象的視角還是旋轉後的對象?

我在DisplayObject實例上使用什麼樣的旋轉和位置?透視變換的價值是什麼,以及我應用了哪些對象?

+0

聽起來就像是圍繞位於對象左上角的註冊點旋轉。 – Aaron 2009-12-21 16:34:13

回答

5

一個非常常見的方法是有一個支點的DisplayObjectContainer,增加您的實際平面成容器,並從中心偏移了。然後你實際上旋轉容器。

var pivot : DisplayObjectContainer; 

// plane is a display object (e.g. Sprite) that has been created previuosly 
// and contains your 2D image. 
plane.x = -plane.width/2; 
plane.y = -plane.height/2; 
pivot.addChild(plane); 

pivot.rotationY = 45; 

這裏,平面放在名爲支點容器內,並抵消了一半它的寬度和高度。這意味着平面顯示對象的中心將與容器的註冊點(原點)對齊。現在圍繞原點旋轉容器(樞軸點)將圍繞同一點旋轉它的所有孩子,包括飛機。

這通常是容易的工作比矩陣變換。特別是3D矩陣很容易難以理解,除非你很熟悉數學。

此外,只需修改plane.transform.matrix3D矩陣對象不會影響顯示對象,直到您重置矩陣。這是非常乏味,如果您使用的是吐溫引擎例如,您可能需要有一個UPDATE事件處理程序,每次重置矩陣,就像這樣:

plane.transform.matrix3D = myModifiedMatrix3D; 

使用樞軸方法,您可以只需旋轉rotationX/Y/Z屬性即可。

+0

這是最直接的方法。確認,爲我工作,並大大簡化了我的代碼。 – Will 2009-12-22 15:07:08

0

你可以用rotationX, rotationY, rotationZ對象屬性改變視角,但它只能在flashplayer> 9中工作,如果你想擁有更多的控制權,我建議你使用papervision3d庫(例如camera fov,zoom ect。)。

+0

是的,但值會是什麼?我是否必須按特定順序設置它們,例如在添加位圖之前將透視變換設置爲場景,或者? – Will 2009-12-21 10:58:42

3

我對你的問題有些困惑,但它仍然很有趣。

你有什麼東西等距工作,想渲染透視投影視圖或其他方式?你有一個透視圖,想要去等距?

在Flash CS4 IDE中,您可以使用「3D」幾個參數進行播放。爲了說明這一點,我將一堆MovieClip組裝成一個立方體。

這裏是立方體,旋轉在Y上45度,然後以45度的X,你可以在變換面板中看到:

flash perspective

這裏是相同的立方體,與透視角度在右側的屬性檢查器中的3D位置和視圖組中更改。

flash isometric

IDE中的屬性可通過動作進行控制。每個DisplayObject都有一個transform屬性,該屬性保存控制2D和3D屬性的對象的引用,如:Matrix,Matrix3D,PerspectiveProjection等。

您可以通過PerspectiveProjection的fieldOfView屬性來控制透視變形。

假設框剪輯被命名爲,我可以將它的fieldOfView設置爲非常小的值(因爲允許的值大於0且小於180),那就是它。

例如

var isometric:PerspectiveProjection = new PerspectiveProjection(); 
isometric.fieldOfView = 0.00001; 
box.transform.perspectiveProjection = isometric; 

對於軌道運行,請查看此article on devnet。它解釋了一種繞軌道的方法。根據您要達到的目標,它可能爲Ralph Hauwert's Arcball article

這裏有幾個as3等距圖書館像FFilmationas3isolib,但 我不確定你需要什麼。作爲antpaw是說,如果你在更大的東西的工作,你可能會使用靈活的3D API,如PapervisionAway3D

disturb我們取得了有趣的等距接口,用於可視化被稱爲Twigital的推文。我們爲此使用了papervision。

UPDATE

看來你需要繞樞軸動態旋轉。你可以使用變換矩陣來做到這一點。這裏是你如何做到這一點在2D

/** 
    * Rotates a matrix about a point defined inside the matrix's transformation space. 
    * This can be used to rotate a movie clip around a transformation point inside itself. 
    * 
    * @param m A Matrix instance. 
    * 
    * @param x The x coordinate of the point. 
    * 
    * @param y The y coordinate of the point. 
    * 
    * @param angleDegrees The angle of rotation in degrees. 
    * @playerversion Flash 9.0.28.0 
    * @langversion 3.0 
    * @keyword Matrix, Copy Motion as ActionScript  
    * @see flash.geom.Matrix   
    */ 
    public static function rotateAroundInternalPoint(m:Matrix, x:Number, y:Number, angleDegrees:Number):void 
    { 
     var point:Point = new Point(x, y); 
     point = m.transformPoint(point); 
     m.tx -= point.x; 
     m.ty -= point.y; 
     m.rotate(angleDegrees*(Math.PI/180)); 
     m.tx += point.x; 
     m.ty += point.y; 
    } 



    /** 
    * Rotates a matrix about a point defined outside the matrix's transformation space. 
    * This can be used to rotate a movie clip around a transformation point in its parent. 
    * 
    * @param m A Matrix instance. 
    * 
    * @param x The x coordinate of the point. 
    * 
    * @param y The y coordinate of the point. 
    * 
    * @param angleDegrees The angle of rotation in degrees. 
    * @playerversion Flash 9.0.28.0 
    * @langversion 3.0 
    * @keyword Matrix, Copy Motion as ActionScript  
    * @see flash.geom.Matrix  
    */ 
    public static function rotateAroundExternalPoint(m:Matrix, x:Number, y:Number, angleDegrees:Number):void 
    { 
     m.tx -= x; 
     m.ty -= y; 
     m.rotate(angleDegrees*(Math.PI/180)); 
     m.tx += x; 
     m.ty += y; 
    } 

代碼的,雖然不是我的,這是Adobe公司(羅伯特·彭納的我猜),該MatrixTransformer類的一部分。現在

,對於3D,那就更簡單了,因爲Matrix3D類具有旋轉的方法,如prependRotationappendRotation,接受3個參數:

  • 度:數
  • 軸:的Vector3D
  • pivotPoint:Vector3D

所以你可以很容易地旋轉一個方框30度的X軸約0,0,0 w第i個像:

var m:Matrix3D = box.transform.matrix3D; 
m.prependRotation(30,Vector3D.X_AXIS,new Vector3D(0,0,0)); 

再次,檢查了國際信息發展網的文章中,Matrix3D classVector3D類。

如果你想深入瞭解有關矢量,矩陣和變換的知識,你可以查看3D Math Primer,整個事情都很好解釋,它只是數學,所以你學到的東西在任何3D設置都很方便(純as3,away3d,papervision,openGL等)。

HTH, 喬治

+0

謝謝George;我試圖稍微改寫我的問題以避開等距。 – Will 2009-12-21 12:53:55

+0

我也修改了我的答案,就像你在UPDATE中看到的一樣。祝你好運! – 2009-12-21 14:14:35