Skip to Content
Learning XNA 3.0
book

Learning XNA 3.0

by Aaron Reed
November 2008
Beginner
510 pages
16h 24m
English
O'Reilly Media, Inc.
Content preview from Learning XNA 3.0

Creating a 3D Camera

OK, let's put together some code. When dealing with cameras, it often makes sense to create a game component for your camera. This makes it really easy to add cameras to new games (just add the component to the project and then add it in code to the game) and to update the camera and move it around. Create a new game component in your 3D project by right-clicking the project name and selecting Add → New Item.... In the templates list on the right side of the Add New Item screen, select Game Component. Name the component Camera.cs, and click Add (see Figure 9-4).

In the Camera.cs class, add two class-level variables (and appropriate auto-implemented properties) to represent your camera's view and projection matrices:

public Matrix view {get; protected set;}
public Matrix projection { get; protected set; }

Then, change the constructor to accept three Vector3 variables representing the initial position, target, and up vectors for the camera. Also, in your constructor, initialize the view by calling Matrix.CreateLookAt and the projection by calling Matrix.CreatePerspectiveFeldOfView. Here's the complete code for the constructor of the Camera class:

public Camera(Game game, Vector3 pos, Vector3 target, Vector3 up)
    : base(game)
{
    view = Matrix.CreateLookAt(pos, target, up);

    projection = Matrix.CreatePerspectiveFieldOfView(
        MathHelper.PiOver4,
        (float)Game.Window.ClientBounds.Width /
        (float)Game.Window.ClientBounds.Height,
        1, 100);
}

Figure 9-4. Creating a camera game component ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning XNA 4.0

Learning XNA 4.0

Aaron Reed

Publisher Resources

ISBN: 9780596154905Errata Page