如何使用W A S D键向上和向下左右转动相机?

时间:2013-10-21 12:57:49

标签: c# xna xna-4.0

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

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

在构造函数中我做了:

rotation = Matrix.Identity;

在Update方法中,我调用keyes:

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

在draw方法中,使用变量angleRightLeft来旋转世界(terrain):

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

在抽奖方法中,我做了:

worldMatrix = Matrix.CreateFromAxisAngle(this.rotation.Left,angleRightLeft);
worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angleRightLeft);

因此,如果我按下A(左)键,它向左移动但点击D dosent则向右转动。 这是密钥处理方法:

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方法出了问题。

如何根据按键操作?

1 个答案:

答案 0 :(得分:3)

因此,从我的问题中收集到的是,按下W按下angleRightLeft += 0.05f;按下该按键可以向右移动,但按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 <时,该语句后面的行将被视为要执行的行/强>

因为你已经注释掉了这些行,所以我假设你的编译器看到下一个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;
}