我想旋轉一個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發生了什麼的一些理解。我該如何實現這個,所以值之間沒有相互依賴關係?
最好的辦法是檢出[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
沒有瀏覽您的代碼,但您是否遇到Gimbal Lock? http://en.wikipedia.org/wiki/Gimbal_lock – 2010-07-31 07:23:33