我正在尝试围绕其中心点在三维空间内旋转一个Sprite,并且我正在努力理解matrix3D的一些行为。
我已经重写了Sprite的set 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用于取消之前应用的旋转。
经过测试,rotationX的效果完全符合预期,Sprite围绕其水平轴旋转。
rotationY和rotationZ也似乎工作得很好。然而,存在一个问题:每当设置rotationY或rotationZ时,所有其他旋转值也会改变。这对我的应用程序是个问题(它试图保存和恢复值)。
我认为我只是缺乏一些关于matrix3D的理解。如何实现这样的效果,使各值之间没有相互依赖?