我想編碼兩個合適的鑰匙來更新適當速度的船舶前進/後退方向的位置,我想出的是: 在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;
我不確定我是否理解您的問題,但您是否要分配新值而不是修改以前的值? 'position.Z - = 50.0f;''和position.Z + = 50.0f',分別。 –
我已經將這艘船置於屏幕中間,我可以左右旋轉,上下左右移動。從相機的角度來看,後退和前進就像是一樣。我希望它能夠回到原來的位置。我很抱歉,如果這不明確,但我是全新的xna – Dave
我會說2個枚舉,一個與RelativeDirection在,左,右,前進,後退。另一個是絕對方向(北,東,南,西),那麼你可以說,如果絕對方向是東方,而你向左轉,它應該向北。應該可以很容易地完成這個 – AssaultingCuccos