2012-11-19 88 views
1

我想編碼兩個合適的鑰匙來更新適當速度的船舶前進/後退方向的位置,我想出的是: 在Cmodel中:將船舶向前移向船的方向C#

//Update method 
     public void Update(Vector3 position, Vector3 orientation) 
     { 

      //TO DO - Create an updated version of this.local matrix 


      this.local *= Matrix.CreateFromYawPitchRoll(orientation.Y, orientation.X, orientation.Z); 

      this.local *= Matrix.CreateTranslation(position.X, position.Y, position.Z); 
     } 

比的Game1:

protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     // TODO: Add your update logic here 
     //Update the camera 
     camera.Update(); 

     KeyboardState keyState = Keyboard.GetState(); 

     // The time since Update was called last. 
     float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; 

     float RotationAngle = 0; 
     RotationAngle += elapsed; 
     float circle = MathHelper.Pi * 2; 
     RotationAngle = RotationAngle % circle; 

     Vector3 orientation = new Vector3(0, 0, 0); 
     Vector3 position = new Vector3(0, 0, 0); 


     if (keyState.IsKeyDown(Keys.W)) 
      orientation.X = RotationAngle; 

     if (keyState.IsKeyDown(Keys.S)) 
      orientation.X = -RotationAngle; 

     if (keyState.IsKeyDown(Keys.A)) 
      orientation.Y = RotationAngle; 

     if (keyState.IsKeyDown(Keys.D)) 
      orientation.Y = -RotationAngle; 

     if (keyState.IsKeyDown(Keys.Z)) 
      orientation.Z = RotationAngle; 

     if (keyState.IsKeyDown(Keys.X)) 
      orientation.Z = -RotationAngle; 


     //TO DO - Code two suitable keys to update position in the forward/backward direction of Ship travel at a suitable velocity 


     if (keyState.IsKeyDown(Keys.F)) 
      position.Z = 1f; 
     if (keyState.IsKeyDown(Keys.B)) 
      position.Z = 1f; 


     //Update the first model i.e. the Ship 
     scene[0].Update(position, orientation); 

     base.Update(gameTime); 
    } 

的事情是,當我向左或向右旋轉這些按鈕仍然工作就像回到朝我(相機)轉寄船舶的鼻子。什麼是Iadd使它迴歸船舶的「鼻子」,船的方向?

答案!!!! 所有我需要做的工作是不同的矩陣:旋轉首先比翻譯和結束本地!謝謝everyboy尋求答案。這真是我的頭大時間!

this.local = Matrix.CreateFromYawPitchRoll(orientation.Y, orientation.X, orientation.Z) * Matrix.CreateTranslation(position.X, position.Y, position.Z) * this.local; 
+0

我不確定我是否理解您的問題,但您是否要分配新值而不是修改以前的值? 'position.Z - = 50.0f;''和position.Z + = 50.0f',分別。 –

+0

我已經將這艘船置於屏幕中間,我可以左右旋轉,上下左右移動。從相機的角度來看,後退和前進就像是一樣。我希望它能夠回到原來的位置。我很抱歉,如果這不明確,但我是全新的xna – Dave

+1

我會說2個枚舉,一個與RelativeDirection在,左,右,前進,後退。另一個是絕對方向(北,東,南,西),那麼你可以說,如果絕對方向是東方,而你向左轉,它應該向北。應該可以很容易地完成這個 – AssaultingCuccos

回答

0

記住你在學校的數學課!你的「船」是在一個點上,你有你想要移動它的速度/距離和角度。下面是一些pseude代碼,以幫助:

float a = math.rad(angle); 
float newX = currentX + math.sin(a) * distanceToMove; 
float newZ = currentZ - math.cos(a) * distanceToMove; 

這是一個基本的矢量計算,我怎麼過我不能確定軸的權利,但如果你鼓搗X,Y和Z和你一點點應該算出來!對不起,如果上面的公式是錯誤的,這是一個懶惰的時間以前,因爲我實際上做了這樣的數學;)

0

我可能是不正確的,因爲我在AS3做了這個。

當你旋轉船,你需要重新計算它的運動。

爲了簡化,如果只有XY,並且該按鍵移動10個單位(無論您的單位是什麼(像素))。當您將船舶旋轉到某個角度時,您需要重新計算其在XY軸上的移動。

他的移動功能如下:

Ship.X += Math.Cos(currentAngle) * Ship.DefaultMoveUnits; 
Ship.Y += Math.Sin(currentAngle) * Ship.DefaultMoveUnits; 

這是XYZ同樣的原則。

0

爲了向前邁進,您需要了解船的方向。這不是問題,因爲你在你的代碼已經設置方向:Matrix.CreateFromYawPitchRoll(orientation.Y, orientation.X, orientation.Z);
使用此RotationMatrix可以提取船舶鼻子朝指向所需方向值:

//this method uses 'orientation' as the current orientation of the ship 
public Vector3 GetForward() 
{ 
    Matrix rotation = Matrix.CreateFromYawPitchRoll(orientation.Y, orientation.X, orientation.Z); 
    //i don't know the matrix structure of XNA, but it should be possible 
    //to access each field seperately 
    //M13 is here: row 1, column 3 
    //M23 is here: row 2, column 3 and so on 
    return new Vector3(rotation.M13, rotation.M23, rotation.M33); 
} 

//now that you got the appropriate vector 
//you just can move into the direction of this vector like 
Vector3 position = new Vector3(0, 0, 0); 
if(keyState.IsKeyDown(Keys.F)) 
{ 
    position += ship.GetForward(); 
} 

position *= elapsed; 

//update the position of the ship here 

你可以得到正確的最多用同樣的方法載體,只是用旋轉矩陣的其它列:

public Vector3 GetRight() 
{ 
    Matrix rotation = Matrix.CreateFromYawPitchRoll(orientation.Y, orientation.X, orientation.Z); 
    return new Vector3(rotation.M11, rotation.M21, rotation.M31); 
} 

public Vector3 GetUp() 
{ 
    Matrix rotation = Matrix.CreateFromYawPitchRoll(orientation.Y, orientation.X, orientation.Z); 
    return new Vector3(rotation.M12, rotation.M22, rotation.M32); 
} 

你可以得到所有其他方向(向後,向左,向下)通過否定相應的載體。所以要使用-GetForward()

+0

仍然無法做到這一點。我想我太周了。這應該是非常容易的事情。只是爲了得到船的當前y軸,而不是直接朝這個方向。 – Dave

+0

@Dave你的問題到底是什麼?更好的說,我不知道你答案的哪一部分? –

+0

我有一個答案!非常感謝大家,再多花幾小時就可以了!我所要做的就是不同的矩陣乘法! 30秒的工作和12小時找到它! – Dave

0

爲了使它工作的所有我需要做的就是改變矩陣乘法:

this.local =矩陣。CreateFromYawPitchRoll(orientation.Y,orientation.X,orientation.Z)* Matrix.CreateTranslation(position.X,position.Y,position.Z)* this.local;