2011-04-14 41 views
1

當我控制在1軸的飛船一切都很好,也就是深度(z)和z平面上的旋轉360度,這樣就是2軸。我也有一臺相機,我必須保持它的地位。當第三個到位時,所有的都變壞了。讓我告訴你一些代碼: 這裏是失敗的部分:飛船控制XNA中3軸的飛船

抽獎方法:

public void Draw(Matrix view, Matrix projection) 
     { 
public float ForwardDirection { get; set; } 
     public float VerticalDirection { get; set; } 

      Matrix[] transforms = new Matrix[Model.Bones.Count]; 
      Model.CopyAbsoluteBoneTransformsTo(transforms); 

      Matrix worldMatrix = Matrix.Identity; 
      Matrix worldMatrix2 = Matrix.Identity; 

      Matrix rotationYMatrix = Matrix.CreateRotationY(ForwardDirection); 
      Matrix rotationXMatrix = Matrix.CreateRotationX(VerticalDirection); // me 

      Matrix translateMatrix = Matrix.CreateTranslation(Position); 

      worldMatrix = rotationYMatrix * translateMatrix; 
      worldMatrix2 = rotationXMatrix * translateMatrix; 
      //worldMatrix*= rotationXMatrix; 

      foreach (ModelMesh mesh in Model.Meshes) //NEED TO FIX THIS 
      { 
       foreach (BasicEffect effect in mesh.Effects) 
       { 
        effect.World = 
         worldMatrix * transforms[mesh.ParentBone.Index]; ; //position 
        //effect.World = 
         //worldMatrix2 * transforms[mesh.ParentBone.Index]; ; //position 
        effect.View = view; //camera 
        effect.Projection = projection; //2d to 3d 

        effect.EnableDefaultLighting(); 
        effect.PreferPerPixelLighting = true; 
       } 
       mesh.Draw(); 
      } 
     } 

對於額外的軸worldMatrix2實現,我不知道如何與其他軸結合。我是否乘以它?另外:

相機更新方法也有類似的問題:

public void Update(float avatarYaw,float avatarXaw, Vector3 position, float aspectRatio) 
     { 
      //Matrix rotationMatrix = Matrix.CreateRotationY(avatarYaw); 
      Matrix rotationMatrix2 = Matrix.CreateRotationX(avatarXaw); 

      //Vector3 transformedheadOffset = 
       //Vector3.Transform(AvatarHeadOffset, rotationMatrix); 

      Vector3 transformedheadOffset2 = Vector3.Transform(AvatarHeadOffset, rotationMatrix2); 
      //Vector3 transformedheadOffset2 = Vector3.Transform(transformedheadOffset, rotationMatrix2); 


      //Vector3 transformedReference = 
       //Vector3.Transform(TargetOffset, rotationMatrix); 


      Vector3 transformedReference2 = Vector3.Transform(TargetOffset, rotationMatrix2); 
      //Vector3 transformedReference2 = Vector3.Transform(transformedReference, rotationMatrix2); 


      Vector3 cameraPosition = position + transformedheadOffset2; /** + transformedheadOffset; */ 
      Vector3 cameraTarget = position + transformedReference2; /** + transformedReference; */ 

      //Calculate the camera's view and projection 
      //matrices based on current values. 
      ViewMatrix = 
       Matrix.CreateLookAt(cameraPosition, cameraTarget, Vector3.Up); 
      ProjectionMatrix = 
       Matrix.CreatePerspectiveFieldOfView(
        MathHelper.ToRadians(GameConstants.ViewAngle), aspectRatio, 
        GameConstants.NearClip, GameConstants.FarClip); 
     } 
    } 

最後這裏是遊戲類更新的方法:

回答

1
在你的代碼

某處,你可能有方法即操縱並設置變量'ForwardDirection''VerticalDirection',這些變量似乎分別代表圍繞X軸的角度值。據推測,你可能打算有一個變量來表示圍繞Z軸的角度值。我還假設(和你的代碼暗示)這些變量最終是如何存儲你的宇宙飛船的方向。

只要你繼續嘗試用這些角度表示方位,你就會發現很難實現對你的飛船的控制。

有幾種類型的方向表示可用。當涉及到三維結合旋轉時,3角方法具有固有的弱點。

我的建議是,你轉移範例,並考慮以矩陣或四元數形式存儲和操縱你的方向。一旦你學會操縱一個矩陣或四元數,你試圖做的事變得簡直難以置信。

+0

+1當你使用四元數時,一切都變得更容易。投入一天的時間閱讀數學。 – Boinst 2012-05-10 22:25:50