XNA for C#
DirectX 9 for C#
DirectX 9 for C++
DirectX 9 for VB
Forum
   July 19: Series4 - 2 Pass Renderstates
My Book: Out Now!
      
       Go to section on this site

Additional info


Latest Forum posts

 WinForm not found
  Posted by: riemer
  When: 20/07/2008 at 14:30:03

 Bounding Boxes for Maya Data
  Posted by: riemer
  When: 20/07/2008 at 14:27:31

 QuadTree
  Posted by: riemer
  When: 20/07/2008 at 14:23:48

 Bounding Boxes for Maya Data
  Posted by: vijay
  When: 20/07/2008 at 12:25:40

 Error on compiling
  Posted by: libia
  When: 20/07/2008 at 02:36:16

 Terrain From RAW
  Posted by: reiko
  When: 19/07/2008 at 20:36:39

 WinForm not found
  Posted by: kapil.rajak
  When: 19/07/2008 at 16:24:52

 human skin colors
  Posted by: aguacate
  When: 19/07/2008 at 13:34:55

 Bounding Boxes for Maya Data
  Posted by: Archenon
  When: 19/07/2008 at 09:19:16

 Volunteer to write VB.net tutorial
  Posted by: riemer
  When: 19/07/2008 at 07:00:19


Ads

Rotate your terrain using the keyboard

Using XNA, it is very easy to read in the current state of your keyboard. The correct libraries, Microsoft.Xna.Framework.Input, are already linked to by default when you’ve started your XNA project, so we can immediately move on to the code that reads in the keyboard input.

Add this bit of code to your Update method, which is called 60 times per second:

 KeyboardState keyState = Keyboard.GetState();
 if (keyState.IsKeyDown(Keys.Delete))
     angle += 0.05f;
 if (keyState.IsKeyDown(Keys.PageDown))
     angle -= 0.05f;

Here you put the current state of the keyboard in a variable 'keyState'. Using this variable, you can immediately read out which keys are currently being pressed! When the user presses the Delete or PageDown key, the value of the ‘angle’ variable will be adjusted.

In our Draw method, we need to make sure that this rotation is taken into account by our World matrix:

 Matrix worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angle);

The terrain is rotated around the Y Up axis before it is moved to the (0,0,0) 3D origin.

Et voila! When you run the code, you can rotate your terrain simply by pressing the Delete and PageDown buttons!




DirectX Tutorial 9 - Keyboard

If you appreciate the amount of time I spend creating and updating
these pages, feel free to donate -- any amount is welcome !



Click here to go to the forum on this chapter!

