3D-XNA Intro Exercise

First Program

  1. Open Visual Studio 2010
  2. Select File/New Project...
  3. Select the "Windows Game" template, to create a new XNA project
  4. Give the project the name "XNA_3D_INTRO"
  5. OK

Add some content

 

  1. This is a picture of a spaceship model and texture (stolen from the spacewar demo).
  2. Download the model and texture to your project content directory (XNA_3D_INTROContent). In solution explorer, select the content folder, right click and "Add/Existing Item...", choose "Content Pipeline Files" in the file type area. Select the Model and Texture files.
  3. Create the following class wide variables.
  4.         
    
      
     
    
    		Model ship;
            Matrix view, world, proj;
      
  5. To load this model in your application, at the following line to LoadContent(...). Note that we don't give the filename extension.
  6. ship=Content.Load<Model>("p1_wedge");
  7. Add this code to Draw(...) (after GraphicsDevice.Clear()), this will draw the model.
  8. 			  view = Matrix.CreateLookAt(new Vector3(0, 10, 5000), Vector3.Zero, Vector3.Up);
                proj = Matrix.CreatePerspectiveFieldOfView((float)Math.PI / 4.0f, 
                    (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height,
                    0.1f, 
                    100000);
                world = Matrix.CreateTranslation(Vector3.Zero);
    
    
                foreach (ModelMesh mesh in ship.Meshes)
                {
                    foreach (BasicEffect effect in mesh.Effects)
                    {
                        //Effect Settings Goes here
                        effect.LightingEnabled = false;
                        effect.World = world;
                        effect.Projection = proj;
                        effect.View = view;
                    }
                    mesh.Draw();
                }
  9. Run the Game to see the object.

Adding Control

The world matrix in the above example is used to control the position of the object on screen. We can modify this matrix to move the object around. Read this page to get an overview of the various math utility classes available in XNA

The keyboard can be interrogated with the following code;

			  // this code is normally found in Update(...)            
			  KeyboardState ks=Keyboard.GetState();


            if (ks.IsKeyDown(Keys.Left)){// check if left key is pressed
					... 
			  }

 

The position and orientation of the model is determined by the world matrix. In this example, world is assigned a translation matrix of zero displacement (this code is taken from the example above).

world = Matrix.CreateTranslation(Vector3.Zero);

 

In this example, world is assigned a translation matrix 5 units along the x-axis

world = Matrix.CreateTranslation(5,0,0);

 

Models can be rotated, this example rotates i radians about the Y axis (use MathHelper to convert from deg to rad);

world = Matrix.CreateRotationY(i);

 

Model transformations can be combined by multiplication. As always, the ORDER of the matrix multiplication has a major impact on the effect.

world = Matrix.CreateTranslation(4,2,1) * Matrix.CreateRotationY(i);


//or equivalently
world = Matrix.CreateTranslation(4,2,1);
world *= Matrix.CreateRotationY(i);

 

  1. Add some code to move the ship left/ right & forward/back using the cursor keys
  2. Add some code to rotate the ship about the x-axis using z/x keys
  3. Add some code to rotate the ship about the y-axis using a/s keys