2011-04-14 64 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