Beginning XNA 3.0
Game Programming
From Novice to Professional
■■■
Alexandre Santos Lobão,
Bruno Evangelista,
José Antonio Leal de Farias,
and Riemer Grootjans
Beginning XNA 3.0 Game Programming: From Novice to Professional
Copyright © 2009 by Alexandre Santos Lobão, Bruno Evangelista, José Antonio Leal de Farias,
Riemer Grootjans
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13 (pbk): 978-1-4302-1817-3
ISBN-13 (electronic): 978-1-4302-1818-0
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editors: Ewan Buckingham, Joohn Choe
Technical Reviewer: Fabio Claudio Ferracchiati
Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Tony Campbell,
Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie,
Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Ben Renow-Clarke, Dominic Shakeshaft,
Matt Wade, Tom Welsh
Project Manager: Sofia Marchant
Copy Editor: Marilyn Smith
Associate Production Director: Kari Brooks-Copony
Production Editor: Laura Esterman
Senior Compositor: Susan Glinert
Proofreader: Greg Teague
Indexer: Becky Hornyak
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders ny@springer sbm.com, or
visit .
For information on translations, please contact Apress directly at 2855 Telegraph Avenue, Suite 600,
Berkeley, CA 94705. Phone 510-549-5930, fax 510-549-5939, e-mail , or visit http://
www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special
Bulk Sales–eBook Licensing web page at />The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at .
Contents at a Glance
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
■CHAPTER 1
Game Planning and Programming Basics . . . . . . . . . . . . . . . . . . . . . . 1
■CHAPTER 2
2D Graphics, Audio, and Input Basics . . . . . . . . . . . . . . . . . . . . . . . . . 15
■CHAPTER 3
Creating Your First 2D Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
■CHAPTER 4
Improving Your First 2D Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
■CHAPTER 5
Basics of Game Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
■CHAPTER 6
Rock Rain Live! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
■CHAPTER 7
Rock Rain Zune . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
■CHAPTER 8
3D Game Programming Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
■CHAPTER 9
Rendering Pipeline, Shaders, and Effects . . . . . . . . . . . . . . . . . . . . 227
■CHAPTER 10
Lights, Camera, Transformations! . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
■CHAPTER 11
Generating a Terrain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
■CHAPTER 12
Skeletal Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
■CHAPTER 13
Creating a Third-Person Shooter Game . . . . . . . . . . . . . . . . . . . . . . 337
■CHAPTER 14
Closing Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
iii
Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
■CHAPTER 1
Game Planning and Programming Basics . . . . . . . . . . . . . . . . . 1
Planning the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Target Market . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Game Genre. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
The Game Team . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Game Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
XNA Game Programming Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
General Game Structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Game Initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Game Finalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Game Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
■CHAPTER 2
2D Graphics, Audio, and Input Basics
. . . . . . . . . . . . . . . . . . . . 15
2D Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Common Gaming Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2D and Screen Coordinate Systems . . . . . . . . . . . . . . . . . . . . . . . . . 16
Drawing a Sprite Using XNA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Moving the Sprite on the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Coding for Collision Detection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Game Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Using the Xbox 360 Gamepad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Using the Keyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Using the Mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Game Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Creating Audio Content with XACT . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Using Audio in Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
v
■I N D E X
user input
vertices
keyboard, 31
3-D games, 201–205
mouse, 31
coding for 3-D axis, 211–215
overview of, 29
mesh, 266, 324–325
Xbox 360 gamepad, 30–31
moving triangle up Y axis and, 206
using statement, creating object in, 8
normalizing weights of, 325
■V
representing as matrices, 207
varying input data, 231
terrains
generating position and texture
coordinate, 269–272
Vector2 class, 18
Vector3 class
normal vector, 272–273
Cross method, 243, 245
Lerp method, 249
Vector3 data type
methods of, 205
shortcuts of, 206
vertices and, 201
vectors
tangent and binormal vectors, 273–275
vibration effect, adding to Rock Rain, 56–57
view matrix, 209, 243–244
viewport, aspect ratio and, 209
Visual C# Express Edition, downloading, 5
■W
3-D games, 205–206
walking character, animating, 299
camera coordinate system, 244–246
Wander method (Enemy class), 376–377
velocity property (clsSprite class), 24–25
Wandering state (Enemy class), 375–376
vertex buffer, coding for 3-D axis, 211–215
wave bank operations (XACT), 33
vertex grid, 6x6, created over x,z plane, 261
WaveBank object, 36
vertex processing
web sites
AnimatedModel class, 331–333
Apress, 396
for normal mapping, 290
Augusto, Carlos, 366
vertex shaders
Entertainment Software Association, 2
See also techniques
image editors, 19
description of, 228
Microsoft Developer Network
semantics, 231
for terrain effect, 280–281
Platformer Starter Kit example, 16
Psionic, 372
vertex structures, predefined, 266
recommended, 396–397
VertexBuffer for terrain mesh, 267
Sharp Games community sites, 396
VertexPositionNormalTangentBinormalText
ure, defining, 266–267
SpriteLIB GPL, 43
Xbox LIVE Community Games, 126
XNA Creators Club, 5, 397
421
422
■I N D E X
weights of vertex, normalizing, 325
XNA Game Studio, downloading, 5
window size, changing, 23
world matrix, 209
XNA Game Studio Device Center with Zune
added, 196
World property (BasicEffect class), 222
■Z
world transformation, 258
Zune platform
writing custom user data
(AnimatedModelProcessor class),
316–318
deploying game on, 196–198
■X
modifying classes for Rock Rain
version for
X (DirectX File) format, 301
menu changes for Rock Rain version
for, 187
XACT (Microsoft Cross-Platform Audio
Creation Tool), 32–34
ActionScene class, 195–196
Xbox 360
HelpScene class, 186–187
Game1 class, 193–195
gamepad, user input from, 30–31
Meteor class, 188–190
modifying and deploying Rock Rain to,
57–59
MeteorsManager class, 192
skeletal animation and, 307
PowerSource class, 187–188
Xbox LIVE (Microsoft)
Player class, 190–192
StartScene class, 192–193
Community Games, 126
organizing Rock Rain version for, 184–185
profile, creating, 127–131
planning Rock Rain version for, 183–184
XNA 3.0 Game Programming Recipes
(Grootjans), 396
XNA Creators Club
Billboards sample, 16
web site, 5, 397