2012-07-13 29 views
0

我花了8個小時試圖找到解決方案。XNA Space Simulator中的奇怪問題四元數旋轉

所以這是我的問題。我的船像旋轉一樣旋轉,就像一根繩子一樣。當我離開初始位置越來越遠時,我開始轉動更多的怪物和怪物。就像我附加到一個字符串。

這裏是旋轉和運動的代碼。

public void MoveShip(List<InputAction> InputActionList, GameTime gameTime) 
    { 
     float second = (float)gameTime.ElapsedGameTime.TotalSeconds; 
     float currentTurningSpeed = second * TurningSpeed; 
     float leftRightRotation = 0; 
     float upDownRotation = 0; 
     float linearLeftRightRotation = 0; 
     foreach(InputAction action in InputActionList) 
     { 
      switch(action) 
      { 
       case InputAction.Left: 
        leftRightRotation -= currentTurningSpeed; 
        break; 
       case InputAction.Right: 
        leftRightRotation += currentTurningSpeed; 
        break; 
       case InputAction.Up: 
        upDownRotation += currentTurningSpeed; 
        break; 
       case InputAction.Down: 
        upDownRotation -= currentTurningSpeed; 
        break; 
       case InputAction.IncreaseSpeed: 
        if (ShipSpeed < MaxShipSpeed) 
         ShipSpeed += Acceleration; 
        break; 
       case InputAction.DecreaseSpeed: 
        if (ShipSpeed > MinShipSpeed) 
         ShipSpeed -= Acceleration; 
        break; 
       case InputAction.LinearLeft: 
        linearLeftRightRotation += currentTurningSpeed; 
        break; 
       case InputAction.LinearRight: 
        linearLeftRightRotation -= currentTurningSpeed; 
        break; 
       case InputAction.Fire1: 
        WeaponSystem2D.RequestFire(ShipPosition, ShipRotation); 
        break; 
      } 

     } 


     Quaternion currentRotation = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, 1), leftRightRotation) * 
      Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), upDownRotation) * 
      Quaternion.CreateFromAxisAngle(new Vector3(0, 1, 0), linearLeftRightRotation); 


     currentRotation.Normalize(); 
     ShipRotation *= currentRotation; 

     ShipPosition *= Vector3.Transform(new Vector3(0, 0, 1), ShipRotation) * (ShipSpeed * second); 

     ShipWorld = Matrix.CreateFromQuaternion(ShipRotation) * Matrix.CreateTranslation(ShipPosition); 

    } 

什麼問題?它爲什麼這樣做?我希望船在角錢上旋轉,因爲它是空間。編輯: - 模型不是問題。編輯2:沒關係,旋轉實際上是工作,這是我的天空盒被打破了!

+1

我知道你確定模型不是問題,但是模型的透視/中心/中點是否正確?我以前有類似的問題,這是我的模型。 – Karis 2012-07-13 17:51:57

回答

0

這段代碼實際上是完美的,我的天空盒使它看起來像是壞了。