我有很多排列在 3D 空间中的精灵,并且它们的父容器应用了旋转。
我如何反转精灵 3D 旋转,使其始终面向相机(Actionscript 3)?
这是一个测试它的代码:
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
}
})
}
}
}
我猜我必须对精灵的旋转 3D 矩阵施展一些魔法......
我尝试实现这个脚本:http://ughzoid.wordpress.com/2011/02/03/papervision3d-sprite3d/ http://ughzoid.wordpress.com/2011/02/03/papervision3d-sprite3d/,但取得了如此成功
感谢帮助。
最简单的方法是“清除”变换矩阵的旋转部分。典型的同质转换看起来像这样
| xx xy xz xw |
| yx yy yz yw |
| zx zy zz zw |
| wx wy wz ww |
其中 wx = wy = wz = 0,ww = 1。如果您仔细观察,您会发现实际上该矩阵由定义旋转的 3x3 子矩阵、用于平移的 3 子向量和同质行 0 0 组成0 1
| R T |
| (0,0,0) 1 |
对于广告牌/精灵,您希望保留平移,但摆脱旋转,即 R = I。如果应用了某些缩放,则身份也需要缩放。
这给出了以下配方:
d = sqrt( xx² + yx² + zx² )
| d 0 0 T.x |
| 0 d 0 T.y |
| 0 0 d T.z |
| 0 0 0 1 |
加载此矩阵允许您绘制相机对齐的精灵。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)