我有很多安排在三维空间的精灵,他们的父容器已旋转应用。 我要如何扭转精灵3D旋转,它们总是面对摄像机(ActionScript 3的)?
i have lots of sprites arranged in 3D space, and their parent container has rotations applied. How do i reverse the sprites 3D rotation, that they always face the camera (Actionscript 3)?
继承人code来测试它:
heres a code to test it:
package{
import flash.display.Sprite;
import flash.events.Event;
public class test extends Sprite{
var canvas:Sprite = new Sprite();
var sprites:Array = []
public function test(){
addChild(canvas)
for (var i:int=0;i<20;i++){
var sp:Sprite = new Sprite();
canvas.addChild(sp);
sp.graphics.beginFill(0xFF0000);
sp.graphics.drawCircle(0,0,4);
sp.x = Math.random()*400-200;
sp.y = Math.random()*400-200;
sp.z = Math.random()*400-200;
sprites.push(sp);
}
addEventListener(Event.ENTER_FRAME,function():void{
canvas.rotationX++;
canvas.rotationY = canvas.rotationY+Math.random()*2;
canvas.rotationZ++;
for (var i:int=0;i<20;i++){
//this is not working...
sprites[i].rotationX = -canvas.rotationX
sprites[i].rotationY = -canvas.rotationY
sprites[i].rotationZ = -canvas.rotationZ
}
})
}
}
}
我猜我已经做了一些魔法与精灵的rotation3D矩阵... 我试图执行这个脚本:的http:// ughzoid .word press.com / 2011/02/03 / Papervision3D的-sprite3d / ,但有这么成功 感谢您的帮助。
I am guessing i have to do some magic with the rotation3D matrices of the sprites... I've tried to implement this script: http://ughzoid.wordpress.com/2011/02/03/papervision3d-sprite3d/ , but had so success Thanks for help.
要做到这一点最简单的方法是清算的变换矩阵的旋转部分。典型的同质转型看起来像这样
The easiest way to do this is "clearing" the rotational part of the transform matrix. Your typical homogenous transformation looks like this
| xx xy xz xw |
| yx yy yz yw |
| zx zy zz zw |
| wx wy wz ww |
与WX = WY = WZ = 0,WW = 1。如果你把你会看到,其实这个矩阵是由一个3×3子矩阵的定义旋转仔细一看,有3子向量的翻译和均匀行0 0 0 1
with wx = wy = wz = 0, ww = 1. If you take a closer look you'll see that in fact this matrix is composed of a 3x3 submatrix defining the rotation, a 3 subvector for the translation and a homogenous row 0 0 0 1
| R T |
| (0,0,0) 1 |
对于要保留翻译,但摆脱了旋转的广告牌/雪碧,即R =一,在某些情况下是scaleing应用的身份需要进行调整为好。
For a billboard/sprite you want to keep the translation, but get rid of the rotation, i.e. R = I. In case some scaleing was applied the identity needs to be scaled as well.
这给出了以下recipie:
This gives the following recipie:
D =开方(xx²+yx²+zx²)
d = sqrt( xx² + yx² + zx² )
| d 0 0 T.x |
| 0 d 0 T.y |
| 0 0 d T.z |
| 0 0 0 1 |
加载这个矩阵可以绘制相机对准精灵。
Loading this matrix allows you to draw camera aligned sprites.