如何避免相机在四元数旋转时翻转?

时间:2012-04-27 16:06:06

标签: java opengl rotation jogl quaternions

我有一台使用四元数绕固定点旋转的相机。问题在于,当相机围绕x轴旋转并越过一个杆时,相机将翻转并产生镜像。这是有道理的,因为相机面向相反的方向,但相机的向上矢量没有改变。显然,需要从外观向量和右向量计算新的向上向量,但我似乎无法使其工作。注意:这使用http://commons.apache.org/math/api-2.2/org/apache/commons/math/geometry/Rotation.html来表示四元数。

无论如何,我正在使用以下基本程序来旋转相机。

旋转四元数初始化为标识[1,0,0,0]:

private Rotation total = Rotation.IDENTITY;

摄像机的向上矢量初始化为:

private Vector3D up = new Vector3D(0, 1, 0);

通过将相机的当前旋转存储为四元数然后将任何后续旋转应用于此四元数来完成旋转,然后使用组合四元数(总数)旋转相机的位置。以下代码描述了此过程:

/**
 * Rotates the camera by combining the current rotation (total) with any new axis/angle representation of a new rotation (newAxis, rotation).
 */
public void rotateCamera() {
    if (rotation != 0) {
        //Construct quaternion from the new rotation:
        Rotation local = new Rotation(Math.cos(rotation/2), Math.sin(rotation/2) * newAxis.getX(), Math.sin(rotation/2) * newAxis.getY(), Math.sin(rotation/2) * newAxis.getZ(), true);

        //Generate new camera rotation quaternion from current rotation quaternion and new rotation quaternion:
        total = total.applyTo(local);

        //Rotate the position of the camera using the camera rotation quaternion:
        cam = rotateVector(local, cam);

        //rotation is complete so set the next rotation to 0
        rotation = 0;
    }
}

/**
 * Rotate a vector around a quaternion rotation.
 * 
 * @param rotation The quaternion rotation.
 * @param vector A vector to be rotated.
 * @return The rotated vector.
 */
public Vector3D rotateVector(Rotation rotation, Vector3D vector) {
    //set world centre to origin, i.e. (width/2, height/2, 0) to (0, 0, 0)
    vector = new Vector3D(vector.getX() - width/2, vector.getY() - height/2, vector.getZ());

    //rotate vector
    vector = rotation.applyTo(vector);

    //set vector in world coordinates, i.e. (0, 0, 0) to (width/2, height/2, 0) 
    return new Vector3D(vector.getX() + width/2, vector.getY() + height/2, vector.getZ());
}

存储新旋转的轴/角度的newAxis和rotation字段由按键生成,如下所示:

@Override
public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_W) {
        camera.setAxis(new Vector3D(1, 0, 0));
        camera.setRotation(0.1);
    }
    if (e.getKeyCode() == KeyEvent.VK_A) {
        camera.setAxis(new Vector3D(0, 1, 0));
        camera.setRotation(0.1);
    }
    if (e.getKeyCode() == KeyEvent.VK_S) {
        camera.setAxis(new Vector3D(1, 0, 0));
        camera.setRotation(-0.1);
    }
    if (e.getKeyCode() == KeyEvent.VK_D) {
        camera.setAxis(new Vector3D(0, 1, 0));
        camera.setRotation(-0.1);
    }
}

在每个渲染循环中调用rotateCamera()方法,然后以下代码设置摄像头:

glu.gluLookAt(camera.getCam().getX(), camera.getCam().getY(), camera.getCam().getZ(), camera.getView().getX(), camera.getView().getY(), camera.getView().getZ(), 0, 1, 0);

1 个答案:

答案 0 :(得分:1)

你做的事情很奇怪。单位四元数表示整个方向。你通常不会使用'向上'向量。但是......看起来你只需要使用一个常量'向上'向量[0,1,0]而不是从四元数中获取它就能得到你想要的效果。

听起来你真的不需要四元数。你想要的是[0,1,0]的常量“向上”向量。那么你想要一个“at”点,它始终是你在轨道上运行的点。然后你想要一个可以围绕y轴旋转(相对于你的轨道点)或围绕由你的'向上'向量和你的轨道点与之间的向量之间的交叉积所定义的轴的“眼睛”点。 '眼'点。