2011-07-13 115 views
1

我的XNA遊戲使用Farseer Physics,它是一個2D物理引擎,帶有可選的物理引擎數據渲染器,可以幫助您進行調試。可視化調試數據非常有用,所以我根據相機的狀態設置了它。除z軸旋轉外,此功能完美無缺。請參閱我有一個支持移動,縮放和z軸旋轉的相機類。我的調試類使用Farseer的調試渲染器來創建矩陣,以便根據相機繪製調試數據,並且它的效果很好,除了一件事情之外...... z軸旋轉使用屏幕的左上角作爲( 0,0),而我的相機使用視口中心作爲(0,0)旋轉。有沒有人對我有任何提示?如果我可以使調試抽屜從中心旋轉,那麼它可以完美地與我的相機配合使用。如何繞中心旋轉矩陣?

 public void Draw(Camera2D camera, GraphicsDevice graphicsDevice) 
    { 
     // Projection (location and zoom) 
     float width = (1f/camera.Zoom) * ConvertUnits.ToSimUnits(graphicsDevice.Viewport.Width/2); 
     float height = (-1f/camera.Zoom) * ConvertUnits.ToSimUnits(graphicsDevice.Viewport.Height/2); 
     //projection = Matrix.CreateOrthographic(width, height, 1f, 1000000f); 
     projection = Matrix.CreateOrthographicOffCenter(
      -width, 
      width, 
      -height, 
      height, 
      0f, 1000000f); 

     // View (translation and rotation) 
     float xTranslation = -1 * ConvertUnits.ToSimUnits(camera.Position.X); 
     float yTranslation = -1 * ConvertUnits.ToSimUnits(camera.Position.Y); 
     Vector3 translationVector = new Vector3(xTranslation, yTranslation, 0f); 
     view = Matrix.CreateRotationZ(camera.Rotation) * Matrix.Identity; 
     view.Translation = translationVector; 

     DebugViewXNA.RenderDebugData(ref projection, ref view); 
    } 

回答

0

解決這類問題的一種常用方法是將所討論的對象移動到「中心」,旋轉並將其移回。 因此,在這種情況下,我建議應用一種轉換,將相機「向上和向下」移動一半的屏幕尺寸,應用旋轉並將其移回。

+0

我試過了,但它似乎仍然圍繞左上角旋轉。也許我是誤解;我可以看到代碼片段嗎? –

0

一般來說,爲了圍繞點(x,Y,Z)進行旋轉時,需要被分解成3個概念性部分的操作:

  1. T是平移矩陣,通過平移( - x,-y,-z)
  2. R是圍繞相關軸旋轉的旋轉矩陣。
  3. T 1 -1是轉換回(X,Y,Z)

你後的矩陣是這些3的乘法運算的結果,按相反順序矩陣:

M = T^-1 * R^T

您應該使用的x,y,z是您的相機的位置。