Xna tutorial for rotating a 3D model

Written by kevin walker
  • Share
  • Tweet
  • Share
  • Pin
  • Email
Xna tutorial for rotating a 3D model
(Christopher Robbins/Photodisc/Getty Images)

Microsoft's XNA Studio is a powerful and relatively easy to program suite designed to provide one interface for creating games for Windows PCs, Xbox consoles and even Windows 7 phones. Using this program, in conjunction with the free version of Microsoft Visual Studio, both 2D and 3D games can be created. A good way to get started with XNA 3D game programming is to load and rotate a 3D model.

Skill level:
Easy

Other People Are Reading

Instructions

  1. 1

    Create a new project in Microsoft XNA Studio by clicking "File" and "New Project" and selecting "Windows Game."

  2. 2

    Right-click the "Content" folder in the "Solutions Explorer" and choose "Add" and "New Folder." Name it "Models."

  3. 3

    Repeat step 2, but this time name the folder "Textures."

  4. 4

    Right-click each folder you just created and select "Add Existing Item." Add an existing 3D model and its texture to the appropriate folders. If you do not have a 3D model, you can download one from Microsoft's website (see Resources).

  5. 5

    Paste the following new variables within the source code, just below the "public class Game" line:

      float aspectRatio;
    
        Model my Model;
    
        Vector3 model Position = Vector3.Zero;
    
        float model Rotation = 0.0f;
    
        Vector3 camera Position = new Vector3(0.0f, 50.0f, 5000.0f);
    
  6. 6

    Paste the following code into the "LoadContent" function that was created for you when you created your project:

            my Model = Content.Load<Model>("Models\\p1_wedge.fbx");
    
    
    
            aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
    

    This uses the free sample model provided by Microsoft "p1_wedge.fbx."

  7. 7

    Paste the following code from Microsoft within the "Draw" function:

           graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
    
    
    
            // Copy any parent transforms.
    
            Matrix[] transforms = new Matrix[myModel.Bones.Count];
    
            myModel.CopyAbsoluteBoneTransformsTo(transforms);
    
    
    
            // Draw the model. A model can have multiple meshes, so loop.
    
            foreach (ModelMesh mesh in myModel.Meshes)
    
            {
    
                // This is where the mesh orientation is set, as well
    
                // as our camera and projection.
    
                foreach (BasicEffect effect in mesh.Effects)
    
                {
    
                    effect.EnableDefaultLighting();
    
                    effect.World = transforms[mesh.ParentBone.Index] *
    
                        Matrix.CreateRotationY(modelRotation)
    
                        * Matrix.CreateTranslation(modelPosition);
    
                    effect.View = Matrix.CreateLookAt(cameraPosition,
    
                        Vector3.Zero, Vector3.Up);
    
                    effect.Projection = Matrix.CreatePerspectiveFieldOfView(
    
                        MathHelper.ToRadians(45.0f), aspectRatio,
    
                        1.0f, 10000.0f);
    
                }
    
                // Draw the mesh, using the effects set above.
    
                mesh.Draw();
    
            }
    
            base.Draw(gameTime);
    
  8. 8

    Paste the following code from Microsoft in the Update function:

            // Allows the game to exit
    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==
    
                ButtonState.Pressed)
    
                this.Exit();
    
    
    
            model Rotation += (float)gameTime.ElapsedGameTime.TotalMilliseconds *
    
                MathHelper.ToRadians(0.1f);
    
    
    
            base.Update(gameTime);
    

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.