本文介绍了在 3D 中反转旋转,使对象始终面向相机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多精灵排列在 3D 空间中,并且它们的父容器应用了旋转.我如何反转精灵 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)?

这是一个测试它的代码:

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

我猜我必须对精灵的旋转 3D 矩阵做一些魔术...我试图实现这个脚本:http://ughzoid.wordpress.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.如果你仔细观察,你会发现这个矩阵实际上是由一个定义旋转的 3x3 子矩阵、一个用于平移的 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 = I.如果应用了一些缩放,则身份也需要缩放.

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.

这给出了以下配方:

d = sqrt( 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.

这篇关于在 3D 中反转旋转,使对象始终面向相机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 22:38