2009-12-14 12 views
8

Estoy tratando de rotar un Sprite en tres dimensiones alrededor de su punto central, y estoy luchando por comprender algo del comportamiento de matrix3D.cómo hacer rotación 3D alrededor del centro en AS3 usando matrix3D?

he anulado los métodos establecidos rotationX, rotationY y rotationZ del Sprite de la siguiente manera:

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); 

} 

estoy usando prependTranslation para corregir el punto central de la rotación, y la primera prependRotation para anular cualquier anteriormente- rotación aplicada

Probándolo, rotationX funciona exactamente como se esperaba, y el Sprite gira alrededor de su eje horizontal.

rotationY y rotationZ también parecen funcionar bien. Sin embargo, hay un problema: cuando se establecen rotationY o rotationZ, también cambian todos los otros valores de rotación. Esto no es un problema con rotationX - si configuro rotationX, nada más cambia. Pero si configuro rotationY o rotationZ cambian todos los valores de rotación, lo cual es un problema para mi aplicación (que está tratando de guardar y restaurar valores).

Creo que me falta cierto conocimiento sobre lo que está pasando con matrix3D. ¿Cómo puedo implementar esto para que no haya interdependencia entre los valores?

+0

Lo mejor es consultar [UIComponent # transformAround] (http://opensource.adobe.com/svn/opensource/flex/sdk/trunk/frameworks/projects/framework/src/mx/core/UIComponent.as) y la implementación relacionada de AdvancedLayout.as. Hacen exactamente eso, envolviendo Matrix3D. –

+0

No revisando su código, pero ¿está experimentando Gimbal Lock? http://en.wikipedia.org/wiki/Gimbal_lock –

Respuesta

3

Otra solución fácil es agregar el objeto y centrarlo en un sprite contenedor y hacer las transformaciones 3D en el sprite que lo contiene.

+0

yeh esto es mucho más fácil. cree un titular Sprite, luego agregue el Sprite con x: -mysprite.width * 0.5 yy: -mysprite.height * 0.5 y luego gire el soporte – daidai

1

No sé nada sobre AS3, etc. Pero solo mirando su código, me pregunto por qué lo traduce en el eje z usando lo que entiendo son los valores xey (ancho y alto). ¿No debería traducirse el eje z usando algo como "profundidad"?

0

Esto es muy simple, puede intentar utilizar el siguiente código:

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

mientras que s es su sprite, el tercer parámetro, Vector3D indican la posición central de su sprite.

El código anterior hará que los sprites s giren más -1 grado.

Cuestiones relacionadas