ページ 11

Minecraftのようなカメラ操作がしたいです

Posted: 2016年3月19日(土) 00:30
by mme
C#でSharpDXを使用してモデルビューアを作っているのですが、
行列関係の知識がないせいで、カメラ操作のプログラムが上手くできません。
どのようにカメラを操作できるようにしたいかというと、
minecraftのような感じで、マウスの操作でカメラのXY軸を回転し、
矢印キーでカメラがY軸で回転した向きに移動する(カメラのX軸の向きは無視して上下移動はしない)。
というものです。

以下のようなプログラムを組んだのですが、
カメラの移動や向きの操作は正常に動きますが、上下が反転してしまいます。
LookAt系統の関数を使用せずにビュー行列を作ったせいでしょうか。
ビュー行列でZ軸(Roll)を180度回転させても無理でした。
(以下のコードは簡略化しています)

コード:

float rotX, rotY;
Vector3 translation;
Matrix viewMatrix;
void roop()
{
    //カメラの角度操作の入力処理
    const float rotAmount = 0.0025f;
    Point move = getMouseMove();
    rotY += move.X * rotAmout;
    rotZ += move.Y * rotAmout;

    //カメラの移動操作の入力処理
    const float moveAmount = 1f;
    if(上矢印押される)
    {
        rotMatrix = Matrix.RotationY(radian(0f) - rotY);
        translation += Vector3.Transform(new Vector3(0, 0, -moveAmount), rotMatrix);
    }
    if(下矢印押される)//上のradian(0f)のところの角度を変えてあとは同じ
    ...

    //ビュー行列作成
    viewMatrix = Matrix.Translation(translation)
                     * Matrix.RotationY(rotY)
                     * Matrix.RotationX(rotX);

    //描画処理
    ...
}
どのように変更したらよいでしょうか?
よろしくお願いいたします。

Re: Minecraftのようなカメラ操作がしたいです

Posted: 2016年3月19日(土) 15:13
by mme
以下のコードで解決しました。
RotationAxisで上の方向を指定すればよかったようです。

コード:

float rotX, rotY;
Vector3 translation;
Matrix viewMatrix;
void loop()
{
    //カメラの角度操作の入力処理
    const float rotAmount = 0.0025f;
    Point move = getMouseMove();
    rotY += move.X * rotAmout;
    rotZ += move.Y * rotAmout;
 
    //カメラの移動操作の入力処理
    const float moveAmount = 1f;
    if(上矢印押される)
    {
        translation += Vector3.Transform(new Vector3(0, 0, -moveAmount), Matrix.RotationY(rotationY));    
    }
    ...
 
    //ビュー行列作成
    viewMatrix = Matrix.Translation(translation)
                     * Matrix.RotationY(rotY)
                     * Matrix.RotationAxis(new Vector3(0, 1, 0), rotY);
 
    //描画処理
    ...
}

Re: Minecraftのようなカメラ操作がしたいです

Posted: 2016年3月19日(土) 15:15
by mme
以下のコード追加でカメラのアングルを制限

コード:

            if (rotationY < 0) rotationY += Radian(360f);
            if (rotationY >= MathUtil.Radian(360f)) rotationY -= Radian(360f);
            rotationX = Math.Max(rotationX, -Radian(90f));
            rotationX = Math.Max(rotationX, Radian(90f));

Re: Minecraftのようなカメラ操作がしたいです

Posted: 2016年3月19日(土) 15:19
by mme
間違えてました

コード:

    //ビュー行列作成
    viewMatrix = Matrix.Translation(translation)
                     * Matrix.RotationX(rotX)
                     * Matrix.RotationAxis(new Vector3(0, 1, 0), rotY);

Re: Minecraftのようなカメラ操作がしたいです

Posted: 2016年3月19日(土) 15:21
by mme
また間違えてました

コード:

    //ビュー行列作成
    viewMatrix = Matrix.Translation(translation)
       * Matrix.RotationAxis(new Vector3(0, 1, 0), rotY)
                     * Matrix.RotationX(rotX);