2013-10-21 52 views
1

在代碼中Game1.cs頂部我有:旋轉地形時,我該如何記住旋轉的最後位置?

private float angle = 0f; 
private float angleRightLeft = 0f; 
private float angleUpDown = 0f; 
private bool rotationDirection; 
private int terrainWidth = 4; 
private int terrainHeight = 3; 
private float[,] heightData; 

在draw方法我有這樣的代碼:

protected override void Draw(GameTime gameTime) 
{ 
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue); 
    device.Clear(Color.Black); 
    RasterizerState rs = new RasterizerState(); 
    rs.CullMode = CullMode.None; 
    rs.FillMode = FillMode.WireFrame; 
    device.RasterizerState = rs; 
    if (rotationDirection == true) 
    { 
     worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left, 
               angleRightLeft); 
     worldMatrix = Matrix.CreateTranslation(-terrainWidth/2.0f, 
               0, 
               terrainHeight/2.0f) * 
               Matrix.CreateRotationY(angleRightLeft); 
    } 
    else 
    { 
     worldMatrix = Matrix.CreateTranslation(-terrainWidth/2.0f, 
               0, 
               terrainHeight/2.0f) * 
               Matrix.CreateRotationX(angleUpDown); 
    } 
    effect.CurrentTechnique = effect.Techniques["ColoredNoShading"]; 
    effect.Parameters["xView"].SetValue(viewMatrix); 
    effect.Parameters["xProjection"].SetValue(projectionMatrix); 
    effect.Parameters["xWorld"].SetValue(worldMatrix); 
} 

和按鍵輸入過程的方法:

private void ProcessInput(float amount) 
{ 
    previousState = currentState; 
    currentState = Mouse.GetState(); 
    Vector3 moveVector = new Vector3(0 , 0 , 0); 
    KeyboardState keyState = Keyboard.GetState(); 
    if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W)) 
    { 
     angleUpDown += 0.05f; 
     rotationDirection = false; 
    } 

    if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S)) 
    { 
     angleUpDown -= 0.05f; 
     rotationDirection = false; 
    } 

    if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D)) 
    { 
     angleRightLeft += 0.05f; 
     rotationDirection = true; 
    } 

    if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A)) 
    { 
     angleRightLeft -= 0.05f; 
     rotationDirection = true; 
    } 

    if (keyState.IsKeyDown(Keys.Q)) 
     cameraPosition += new Vector3(0, 1, 0); 
    if (keyState.IsKeyDown(Keys.Z)) 
     cameraPosition += new Vector3(0, -1, 0); 
    if (keyState.IsKeyDown(Keys.Escape)) 
    { 
     this.graphics.PreferredBackBufferWidth = 800; 
     this.graphics.PreferredBackBufferHeight = 600; 
     this.graphics.IsFullScreen = false; 
     this.graphics.ApplyChanges(); 
    } 
    if (this.graphics.PreferredBackBufferWidth < 1920) 
    { 
     /* 
     if (WasMouseLeftClick()) 
     { 
      changeScreenNode(this.graphics, 1920, 1080, true); 
     }*/ 

     if (WasDoubleClick()) 
     { 
      changeScreenNode(this.graphics, 1920, 1080, true); 
     } 
    } 

    if (WasMouseLeftClick()) 
    { 
     previousClick = DateTime.Now; 
    } 
} 

更新方法:

protected override void Update(GameTime gameTime) 
{ 
    fpsm.Update(); 
    float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds/1000.0f; 
    ProcessInput(timeDifference); 
    modelRotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds * 
          MathHelper.ToRadians(0.1f); 
    base.Update(gameTime); 
} 

問題是,當我按下D按鈕並將地形向右旋轉時,如果停止按住D並按下W將地形向上旋轉,則地形首先移回到其原始位置。

我希望它會保持在按下D或A或S或W後的位置上,當我點擊另一個鍵時,它將從那裏繼續並且不會先復位到原來的位置。

我想我需要改變繪製方法中的東西。

當我點擊一個按鍵,然後按另一個按鍵時,它會從停止的位置開始旋轉,而不是首先返回/重置到原來的位置,我該怎麼做?

編輯**

這是我在抽籤方法做:

if (rotationDirection == true) 
      { 
       worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left, angleRightLeft); 
       worldMatrix = Matrix.CreateTranslation(-terrainWidth/2.0f, 0, terrainHeight/2.0f) * Matrix.CreateRotationY(angleRightLeft); 
       oldPosition = worldMatrix; 
      } 
      else 
      { 
       worldMatrix = Matrix.CreateTranslation(-terrainWidth/2.0f, 0, terrainHeight/2.0f) * Matrix.CreateRotationX(angleUpDown); 
       oldPosition = worldMatrix; 
      } 

oldPosition是型矩陣。 然後在按鍵處理方法中,我對每個按下的鍵進行了處理:worldMatrix = oldPosition;

if (keyState.IsKeyDown(Keys.Up) || keyState.IsKeyDown(Keys.W)) 
      { 
       angleUpDown += 0.05f; 
       rotationDirection = false; 
       worldMatrix = oldPosition; 
      } 

但它仍然不能正常工作。 我做錯了什麼?

+0

爲什麼不扔的當前位置到它的旋轉完畢後,另一個變量?那麼當你按下另一個按鈕時,你可以回到那個「舊」的位置? – sab669

+0

sab669請看我的問題,我剛剛更新了它的底部添加了我所做的改變,試圖做你說的話。但它的工作還不好。 –

回答

2

如果我正在閱讀您的問題,請在更改輸入方向時保持以前的旋轉。與此

worldMatrix = Matrix.CreateTranslation(-terrainWidth/2.0f, 0, terrainHeight/2.0f) * Matrix.CreateRotationX(angleUpDown) * Matrix.CreateRotationY(angleLeftRight); 

現有代碼

if (rotationDirection == true) 
     { 
      worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left, angleRightLeft); 
      worldMatrix = Matrix.CreateTranslation(-terrainWidth/2.0f, 0, terrainHeight/2.0f) * Matrix.CreateRotationY(angleRightLeft); 
      oldPosition = worldMatrix; 
     } 
     else 
     { 
      worldMatrix = Matrix.CreateTranslation(-terrainWidth/2.0f, 0, terrainHeight/2.0f) * Matrix.CreateRotationX(angleUpDown); 
      oldPosition = worldMatrix; 
     } 

沒有一次只使用一個軸旋轉矩陣:如果是這樣的話,你可以通過更換此實現這一目標。如果您在下一次更新/繪圖中更改了方向,則會將之前的軸重置爲零。上面更新的代碼使用了這兩個值。除此之外,你還可以使用Matrix.CreateFromYawPitchRoll像這樣(也許是我錯了這裏,所以仔細檢查它排序):

worldMatrix = Matrix.CreateTranslation(-terrainWidth/2.0f, 0, terrainHeight/2.0f) * Matrix.CreateFromYawPitchRoll(angleUpDown, angleLeftRight, 0);