2012-12-26 36 views
0

這是我的代碼,讓對象隨鼠標移動,但它不會使屏幕(相機)隨之移動。我無法弄清楚爲什麼這樣做。任何人都可以弄清楚爲什麼?爲什麼我的相機無法實現其目標? (XNA 4.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; 

    } 

回答

0

您應該使用調試器來觀看該變量取...是avatarPosition值更改值?

我不明白你用鼠標在做什麼......你應該在世界座標中獲得鼠標光線,以便使計算具有某種意義......你可以使用Viewport.UnProject來獲取它。 Here you are a method

我想你想實現軌跡球鼠標攝像頭...... Here I found one sample但有幾個樣本,如果你尋找「XNA軌跡球」。