1

3D ゲーム用のカメラを作成しています。カメラは次の変数で構成されています。

static Vector3 cameraLookAt = new Vector3(0, 0, 0);
static Vector3 cameraPosition;
static float cameraZoom = 25; // Field of view 
const int cameraZoomNear = 3; // The furthest the camera can zoom in 
const int cameraZoomFar = 45; // The furthest the camera can zoom out 
static float cameraPitch; // Amount to change the pitch 
static float cameraRotation; // Amount to change the rotation

デフォルトでは、「cameraLookAt」は (0, 0, 0) にあり、「cameraPosition」は (0, 0, 10) にあります。これは、カメラが「cameraLookAt」位置の真上にあり、下のオブジェクトの鳥瞰図を提供することを意味します(赤緑と青の xyz 平面テスト オブジェクト)

「cameraLookAt」は x 軸と y 軸に沿って移動でき、「cameraLookAt」位置と「cameraPitch」と「cameraRotation」の現在の値を基準にして「cameraPosition」が計算されます。

現在、x/y 軸の周りを細かく移動し、カメラのピッチを変更して、鳥瞰図から地上図に移動できます。私が問題を抱えているビットは、カメラの回転を機能させることです。

回転値が変更された場合、ピッチが変更された場合にのみ効果があります - プログラムが開始されたときのデフォルトから (「cameraLookAt」を真下に見ています)

値が変化すると、Z 軸を中心に回転できますが、ビューも回転するため、180 度回転するまでにカメラも 180 度回転し、ビューが上下逆になります。

以下は、「cameraPosition」がどこにあるかを解決するコードです。これらの行内の行が修正されていると確信しています。

        cameraPosition = cameraLookAt + new Vector3(0, 0, 10);

        Vector3 temp = Vector3.Normalize(cameraPosition - cameraLookAt); //creates a unit length vector that points in the direction from 'lookAt' to 'position'
        cameraPosition = Vector3.Transform(cameraPosition - cameraLookAt, Matrix.CreateRotationX(cameraPitch)) + cameraLookAt;
        cameraPosition = Vector3.Transform(cameraPosition - cameraLookAt, Matrix.CreateRotationZ(cameraRotation)) + cameraLookAt;

        view = Matrix.CreateLookAt(cameraPosition, cameraLookAt, new Vector3(0.0f, 1.0f, 0.0f));
        projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(cameraZoom), graphics.GraphicsDevice.Viewport.AspectRatio, nearClip, farClip);

また、最初にコーディングするのに役立つことがわかったq&aがありますXNA Rotate Camera Around It's CreateLookAt "Target"

4

1 に答える 1

1

オフセットは、長さを変更せずに回転し、カメラ位置に追加する必要があります。

私はそれがこのようであるべきだと思います:

   offset = new Vector3(0, 0, 10);

   Vector3 rotatedOffset = Vector3.Transform( offset, 
                                      Matrix.CreateRotationX(cameraPitch) 
                                      * Matrix.CreateRotationZ(cameraRotation));

   cameraPosition = cameraLookAt + rotatedOffset;
于 2012-06-18T08:20:39.450 に答える