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-clcking your project in Solution Explorer and selecting Add → Class.... 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've 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 ...

Get Learning XNA 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.