【问题标题】:How do i turn the camera left right up and down using the keys W S A D?如何使用 W S A D 键左右上下转动相机?
【发布时间】:2013-10-21 21:17:25
【问题描述】:

在我的代码顶部我添加了:

Matrix rotation;
private float angleRightLeft = 0f;
private float angleUpDown = 0f;

在我做的构造函数中:

rotation = Matrix.Identity;

在更新方法中,我调用了键:

float timeDifference = (float)gameTime.ElapsedGameTime.TotalMilliseconds / 1000.0f;
ProcessInput(timeDifference);

在draw方法中我使用变量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(左)键,它会向左移动,但单击 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方法出了点问题。

我怎样才能根据按下的键来工作?

【问题讨论】:

  • 你搞定了吗?

标签: c# xna xna-4.0


【解决方案1】:

所以从我从你的问题中得到的信息是,按下 angleRightLeft += 0.05f; 并在该按键下方按下 W 可以向右移动,但按下 D 不能。

我猜想导致这种情况的原因是您拥有以下代码的方式:

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-statement 括起来,那么当 if-statement 等于 true 时,语句后面的行将被视为要执行的行。。 p>

因为您已将这些行注释掉,所以我假设您的编译器将下一个 if-statement 视为要执行的行,然后对以下 if 语句再次执行相同操作。

如果您通过代码进行调试并逐步退出,您很可能会看到这种情况发生。

所以我建议你要么将你的语句括在括号中,要么注释掉整个 if 语句,而不仅仅是其中的代码:

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;
}

【讨论】:

  • 但是您说当您将angleRightLeft += 0.05f 放在W 的检查下时它工作正常,这表明问题出在您的按键逻辑中。
  • 没问题,很高兴帮助:)
猜你喜欢
  • 2015-10-15
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
  • 2021-05-14
  • 1970-01-01
  • 2021-12-06
  • 2013-09-02
  • 1970-01-01
相关资源
最近更新 更多