0

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

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

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

4

1 回答 1

3

因此,从我从您的问题中收集到的信息是,按下该按键WangleRightLeft += 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;
}
于 2013-10-21T13:11:36.557 回答