0

我目前正在尝试实现一个与设置向量相关的 FPS 相机功能。在这种情况下,它将是从行星中心到玩家位置的法线。这将使您能够在地球上行走。相机不会因行星的曲率而扭曲。

目前,我使用它来设置相机方向。

        Main.getMap().getLocalizedUpVector(shootPos, up);
        Main.getMap().getLocalizedAngle(shootPos, angle);

        matrice.rotate(angle.y, RenderElement.AXIS_YAW);
        matrice.rotate(angle.x, RenderElement.AXIS_PITCH);

        matrice.rotate(yaw,up);
        right.set(matrice.m00, matrice.m10, matrice.m20);   

        if(right.length()>0){
            right.normalise();
        }

        matrice.rotate(pitch, right);

        eye.set(matrice.m02, matrice.m12, matrice.m22);

它调用这些函数。

public void getLocalizedUpVector(Vector3f pos, Vector3f res){

    res.set(pos.x - center.x, pos.y - center.y, pos.z - center.z);

    if(res.length() > 0){
        res.normalise();
    }
}


public void getLocalizedAngle(Vector3f pos, Vector3f angle){

    float deltaX = pos.x - center.x;
    float deltaY = pos.y - center.y;
    float deltaZ = pos.z - center.z;

    float distance = (float)Math.sqrt(deltaX*deltaX + deltaY*deltaY + deltaZ*deltaZ);

    float yaw = -(float)Math.atan2(deltaX, deltaZ);     
    float pitch = (float)Math.asin(deltaY/distance);

    angle.set(pitch, yaw, 0);
}

这种实现在地球的一半上运行良好,但在另一边却搞砸了。我怀疑这是因为我的 getLocalizedAngle 函数返回欧拉范围内的值。但我不确定,有什么解决办法吗?

4

1 回答 1

0

好的,在处理其他事情时想通了!

        matrice.setIdentity();

        Main.getMap().getLocalizedUpVector(shootPos, up);
        matrice.m02 = up.x; matrice.m12 = up.y; matrice.m22 = up.z;

        matrice.rotate(pitch, RenderElement.AXIS_PITCH);
        matrice.rotate(yaw, up);

        eye.set(matrice.m02, matrice.m12, matrice.m22);


        if(eye.length() > 0){
            eye.normalise();
        }

首先将矩阵设置为向上向量,然后通过全局俯仰轴旋转该向量以获得俯仰。围绕向上矢量旋转以获得偏航。

然后从矩阵中检索相对眼睛。

于 2013-11-18T17:13:01.780 回答