2011-04-14 4 views
1

1 축의 우주선을 모두 제어하면 깊이가 (z)이고 z 평면이 360도 회전하므로 2 축이됩니다. 나는 또한 그 위치를 유지해야하는 바로 뒤에 카메라가 있습니다. 3 위가 올 때 모두 나 빠진다. 내가 당신에게 몇 가지 코드를 보여주지 :XNA에서 3 축의 우주선을 제어합니다.

그리기 우주선의 방법 : 내가 다른 축과 결합하는 방법을 잘 모릅니다되는 worldMatrix2이 구현 축 추가에 대한

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(); 
      } 
     } 

여기 가 실패하는 부분입니다 . 나는 그것을 곱합니까? 또한 :

카메라 업데이트 방법은 비슷한 문제가있다 : 마지막으로 여기

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); 
     } 
    } 

이 게임 클래스 업데이트 방법 : 코드에서 어딘가에

spaceship.Update(currentGamePadState, 
    currentKeyboardState); // this will be cahnged when asteroids are placed in the game, by adding a new parameter with asteroids. 
      float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio; 
      gameCamera.Update(spaceship.ForwardDirection,spaceship.VerticalDirection, 
       spaceship.Position, aspectRatio); 

답변

1

, 당신은 아마 방법론을 가질 수 변수 'ForwardDirection'을 조작하고 설정합니다. & 'VerticalDirection'은 Y 축 주위의 각도 값을 각각 나타내는 것처럼 보입니다. & X 축. 아마도 Z 축 주위의 각도 값을 나타내는 변수를 가질 수도 있습니다. 또한이 변수는 궁극적으로 당신이 우주선의 방향을 프레임마다 저장하는 방법이라고 가정합니다.

이 각도를 사용하여 계속 오리엔테이션을 표현하려고하면 우주선을 제어하기가 어렵습니다.

사용할 수있는 방향 표현에는 여러 가지 유형이 있습니다. 3 각 접근 방식은 3 차원에서 회전을 결합 할 때 고유 한 약점이 있습니다.

패러다임을 바꾸고 Matrix 또는 Quaternion 형식으로 방향을 저장하고 조작하는 것이 좋습니다. 일단 행렬 또는 쿼터니언을 조작하는 방법을 배우면, 당신이하려는 것은 거의 믿을 수 없을 정도로 쉬워집니다.

+0

쿼터니언을 사용하면 모든 것이 더 쉬워집니다. 수학을 읽고 하루를 투자하십시오. – Boinst