O'Reilly logo

Learning XNA 4.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Drawing a Model Using a BasicModel Class

Following the design in the 2D game from previous chapters, you’ll want to create a base class for all your models. Add a new class by right-clicking your project in Solution Explorer and selecting AddClass…. Name the class BasicModel.cs, as shown in Figure 10-3.

Adding a BasicModel class

Figure 10-3. Adding a BasicModel class

Let’s flesh out the BasicModel class. First, you’ll need to add these namespaces:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

Next, add two class-level variables to the class:

public Model model { get; protected set; }
protected Matrix world = Matrix.Identity;

The first variable is of type Model. The Model class is used to represent 3D models in memory, much like you used the Texture2D class previously to represent 2D images in memory. We’ll go into more depth on what a Model comprises in a few moments.

The next variable is the Matrix representing the world for this particular model. This matrix represents where to draw the model and how to rotate it, scale it, and so on. It should be fairly familiar if you’ve read the previous chapter.

Next, add a constructor for the BasicModel class. All this constructor needs to do is receive a parameter of type Model and set the model member to that value:

public BasicModel(Model m)
{
    model = m;
}

Now, create an empty virtual Update method that can be overridden by classes that derive from ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required