为什么我的相机不遵循目标呢? (XNA 4.0)

时间:2012-12-26 02:12:28

标签: 3d xna camera

这是我的代码,使用鼠标移动对象,但它不会使屏幕(相机)随之移动。我无法弄清楚为什么这样做。任何人都可以找出原因??

void UpdateCamera()
    {
        currentmousestate = Mouse.GetState();
        // Calculate the camera's current position.
        Vector3 cameraPosition = avatarPosition;

        Matrix rotationMatrix = Matrix.CreateRotationY(avatarYaw);

        // Create a vector pointing the direction the camera is facing.

        // Compare the original mouse position with the new one,
        // and divide that by a float of -80.

        cameraReference = new Vector3((currentmousestate.X - originalstate.X)/-80f, (currentmousestate.Y - originalstate.Y)/-80f, 1);
        avatarYaw = (float)(currentmousestate.X - originalstate.X)/-160f;

        Vector3 transformedReference = Vector3.Transform(cameraReference, rotationMatrix);

        // Calculate the position the camera is looking at.
        Vector3 cameraLookat = cameraPosition + transformedReference;

        // Set up the view matrix and projection matrix.
        view = Matrix.CreateLookAt(cameraPosition, cameraLookat, new Vector3(0.0f, 1.0f, 0.0f));

        Viewport viewport = graphics.GraphicsDevice.Viewport;
        float aspectRatio = (float)viewport.Width / (float)viewport.Height;

        proj = Matrix.CreatePerspectiveFieldOfView(viewAngle, aspectRatio, nearClip, farClip);
        //originalstate = currentmousestate;

    }

1 个答案:

答案 0 :(得分:0)

您应该使用调试器来观察变量所采用的值... avatarPosition值是否在变化?

我不明白你用鼠标做了什么......你应该在世界坐标中得到鼠标光线以便进行计算......你可以使用Viewport.UnProject来获取它。 Here you are a method

我想你想要实现一个弧形鼠标相机...... Here I found one sample但是如果你寻找“xna arcball”,有几个样本。