0

This is my code that gets the object to move around with the mouse, but it doesn't make the screen (camera) move along with it. I cant figure out why its doing that. Can anybody figure out why??

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 Answers1

0

You should use the debugger to watch the values that variables take... is the avatarPosition value changing ?

I don't understand what are you doing with the mouse... you should get the mouse ray in world coordinates to make the calculations with some sense... You can use Viewport.UnProject to get it. Here you are a method

I suppose you want to achieve an arcball mouse camera... Here I found one sample but there are several samples if you look for "xna arcball".

Community
  • 1
  • 1
Blau
  • 5,742
  • 1
  • 18
  • 27