【问题标题】:Implementing a camera relative to a planets surface / direction of gravity相对于行星表面/重力方向实现相机
【发布时间】:2013-11-13 00:27:14
【问题描述】:

我目前正在尝试实现一个与设置向量相关的 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 函数返回欧拉范围内的值。 但我不确定,有什么解决办法吗?

【问题讨论】:

  • 这听起来像是(可能是杜撰的)关于配备 GPS 的战斗机在穿越赤道时的自动驾驶仪使它们倒转的故事。

标签: java opengl camera rotation position


【解决方案1】:

好的,在做其他事情的时候想通了!

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    • 2021-04-18
    • 2015-05-14
    • 2015-03-23
    • 1970-01-01
    • 2020-05-19
    相关资源
    最近更新 更多