在我的代码顶部,我添加了:
Matrix rotation;
private float angleRightLeft = 0f;
private float angleUpDown = 0f;
在构造函数中我做了:
rotation = Matrix.Identity;
在更新方法中,我调用键:
float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
ProcessInput(timeDifference);
在绘图方法中,我使用变量 angleRightLeft 来旋转世界(地形):
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;
//Matrix worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f);
//worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angle);
worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left,angleRightLeft);
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angleRightLeft);
effect.CurrentTechnique = effect.Techniques["ColoredNoShading"];
effect.Parameters["xView"].SetValue(viewMatrix);
effect.Parameters["xProjection"].SetValue(projectionMatrix);
effect.Parameters["xWorld"].SetValue(worldMatrix);
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3, VertexPositionColor.VertexDeclaration);
}
在draw方法中我做了:
worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left,angleRightLeft);
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angleRightLeft);
因此,如果我按下 A(Left) 键,它会向左移动,但单击 D 会向右转。这是密钥处理方法:
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))
//cameraPosition += new Vector3(0.0f, 50.0f, +100);
if (keyState.IsKeyDown(Keys.Down) || keyState.IsKeyDown(Keys.S))
//cameraPosition -= new Vector3(0.0f, 50.0f, +100);
if (keyState.IsKeyDown(Keys.Right) || keyState.IsKeyDown(Keys.D))
//cameraPosition += new Vector3(1, 0, 0);
angleRightLeft += 0.05f;
if (keyState.IsKeyDown(Keys.Left) || keyState.IsKeyDown(Keys.A))
//cameraPosition += new Vector3(-1, 0, 0);
angleRightLeft -= 0.05f;
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 (WasDoubleClick())
{
changeScreenNode(this.graphics, 1920, 1080, true);
}
}
if (WasMouseLeftClick())
{
previousClick = DateTime.Now;
}
}
如果我移动这条线:angleRightLeft += 0.05f; 从 D 键将它放在 W 键下,然后当我按 A 时它向左移动,W 向右移动。
但是我想这样做,当我单击 W 和 S 时,它会上下移动世界,如果我单击 A 和 D,它将左右移动世界。所以我创建了变量angleUpDown,但是旋转世界的Draw方法出了点问题。
我怎样才能根据按下的键来工作?