0

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

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;

    }
4

1 回答 1

0

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

我不明白你在用鼠标做什么......你应该在世界坐标系中获取鼠标光线以进行一些有意义的计算......你可以使用 Viewport.UnProject 来获取它。这是一个方法

我想你想实现一个轨迹球鼠标相机......在这里我找到了一个样本,但如果你寻找“xna arcball”,会有几个样本。

于 2012-12-26T08:38:59.593 回答