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

Coding the Camera for the 3D Game

In the previous sections of this chapter, you created a free-flying 3D camera. You’re now going to take that camera and change it to work for the game that you’ll be building throughout the rest of this book. If you want to keep your free-flying camera code, you should make a copy of your project to save the existing code that you have written.

If you download the source code for this chapter, you’ll find the free-flying camera code that you just created in the folder called Flying Camera. The code that will be written and used through the rest of this chapter is located with the source code as well, in the folder called 3D Game.

The game that you’re going to build in the rest of this book will use a stationary camera that can rotate a total of 45° in a pitch and 45° in a yaw. Later, you’ll add some code to send ships flying toward the camera, which you’ll have to shoot down.

Because you won’t be moving your camera and you won’t be rotating in a roll, you can go into the Camera class’s Update method and remove the code that enables that functionality.

To do this, remove the following code (which moves the camera forward and backward and side to side) from the Update method of the Camera class:

// Move forward/backward if (Keyboard.GetState( ).IsKeyDown(Keys.W)) cameraPosition += cameraDirection * speed; if (Keyboard.GetState( ).IsKeyDown(Keys.S)) cameraPosition −= cameraDirection * speed;// Move side to side if (Keyboard.GetState( ).IsKeyDown(Keys.A)) ...

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