2009-12-14 29 views
8

我想旋轉一個Sprite圍繞它的中心點的三個維度,並且我正在努力理解matrix3D的一些行爲。如何使用matrix3D在AS3中圍繞中心進行3D旋轉?

伊夫覆蓋了雪碧的設定的rotationX,的rotationY和的rotationZ方法如下:

override public function set rotationX (_rotationX:Number) : void { 

    this.transform.matrix3D.prependTranslation(this.width/2.0, this.height/2.0, 0); 
    this.transform.matrix3D.prependRotation(-this.rotationX, Vector3D.X_AXIS); 
    this.transform.matrix3D.prependRotation(_rotationX, Vector3D.X_AXIS); 
    this.transform.matrix3D.prependTranslation(-(this.width/2.0), -(this.height/2.0), 0); 

} 

override public function set rotationY (_rotationY:Number) : void { 

    this.transform.matrix3D.prependTranslation(this.width/2.0, this.height/2.0, 0); 
    this.transform.matrix3D.prependRotation(-this.rotationY, Vector3D.Y_AXIS); 
    this.transform.matrix3D.prependRotation(_rotationY, Vector3D.Y_AXIS); 
    this.transform.matrix3D.prependTranslation(-(this.width/2.0), -(this.height/2.0), 0); 

} 

override public function set rotationZ (_rotationZ:Number) : void { 

    this.transform.matrix3D.prependTranslation(this.width/2.0, this.height/2.0, 0); 
    this.transform.matrix3D.prependRotation(-this.rotationZ, Vector3D.Z_AXIS); 
    this.transform.matrix3D.prependRotation(_rotationZ, Vector3D.Z_AXIS); 
    this.transform.matrix3D.prependTranslation(-(this.width/2.0), -(this.height/2.0), 0); 

} 

我使用使用prependTranslation糾正旋轉的中心點,並第一prependRotation取消任何previously-應用旋轉。

測試一下,rotationX的工作方式和預期的完全一樣,Sprite圍繞水平軸旋轉。

rotationY和rotationZ也顯示工作正常。但是,有一個問題:只要設置了rotationY或rotationZ,其他所有旋轉值也都會發生變化。這對rotationX沒有問題 - 如果我設置了rotationX,則沒有其他更改。但是,如果我設置了rotationY或rotationZ,所有旋轉值都會改變,這對於我的應用程序(正在嘗試保存和恢復值)是一個問題。

我想我只是缺乏對matrix3D發生了什麼的一些理解。我該如何實現這個,所以值之間沒有相互依賴關係?

+0

最好的辦法是檢出[UIComponent#transformAround](http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/projects/framework/src/mx/core/UIComponent.as )和相關的AdvancedLayout.as實現。他們正是這樣做,包裝Matrix3D。 – 2010-06-05 10:00:53

+0

沒有瀏覽您的代碼,但您是否遇到Gimbal Lock? http://en.wikipedia.org/wiki/Gimbal_lock – 2010-07-31 07:23:33

回答

3

另一個簡單的解決方案是添加對象並將其居中在一個容器精靈中,並對包含的精靈進行3D變換。

+0

這很容易。 創建一個支架Sprite,然後添加帶有x:-mysprite.width * 0.5和y:-mysprite.height * 0.5的Sprite,然後旋轉支架 – daidai 2010-09-03 03:24:41

1

我對AS3等一無所知。但是看看你的代碼,我想知道你爲什麼使用我所知道的x和y值(寬度和高度)在z軸上進行轉換。不應該使用「深度」之類的東西翻譯Z軸嗎?

0

這是很簡單的,你可以嘗試使用下面的代碼:

var matrix3d:Matrix3D = s.transform.matrix3D; 
matrix3d.appendRotation(-1, Vector3D.Z_AXIS , new Vector3D(390, 360, 0)); 

而s是你的精靈,第三個參數,Vector3D表明你的精靈的中心位置。

上面的代碼將使精靈旋轉更多-1度。