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

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
mme

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

#1

投稿記事 by mme » 9年前

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

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

mme

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

#2

投稿記事 by mme » 9年前

以下のコードで解決しました。
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);
 
    //描画処理
    ...
}

mme

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

#3

投稿記事 by mme » 9年前

以下のコード追加でカメラのアングルを制限

コード:

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

mme

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

#4

投稿記事 by mme » 9年前

間違えてました

コード:

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

mme

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

#5

投稿記事 by mme » 9年前

また間違えてました

コード:

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


閉鎖

“C言語何でも質問掲示板” へ戻る