Game Programming Algorithms and Techniques This Page Intentionally Left Blank Game Programming Algorithms and Techniques
Total Page:16
File Type:pdf, Size:1020Kb
Game Programming Algorithms and Techniques This page intentionally left blank Game Programming Algorithms and Techniques A Platform-Agnostic Approach Sanjay Madhav Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their Editor-in-Chief products are claimed as trademarks. Where those designations appear in this book, Mark Taub and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. E x e c u t i v e E d i t o r Laura Lewin The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors D e v e l o p m e n t E d i t o r or omissions. No liability is assumed for incidental or consequential damages in Chris Zahn connection with or arising out of the use of the information or programs contained M a n a g i n g E d i t o r herein. Kristy Hart For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and P r o j e c t E d i t o r content particular to your business, training goals, marketing focus, or branding Elaine Wiley interests), please contact our corporate sales department at corpsales@pearsoned. C o p y E d i t o r com or (800) 382-3419. Bart Reed For government sales inquiries, please contact [email protected]. Indexer For questions about sales outside the U.S., please contact international@pearsoned. WordWise Publishing Services com. Proofreader Visit us on the Web: informit.com/aw Jess DeGabriele Library of Congress Control Number: 2013950628 Technical Reviewers Copyright © 2014 Pearson Education, Inc. Alexander Boczar All rights reserved. Printed in the United States of America. This publication is Dustin Darcy protected by copyright, and permission must be obtained from the publisher prior Jeff Wofford to any prohibited reproduction, storage in a retrieval system, or transmission in any Editorial Assistant form or by any means, electronic, mechanical, photocopying, recording, or likewise. Olivia Basegio To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle C o v e r D e s i g n e r River, New Jersey 07458, or you may fax your request to (201) 236-3290. Chuti Prasersith Screenshot from Jetpack Joyride ©2011 Halfbrick Studios. Used with permission. C o m p o s i t o r Screenshot from Jazz Jackrabbit 2 ©1998 Epic Games, Inc. Used with permission. Nonie Ratcliff Screenshot from Quadrilateral Cowboy ©2013 Blendo Games LLC. Used with G r a p h i c s permission. Laura Robbins Screenshot from Skulls of the Shogun ©2013 17-BIT. Used with permission. Screenshot from Unreal Engine 3 ©2005 Epic Games, Inc. Used with permission. Screenshot from Unreal Tournament 3 ©2007 Epic Games, Inc. Used with permission. Screenshots from Microsoft® Visual Studio® 2010 ©2010 Microsoft. Used with permission from Microsoft. Image in Figure 2-10 courtesy of © denis_pc/fotolia. Images in Figures 4-4, 8-5, and 8-6 courtesy of © York/fotolia. ISBN-13: 978-0-321-94015-5 ISBN-10: 0-321-94015-6 Text printed in the United States on recycled paper at R.R. Donnelley in Crawfordsville, IN. First printing: December 2013 To my family for supporting me and to all my students for not driving me crazy (yet). This page intentionally left blank Contents Preface . xv 1 Game Programming Overview . .1 Evolution of Video Game Programming . 2 The Game Loop . 5 Time and Games . 9 Game Objects . 13 Summary . 15 Review Questions. 16 Additional References . 16 2 2D Graphics . 19 2D Rendering Foundations. 20 Sprites . 22 Scrolling. 30 Tile Maps . 35 Summary . 39 Review Questions. 39 Additional References . 39 3 Linear Algebra for Games . 41 Vectors . 42 Matrices . 58 Summary . 62 Review Questions. 62 Additional References . 63 viii CONTENTS 4 3D Graphics . 65 Basics . 66 Coordinate Spaces . 67 Lighting and Shading . 76 Visibility . 85 World Transform, Revisited. 88 Summary . 91 Review Questions. 92 Additional References . 92 5 Input . 93 Input Devices . 94 Event-Based Input Systems. 99 Mobile Input . 105 Summary . 108 Review Questions. 108 Additional References . 109 6 Sound . 111 Basic Sound . .112 3D Sound . .115 Digital Signal Processing . .119 Other Sound Topics . 122 Summary . 124 Review Questions. 125 Additional References . 125 7 Physics . 127 Planes, Rays, and Line Segments . 128 Collision Geometry. 130 Collision Detection . 134 Physics-Based Movement. 148 CONTENTS ix Physics Middleware . 153 Summary . 154 Review Questions. 154 Additional References . 155 8 Cameras . 157 Types of Cameras . 158 Perspective Projections . 161 Camera Implementations. 164 Camera Support Algorithms . 175 Summary . 178 Review Questions. 178 Additional References . 178 9 Artificial Intelligence . 179 “Real” AI versus Game AI . 180 Pathfinding. 180 State-Based Behaviors. 192 Strategy and Planning . 198 Summary . 200 Review Questions. 200 Additional References . 202 10 User Interfaces . 203 Menu Systems . 204 HUD Elements . 207 Other UI Considerations. 217 Summary . 221 Review Questions. 222 Additional References . 222 x CONTENTS 11 Scripting Languages and Data Formats . 223 Scripting Languages . 224 Implementing a Scripting Language. 229 Data Formats . 235 Case Study: UI Mods in World of Warcraft . 239 Summary . 241 Review Questions. 241 Additional References . 242 12 Networked Games. 243 Protocols . 244 Network Topology . 250 Cheating . 255 Summary . 257 Review Questions. 257 Additional References . 258 13 Sample Game: Side-Scroller.