2011-08-18 38 views
1

所以我知道有關於建設XNA 2D相機幾個問題/答案然而人們似乎只是很高興不用解釋了發佈他們的代碼。 我正在尋找更多的解釋我做錯了什麼。XNA 4.0:2D相機Y和X會在錯誤的方向

首先,我瞭解整個世界 - >查看 - >投影 - >屏幕轉換。

我的目標是擁有一個以視口中心爲中心的相機對象,並且當相機的位置向上移動時,它與視口中的向上移動相關,並且當移動到右邊時,它將關聯向右移動視。

我很難實現該功能,因爲視口的Y值是顛倒的。

//In Camera Class 
private void UpdateViewTransform() 
{ 
//My thinking here was that I would create a projection matrix to center the camera and then flip the Y axis appropriately 
    Matrix proj = Matrix.CreateTranslation(new Vector3(_viewport.Width * 0.5f, _viewport.Height * 0.5f, 0)) * 
        Matrix.CreateScale(new Vector3(1f, -1f, 1f)); 

//Here is the camera Matrix. I have to give the Inverse of this matrix to the Spritebatch I believe since I want to go from World Coordinates to Camera Coordinates 
    _viewMatrix = Matrix.CreateRotationZ(_rotation) * 
        Matrix.CreateScale(new Vector3(_zoom, _zoom, 1.0f)) * 
        Matrix.CreateTranslation(_position.X, _position.Y, 0.0f); 

    _viewMatrix = proj * _viewMatrix; 

} 

有人可以幫助我瞭解如何構建我的視圖轉換以傳遞到SpriteBatch,以便實現我正在尋找的內容。

編輯

這作爲轉換似乎工作但我不確定這是爲什麼。有人也許可以打破它,我的理解:

Matrix proj = Matrix.CreateTranslation(new Vector3(_viewport.Width * 0.5f, _viewport.Height * 0.5f, 0)); 
    _viewMatrix = Matrix.CreateRotationZ(_rotation) * 
       Matrix.CreateScale(new Vector3(_zoom, _zoom, 1.0f)) * 
       Matrix.CreateTranslation(-1 * _position.X, _position.Y, 0.0f); 

    _viewMatrix = proj * _viewMatrix; 

我已經建立了一個光線跟蹤之前,所以我應該瞭解你的理解,我的困惑在於事實上,它是2D和SpriteBatch是隱藏它從我這兒做。 謝謝! Farid

+0

您是否嘗試過簡單地使用'1f'而不是'-1f'? – ChrisF

+0

使用1f導致x翻譯被翻轉:( – Setheron

回答

2

如果您用縮放矩陣翻轉Y軸上的所有內容,這意味着您正在翻轉SpriteBatch正在繪製的模型(紋理四邊形)。這意味着你也必須改變你的收尾順序(例如:背面剔除是在解釋你正在繪製面向相機的三角形的背面,所以它會剔除它們,所以你必須改變它使用的規則)。

默認情況下,SpriteBatch使用RasterizerState.CullCounterClockwise。當您致電SpriteBatch.Begin時,您需要傳遞RasterizerState.CullClockwise

,當然,Begin是你在你的變換矩陣通過。

我還沒有仔細檢查你的矩陣運算 - 雖然我有一個懷疑,順序不正確。我建議您創建一個非常簡單的測試應用程序,並逐個構建您的轉換。

+0

因此,沒有辦法像我期望的那樣通過轉換獲得我的座標軸? – Setheron

+0

@Setheron:我的答案的重點是,是的,你可以。只是反轉軸也需要顛倒纏繞順序,而且我不知道要使用哪種矩陣操作,而不是我的頭頂部。 msdn.com/b/shawnhar/archive/2010/04/05/spritebatch-and-custom-shaders-in-xna-game-studio-4-0.aspx),顯示SpriteBatch使用的內部矩陣,應用到一個'BasicEffect'(也許你甚至可以使用這個代碼來使用'BasicEffect'和'SpriteBatch'並且直接反向投影矩陣。) –

+0

我能夠使我的座標系按預期工作(帶有正值Y軸)和我的精靈按照安德魯的建議渲染。 – BrettMStory

0

所以後只是想着邏輯,我能推斷出正確的改造。 我將概述這裏的步驟,以便讓任何人想要真正的故障:

重要的是要了解什麼是相機轉換或視圖轉換。 視圖變換通常需要從座標相對於到您的相機到世界空間座標。視圖變換的逆函數將相對於您的相機創建一個世界座標!

創建一個視圖矩陣

  1. 應用相機的旋轉。我們只是在Z軸上做這件事,因爲這是2D相機。
  2. 應用相機的轉換。這是有道理的,因爲我們希望世界座標是攝像機座標和物體相對於攝像機的總和。
    1. 您可能會注意到我將Y分量乘以-1。這是因爲屏幕的Y值指向下方,因此增加相機的Y位置與向上移動相關。
  3. 應用相機的變焦

現在矩陣的逆將做世界座標 - >查看Cordinates。

我也選擇將我的相機居中在屏幕中央,所以我包含了一個預先附加的翻譯。

Matrix proj = Matrix.CreateTranslation(new Vector3(_viewport.Width * 0.5f, _viewport.Height * 0.5f, 0)); 

     _viewMatrix = Matrix.CreateRotationZ(moveComponent.Rotation) * 
        Matrix.CreateTranslation(moveComponent.Position.X, -1 * moveComponent.Position.Y, 0.0f) * 
        Matrix.CreateScale(new Vector3(zoomComponent.Zoom, zoomComponent.Zoom, 1.0f)); 

     _viewMatrix = proj * Matrix.Invert(_viewMatrix); 
1

我用XNA打了很多的努力得到它要像其他引擎我都合作過......我reccomendation,這是不值得的 ...只是去使用XNA標準並使用Matrix幫助程序方法創建您的Perspective/Viewport矩陣。