2013-08-26 52 views
3

幾天以來,我一直在XNA的一個項目中工作。該應用程序的主要想法是有一個平面彩色瓷磚網格,可單獨翻轉。即時通訊的問題在於,我無法真正弄清楚如何圍繞單箇中心軸進行翻轉旋轉。圍繞自己的軸旋轉3D圖元

現在正在發生的事情是瓷磚圍繞世界軸而不是自己旋轉。

每個圖塊都包含一個頂點數組,它在3D世界中創建一個二維正方形。

我很確定這個問題與我混淆了矩陣。

下面是每個拼貼代碼:

public Tile(Vector3 position, int size, Matrix world) 
    { 
     this.position = position; 
     this.size = size; 
     this.world = world; 

     vertices = new VertexPositionColor[6]; 

     for (int i = 0; i < vertices.Length; i++) 
      vertices[i].Position = position; 

     vertices[0].Position += new Vector3(0, 0, 0); 
     vertices[0].Color = Color.Pink; 
     vertices[1].Position += new Vector3(0, size, 0); 
     vertices[1].Color = Color.Yellow; 
     vertices[2].Position += new Vector3(0, size, size); 
     vertices[2].Color = Color.Green; 

     vertices[3].Position += new Vector3(0, size, size); 
     vertices[3].Color = Color.Green; 
     vertices[4].Position += new Vector3(0, 0f, size); 
     vertices[4].Color = Color.Blue; 
     vertices[5].Position += new Vector3(0, 0, 0); 
     vertices[5].Color = Color.Blue; 
    } 

    public VertexPositionColor[] Vertices 
    { 
     get{ return Functions.TransformVertices((VertexPositionColor[])vertices.Clone(), GetMatrix()); } 
    } 

    private Matrix GetMatrix() 
    { 
     return Matrix.CreateRotationZ(rot); 
    } 


    private void TransformVertices(Matrix matrix) 
    { 
     for (int i = 0; i < vertices.Length; i++) 
      vertices[i].Position = Vector3.Transform(vertices[i].Position, matrix); 
    } 

這裏是繪製方法:

protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     RasterizerState rasterizerState = new RasterizerState(); 
     rasterizerState.CullMode = CullMode.None; 
     GraphicsDevice.RasterizerState = rasterizerState; 
     viewMatrix = Matrix.CreateLookAt(cameraPos, new Vector3(0, 0, 0), Vector3.Up); 

     effect.CurrentTechnique = effect.Techniques["ColoredNoShading"]; 
     effect.Parameters["xView"].SetValue(viewMatrix); 
     effect.Parameters["xProjection"].SetValue(projectionMatrix); 

     Vector3 rotAxis = new Vector3(0, 0, 1); 
     rotAxis.Normalize(); 
     worldMatrix = Matrix.Identity; 
     effect.Parameters["xWorld"].SetValue(worldMatrix); 

     foreach (EffectPass pass in effect.CurrentTechnique.Passes) 
     { 
      pass.Apply(); 

      foreach(Tile tile in tiles) 
       GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, tile.Vertices, 0, 2, VertexPositionColor.VertexDeclaration); 
     } 

     base.Draw(gameTime); 
    } 

如果有幫助,我可以送你整個源。任何幫助將是無價的!

回答

6

要旋轉平鋪繞自身軸線:

(1) Transform the tile back to the origin. 
(2) Perform rotation. 
(3) Transform back to original position. 
+0

謝謝!有人可能會展示如何切實做到這一點? Matrix.CreateTranslation(tilePosition) – Pijm0

+0

下降者是否願意解釋他們爲什麼投反對票? – Paddyd