1

一人称カメラを作ってみました。XNA プロジェクトのコードの一部を取り、それを Java (LWJGL) に変換しようとしました。

Matrix4f mx = Matrix4f.rotate(pitch, new Vector3f(1, 0, 0),
            new Matrix4f(), null);

   Matrix4f my = Matrix4f.rotate(yaw, new Vector3f(0, 1, 0),
            new Matrix4f(), null);

    Matrix4f rotationMatrix = Matrix4f.mul(mx, my, null);

    Vector4f tmp = Matrix4f.transform(rotationMatrix, new Vector4f(0, 0,
            -1, 0), null);

    target = new Vector3f(position.x + tmp.x, position.y + tmp.y,
            position.z + tmp.z);

    GLU.gluLookAt(this.position.x, this.position.y, this.position.z,
            this.target.x, this.target.y, this.target.z, 0, 1.0f, 0);

マウスを動かすと、ピッチとヨーが増減します。ゲームの実行開始時は正常に動作しますが、いくつかの動きの後、下や上を見ることができません。私の位置が < 0 の場合に発生する必要がありますか?

完璧に動作する XNA コード:

         Matrix rotationMatrix = Matrix.CreateRotationX(pitch) * Matrix.CreateRotationY(yaw);

        camera.Target = Vector3.Transform(Vector3.Forward, rotationMatrix) + camera.Position;
        this.viewMatrix = Matrix.CreateLookAt(this.position, this.target, Vector3.Up);

編集

そのため、行列を回転させるために LWJGL と XNA の間に何か違いがあることがわかりました。

ジャワ:

Matrix4f mx = Matrix4f.rotate(1.2345f, new Vector3f(1,0,0),new Matrix4f(),null);

結果:

{1.0 0.0 0.0 0.0} {0.0 0.3299931 -0.9439833 0.0}{0.0 0.9439833 0.3299931 0.0}{0.0 0.0 0.0 1.0}

XNA

Matrix.CreateRotationX(1.2345f)

結果:

{1,0,0,0} {0,0.3299931,0.9439833,0} {0,-0.9439833,0.3299931,0} {0,0,0,1}

違いは -0.9439833 と +0.9439833 です ... 異なる結果がある理由を誰か説明してもらえますか? 同じ機能じゃない?

ありがとうございました!

4

1 に答える 1

0

ああなるほど。わかりました、それは違いを生みます。最後に私は私の問題を解決しました。問題は、LWJGL の「変換」メソッドでした。それが私の最終的な作業コードです。多分私はそれを最適化できますか?

    public static Matrix4f createRotationX(float pitch) {
    Matrix4f mX = new Matrix4f();
    mX.m00 = 1;
    mX.m10 = 0;
    mX.m20 = 0;
    mX.m30 = 0;
    mX.m01 = 0;
    mX.m11 = (float) Math.cos(pitch);
    mX.m21 = (float) Math.sin(pitch);
    mX.m02 = 0;
    mX.m03 = 0;
    mX.m12 = (float) -Math.sin(pitch);
    mX.m22 = (float) Math.cos(pitch);
    mX.m23 = 0;
    mX.m03 = 0;
    mX.m13 = 0;
    mX.m23 = 0;
    mX.m33 = 1;
    return mX;
}

public static Matrix4f createRotationY(float yaw) {
    Matrix4f mY = new Matrix4f();
    mY.m00 = (float) Math.cos(yaw);
    mY.m10 = 0;
    mY.m20 = (float) -Math.sin(yaw);
    mY.m30 = 0;
    mY.m01 = 0;
    mY.m11 = 1;
    mY.m21 = 0;
    mY.m31 = 0;
    mY.m02 = (float) Math.sin(yaw);
    mY.m12 = 0;
    mY.m22 = (float) Math.cos(yaw);
    mY.m23 = 0;
    mY.m03 = 0;
    mY.m13 = 0;
    mY.m23 = 0;
    mY.m33 = 1;
    return mY;
}

public static Vector3f getNewTarget(Vector3f position,float pitch, float yaw){

    Matrix4f mx = MathUtil.createRotationX(pitch);
    Matrix4f my = MathUtil.createRotationY(yaw);      

    Matrix4f rotationMatrix = Matrix4f.mul(mx, my, null);

    Matrix4f def = new Matrix4f();
    def.m20 = -1;
    def.m00 = 0;
    def.m11 = 0;
    def.m22 = 0;
    def.m33 = 0;

    Matrix4f v4 = Matrix4f.mul(def,rotationMatrix,null);

    return new Vector3f(v4.m00+position.x, v4.m10+position.y, v4.m20+position.z);
}
于 2013-04-05T12:48:42.527 に答える