www.it-ebooks.info XNA 4 3D Game Development by Example Beginner's Guide Create acon-packed 3D games with the Microso XNA Framework Kurt Jaegers BIRMINGHAM - MUMBAI www.it-ebooks.info XNA 4 3D Game Development by Example Beginner's Guide Copyright © 2012 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmied in any form or by any means, without the prior wrien permission of the publisher, except in the case of brief quotaons embedded in crical arcles or reviews. Every eort has been made in the preparaon of this book to ensure the accuracy of the informaon presented. However, the informaon contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark informaon about all of the companies and products menoned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this informaon. First published: September 2012 Producon Reference: 1180912 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84968-708-9 www.packtpub.com Cover Image by Sandeep Babu ( sandyjb@gmail.com ) www.it-ebooks.info Credits Author Kurt Jaegers Reviewers Kenneth Dahl Pedersen Michael Schuld Newton Sheikh Pedro Daniel Güida Vázquez Acquision Editor Dilip Venkatesh Lead Technical Editor Ankita Shashi Technical Editors Prashant Salvi Ankita Shashi Copy Editor Alda Paiva Project Coordinator Joel Goveya Proofreaders Aaron Nash Chris Smith Indexer Tejal Soni Graphics Adi Gajjar Producon Coordinator Arvindkumar Gupta Cover Work Arvindkumar Gupta www.it-ebooks.info About the Author Kurt Jaegers is an Oracle Database Administrator and Windows Network Administrator, as well as a long-me hobbyist game developer. He has built games for everything from the Commodore 64 to the Xbox 360. He is the owner of xnaresources.com , and the author of XNA 4.0 Game Development by Example: Beginner's Guide (C# edion) and XNA 4.0 Game Development by Example: Beginner's Guide – Visual Basic Edion, both of which were published by Packt Publishing. As always, I would like to thank my wife Linda for proofreading my original dras. I would also like to thank the visitors to my website and the readers of my rst two XNA books for their support, encouragement, and thought provoking quesons. Finally, I would like to thank the team at Packt Publishing. This is my third book with them, and the experience has always been wonderful. www.it-ebooks.info About the Reviewers Kenneth Dahl Pedersen now aged 35, has been programming since he got his rst computer at age 6, starng on the Commodore 64 with small programs that could do next to nothing. It quickly evolved when he migrated to the much more powerful Amiga 500, developing some demos and light applicaons, and nally culminated when he got his rst PC. Since then, game development has held his interest in a vice grip and Kenneth has since then studied numerous programming languages and APIs for that purpose, his repertoire includes C/C++, OpenGL, DirectX, C#, WPF, WCF, MDX, and XNA. With the appearance of readily available high-end engines, such as Unreal Development Kit, Kenneth had found another interest to keep him well sated in his thirst for game development knowledge. UnrealScript provided another mountain top to climb. Kenneth has an educaon as a Systems Analyst and Developer. Originally from Denmark, where he sll works, he now lives in Sweden with his wife and baby daughter. Other than his wife and daughter, he uses his spare me for game development, seeking new knowledge, and dabbling in 3D visual arts in applicaons such as 3D Studio Max and ZBrush. And of course, Kenneth is an avid gamer – aer all, you cannot make a game if you don't enjoy playing them! First of all, I would like to thank my wife, Nina, for the paence she's shown while I was doing this review—I have probably not been as helpful around the house as I should have been while this has been going on. My beauful daughter, Nadia, for always giving me a reason to smile and laugh. www.it-ebooks.info Michael Schuld started his foray into game development using Managed DirectX v9; aer playing with the framework for a few months, he decided there wasn't enough beginner content out in the world to help those people who are new to game development. To x this problem, he immediately set out wring a tutorial series that he kept up to date with the change from Managed DirectX to XNA and all the updates to the XNA Framework since then. Along with these tutorials, he has hosted a popular XNA Game Development forum and has helped hundreds of programmers new to game development get their feet wet. The site and tutorials have been listed by Microso and GameInformer as one of a select list of community resources for anyone wanng to learn the XNA Framework. More recently, he has expanded his work into DirectX v11 and reviewing books in the game development arena. His recent work, tutorials, and reviews can all be found on http://www.thehazymind.com . I would like to thank David Bonner, Charles Humphrey, and Michael Quandt for their early interest and assistance with my tutorial series, both in reviewing the content for ease of use and helping out with the forums. I'm glad to have you guys around to keep things from geng too crazy. Newton Sheikh , is a soware engineer working on Cloud applicaons for Windows Azure plaorm. Newton has been working with .NET technologies for the last 4 years and loves programming both in C# and VB. Newton's hobbies include web development and web designing. He is a casual game programmer for Android and Windows. When not programming, Newton loves to hang out with his friends. Newton made a very humble start of his career with a company named Inyxa LLC based in Faridabad, India. Currently Newton is working with Hanu Sowares in Gurgaon, India. www.it-ebooks.info Pedro Daniel Güida Vázquez is an Economist, System Analyst, Professor, and Microso MVP for DirectX and XNA, which are some of the accomplishments obtained throughout his life. Owner of Pulsar Coders, an indie company that develops videogames for many plaorms, he enjoys working daily on everything related to videogame development. His skills cover many areas in the eld, both technical and arsc, and he is always looking for interesng challenges to extend his personal and professional goals. You can nd a comprehensive bio of Pedro at http://www.linkedin.com/in/pedroguida . Pedro has worked on XNA 4.0 Game Development by Example: Beginner's Guide – Visual Basic Edion (Book) and XNA Game Development Video Training Screencast (Video tutorial), both of which are endeavors by Packt Publishing. www.it-ebooks.info www.PacktPub.com Support les, eBooks, discount offers and more You might want to visit www.PacktPub.com for support les and downloads related to your book. Did you know that Packt oers eBook versions of every book published, with PDF and ePub les available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entled to a discount on the eBook copy. Get in touch with us at service@ packtpub.com for more details. At www.PacktPub.com, you can also read a collecon of free technical arcles, sign up for a range of free newsleers and receive exclusive discounts and oers on Packt books and eBooks. http://PacktLib.PacktPub.com Do you need instant soluons to your IT quesons? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's enre library of books. Why Subscribe? Fully searchable across every book published by Packt Copy and paste, print and bookmark content On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine enrely free books. Simply use your login credenals for immediate access. Instant Updates on New Packt Books Get noed! Find out when new books are published by following @PacktEnterprise on Twier, or the Packt Enterprise Facebook page. www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Introducon to XNA 7 System requirements 8 Installing the Windows Phone SDK 8 Time for acon – installing Windows Phone SDK 9 Speller – Our rst XNA game 10 Time for acon – creang an XNA project 11 Managing content 12 Time for acon – creang content assets 13 Member variables 14 Time for acon – declaring new member variables 14 The Game1 constructor 16 Inializaon 17 Time for acon – customizing the Inialize() method 17 Loading content 17 Time for acon – creang a square texture 18 Updang 19 Time for acon – customizing the Update() method 19 The Draw() method 21 Time for acon – drawing Speller 21 Helper methods 23 Time for acon – words and leers 24 Time for acon – compleng the Speller project 27 Summary 30 Chapter 2: Cube Chaser – A Flat 3D World 31 Designing the game 31 3D coordinates 32 www.it-ebooks.info . xnaresources.com , and the author of XNA 4. 0 Game Development by Example: Beginner's Guide (C# edion) and XNA 4. 0 Game Development by Example: Beginner's. www.it-ebooks.info XNA 4 3D Game Development by Example Beginner's Guide Create acon-packed 3D games with the Microso XNA Framework Kurt Jaegers