2

四元数を使用して固定点の周りを周回するカメラがあります。問題は、カメラが 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);
4

1 に答える 1

1

あなたは何か変なことをしている。単位クォータニオンは、方向全体を表します。通常、「up」ベクトルは使用しません。しかし...クォータニオンから取得する代わりに、定数の「アップ」ベクトル [0,1,0] を使用するだけで、必要な効果が得られるようです。

クォータニオンは必要ないようです。必要なのは、[0,1,0] の一定の「アップ」ベクトルです。次に、常に周回しているポイントである「at」ポイントが必要です。次に、y軸の周り、または「上」ベクトルと軌道点と「目」ポイント。

于 2012-05-01T03:28:00.690 に答える