Or click on one of the topics on this chapter to go there:
  • Input with JOYPAD
          Hi, Thanks for your work Would it possible t...
  • Problem with LoaderLock and the keyboard
          I was going through everything fine until I got to...
  • Inline code vs end of page
          Hi Riemer! I love the tutorials. I know website m...



    You can try these exercises to practice what you've learned:
  • Add some code that reads in the Arrow keys. Adjust your world matrix so the terrain is moved into the direction of the Arrow key being pressed.
     The code for our rotating terrain:

     using System;
     using System.Collections.Generic;
     using Microsoft.Xna.Framework;
     using Microsoft.Xna.Framework.Audio;
     using Microsoft.Xna.Framework.Content;
     using Microsoft.Xna.Framework.GamerServices;
     using Microsoft.Xna.Framework.Graphics;
     using Microsoft.Xna.Framework.Input;
     using Microsoft.Xna.Framework.Net;
     using Microsoft.Xna.Framework.Storage;
     
     namespace XNAtutorial
     {
         public class Game1 : Microsoft.Xna.Framework.Game
         {
             GraphicsDeviceManager graphics;
             SpriteBatch spriteBatch;
             GraphicsDevice device;
             Effect effect;
             VertexPositionColor[] vertices;
             VertexDeclaration myVertexDeclaration;
             int[] indices;
     
             Matrix viewMatrix;
             Matrix projectionMatrix;
     
             private int terrainWidth;
             private int terrainHeight;
             private float[,] heightData;
     
             float angle = 0;
     
             public Game1()
             {
                 graphics = new GraphicsDeviceManager(this);
                 Content.RootDirectory = "Content";
             }
     
             protected override void Initialize()
             {
                 graphics.PreferredBackBufferWidth = 500;
                 graphics.PreferredBackBufferHeight = 500;
                 graphics.IsFullScreen = false;
                 graphics.ApplyChanges();
                 Window.Title = "Riemer's XNA Tutorials -- Series 1";
     
                 base.Initialize();
             }
     
             protected override void LoadContent()
             {
                 device = graphics.GraphicsDevice;
                 spriteBatch = new SpriteBatch(GraphicsDevice);

                effect = Content.Load<Effect> ("effects");

                Texture2D heightMap = Content.Load<Texture2D> ("heightmap");            LoadHeightData(heightMap);

                SetUpVertices();
                SetUpCamera();
                SetUpIndices();
            }

            private void SetUpVertices()
            {
                vertices = new VertexPositionColor[terrainWidth * terrainHeight];
                for (int x = 0; x < terrainWidth; x++)
                {
                    for (int y = 0; y < terrainHeight; y++)
                    {
                        vertices[x + y * terrainWidth].Position = new Vector3(x, heightData[x, y], -y);
                        vertices[x + y * terrainWidth].Color = Color.White;
                    }
                }

                myVertexDeclaration = new VertexDeclaration(device, VertexPositionColor.VertexElements);
            }

            private void SetUpIndices()
            {
                indices = new int[(terrainWidth - 1) * (terrainHeight - 1) * 6];
                int counter = 0;
                for (int y = 0; y < terrainHeight - 1; y++)
                {
                    for (int x = 0; x < terrainWidth - 1; x++)
                    {
                        int lowerLeft = x + y*terrainWidth;
                        int lowerRight = (x + 1) + y*terrainWidth;
                        int topLeft = x + (y + 1) * terrainWidth;
                        int topRight = (x + 1) + (y + 1) * terrainWidth;
            
                        indices[counter++] = topLeft;
                        indices[counter++] = lowerRight;
                        indices[counter++] = lowerLeft;
            
                        indices[counter++] = topLeft;
                        indices[counter++] = topRight;
                        indices[counter++] = lowerRight;
                    }
                }
            }

            private void SetUpCamera()
            {
                viewMatrix = Matrix.CreateLookAt(new Vector3(60, 80, -80), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
                projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, device.Viewport.AspectRatio, 1.0f, 300.0f);
            }

            private void LoadHeightData(Texture2D heightMap)
            {
                terrainWidth = heightMap.Width;
                terrainHeight = heightMap.Height;

                Color[] heightMapColors = new Color[terrainWidth * terrainHeight];
                heightMap.GetData(heightMapColors);

                heightData = new float[terrainWidth, terrainHeight];
                for (int x = 0; x < terrainWidth; x++)
                    for (int y = 0; y < terrainHeight; y++)
                        heightData[x, y] = heightMapColors[x + y * terrainWidth].R / 5.0f;
            }

            protected override void UnloadContent()
            {
            }

            protected override void Update(GameTime gameTime)
            {
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                    this.Exit();


                 KeyboardState keyState = Keyboard.GetState();
                 if (keyState.IsKeyDown(Keys.Delete))
                     angle += 0.05f;
                 if (keyState.IsKeyDown(Keys.PageDown))
                     angle -= 0.05f;
     
                 base.Update(gameTime);
             }
     
             protected override void Draw(GameTime gameTime)
             {
                 device.Clear(Color.Black);
                 device.RenderState.CullMode = CullMode.None;
                 device.RenderState.FillMode = FillMode.WireFrame;
     
                 Matrix worldMatrix = Matrix.CreateTranslation(-terrainWidth / 2.0f, 0, terrainHeight / 2.0f) * Matrix.CreateRotationY(angle);
                 effect.CurrentTechnique = effect.Techniques["Colored"];
                 effect.Parameters["xView"].SetValue(viewMatrix);
                 effect.Parameters["xProjection"].SetValue(projectionMatrix);
                 effect.Parameters["xWorld"].SetValue(worldMatrix);
     
                 effect.Begin();
                 foreach (EffectPass pass in effect.CurrentTechnique.Passes)
                 {
                     pass.Begin();
     
                     device.VertexDeclaration = myVertexDeclaration;
                     device.DrawUserIndexedPrimitives(PrimitiveType.TriangleList, vertices, 0, vertices.Length, indices, 0, indices.Length / 3);
     
                     pass.End();
                 }
                 effect.End();
     
                 base.Draw(gameTime);
             }
         }
     }
     


    Google
     
    Webwww.riemers.net


    If you appreciate the amount of time I spend creating and updating
    these pages, feel free to donate -- any amount is welcome !



    - Website design & XNA + DirectX code : Riemer Grootjans -
    ©2003 - 2008 Riemer Grootjans
  • Translations

    This site in English
    This site in Korean
    This site in Czech

    Microsoft MVP Award



    2007 - 2008 MVP Award
    DirectX - XNA

    Contents

    News
    Home
    Forum
    XNA 2.0 Recipes Book (8)
    Downloads
    Extra Reading (3)
    Matrices: geometrical
    Matrix Mathematics
    Homogenous matrices
    Tutorials (136)
    XNA 2.0 using C# (65)
    Series 1: Terrain (13)
    Starting a project
    The effect file
    The first triangle
    World space
    Rotation - translation
    Indices
    Terrain basics
    Terrain from file
    Keyboard
    Adding colors
    Lighting basics
    Terrain lighting
    VertexBuffer & IndexBuffer
    Series 2:Flightsim (14)
    Series 3: HLSL (18)
    Series 4: Advanced terrain (17)
    Short Tuts (3)
    DirectX using C# (54)
    DirectX using C++ (15)
    DirectX using VB (2)
    -- Expand all --


    Thank you!

    Support this site --
    any amount is welcome !

    Stay up-to-date

    I don't have the time to keep a News section, so stay informed about the updates by clicking on this RSS file!