This excellent volume is unique in that it covers not only the basic techniques of computer graphicsandgamedevelopment,butalsoprovidesathoroughandrigorous—yetveryreadable— treatmentoftheunderlyingmathematics.Fledglinggraphicsandgamesdeveloperswillfindit avaluableintroduction;experienceddeveloperswillfinditaninvaluablereference.Everything ishere,fromthedetailednumericissuesofIEEEfloatingpointnotation,tothecorrectwayto usequaternionsandsphericallinearinterpolationtorepresentorientation,tothemathematics ofcollisiondetectionandrigid-bodydynamics. —DavidLuebke,UniversityofVirginia, co-authorofLevelofDetailfor3DGraphics Whenitcomestosoftwaredevelopmentforgamesorvirtualreality,youcannotescapethemath- ematics.Thebestperformancecomesnotfromsuperfastprocessorsandterabytesofmemory, butfromwell-chosenalgorithms.Withthisinmind,thetechniquesmostusefulfordeveloping production-qualitycomputergraphicsforHollywoodblockbustersarenotthebestchoicefor interactiveapplications.Whenrenderingtimesaremeasuredinmillisecondsratherthanhours, youneedanentirelydifferentperspective. Essential Mathematics for Games and Interactive Applications provides this perspective. Whilethemathematicsarerigorousandperhapschallengingattimes,VanVerthandBishop providethecontextforunderstandingthealgorithmsanddatastructuresneededtobringgames andVRapplicationstolife.ThismaynotbetheonlybookyouwilleverneedforgamesandVR softwaredevelopment,butitwillcertainlyprovideanexcellentframeworkfordevelopingrobust andfastapplications. —IanAshdown,President,ByHeartConsultantsLimited WithEssentialMathematicsforGamesandInteractiveApplications,VanVerthandBishophave providedinvaluableassistanceforprofessionalgamedeveloperslookingtoshoreupweaknesses intheirmathematicaltraining.Evenifyouneverintendtowritearendererortuneaphysics engine,thisbookprovidesthemathematicalandconceptualgroundingneededtounderstand manyofthekeyconceptsinrendering,simulation,andanimation. —DaveWeinstein,Microsoft,RedStormEntertainment Geometry,trigonometry,linearalgebra,andcalculusareallessentialtoolsfor3Dgraphics.Math- ematicscoursesinthesesubjectscovertoomuchground,whileatthesametimeglossingoverthe bread-and-butteressentialsfor3Dgraphicsprogrammers.InEssentialMathematicsforGames andInteractiveApplications,VanVerthandBishopbringjusttherightlevelofmathematicsout ofthetrenchesofprofessionalgamedevelopment.Thisbookprovidesanaccessibleandsolid mathematicalfoundationforinteractivegraphicsprogrammers.Ifyouareworkinginthearea of3Dgames,thisbookisa“musthave.” —JonathanCohen,DepartmentofComputerScience, JohnsHopkinsUniversity, co-authorofLevelofDetailfor3DGraphics It’sthebookwithallthemathyouneedforgames. —NeilKirby,BellLabs As games become ever more sophisticated, mathematics and technical programming skills becomeincreasinglyimportanttohaveinyourtoolbox.EssentialMathprovidesasolidfoun- dationinmanycriticalareas.Youwillfindmanytopicscoveredindetail:fromlinearalgebra to calculus, from physics to rasterization. Some of this will be review material, but you will undoubtedlylearnsomethingnewand,mostimportantly,somethinguseful. —ErinCatto,BlizzardEntertainment Thispageintentionallyleftblank Essential Mathematics for Games and Interactive Applications A Programmer’s Guide Second Edition Thispageintentionallyleftblank Essential Mathematics for Games and Interactive Applications A Programmer’s Guide Second Edition James M. Van Verth Lars M. Bishop AMSTERDAM•BOSTON•HEIDELBERG•LONDON NEWYORK•OXFORD•PARIS•SANDIEGO SANFRANCISCO•SINGAPORE•SYDNEY•TOKYO MorganKaufmannPublishersisanimprintofElsevier AcquisitionsEditor LauraLewin AssistantEditor ChrisSimpson PublishingServicesManager GeorgeMorrison SeniorProductionManager PaulGottehrer CoverDesigner JoanneBlank Composition diacriTech Interiorprinter RRDonnelley Coverprinter PhoenixColor MorganKaufmannPublishersisanimprintofElsevier. 30CorporateDrive,Suite400,Burlington,MA01803,USA (cid:3) Thisbookisprintedonacid-freepaper. ∞ Copyright©2008byElsevierInc.Allrightsreserved. Designationsusedbycompaniestodistinguishtheirproductsareoftenclaimedas trademarksorregisteredtrademarks.InallinstancesinwhichMorganKaufmann Publishersisawareofaclaim,theproductnamesappearininitialcapitalorall capitalletters.Readers,however,shouldcontacttheappropriatecompaniesfor morecompleteinformationregardingtrademarksandregistration. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,or transmittedinanyformorbyanymeans—electronic,mechanical,photocopying, scanning,orotherwise—withoutpriorwrittenpermissionofthepublisher. PermissionsmaybesoughtdirectlyfromElsevier’sScience&TechnologyRights DepartmentinOxford,UK:phone:(+44)1865843830,fax:(+44)1865853333, E-mail:[email protected] viatheElsevierhomepage(http://elsevier.com),byselecting“Support&Contact” then“CopyrightandPermission”andthen“ObtainingPermissions.” LibraryofCongressCataloging-in-PublicationData APPLICATIONSSUBMITTED ISBN:978-0-12-374297-1 ISBN:978-0-12-374298-8(CD-ROM) ForinformationonallMorganKaufmannpublications, visitourWebsiteatwww.mkp.comorwww.books.elsevier.com PrintedinTheUnitedStatesofAmerica 08 09 10 11 12 5 4 3 2 1 Dedications ToMurandFiona,forallowingmetoslaythemonsteronemoretime.—Jim ToJen,whoconstantlyhelpsmegeteverythingdone;andtoNadiaandTasha, whoconstantlyhelpmeavoidgettinganyofitdoneontime.—Lars About the Authors James M. Van Verth is an OpenGL Software Engineer at NVIDIA, where he works on device drivers for NVIDIA GPUs. Prior to that, he was a found- ing member of Red Storm Entertainment, where he was a lead engineer for eightyears.Forthepastnineyearshealsohasbeenaregularspeakeratthe GameDevelopersConference,teachingtheall-daytutorials“MathforGame Programmers” and “Physics for Game Programmers,” on which this book is based. His background includes a B.A. in Math/Computer Science from Dartmouth College, an M.S. in Computer Science from the State University ofNewYorkatBuffalo,andanM.S.inComputerSciencefromtheUniversity ofNorthCarolinaatChapelHill. LarsM.BishopisanengineerintheHandheldDeveloperTechnologiesgroup at NVIDIA. Prior to joining NVIDIA, Lars was the Chief Technology Officer at Numerical Design Limited, leading the development of the Gamebryo3D cross-platform game engine. He received a BS in Math/Computer Science fromBrownUniversityandanMSinComputerSciencefromtheUniversity of North Carolina at Chapel Hill. His outside interests include photography, drumming,andplayingbassguitar. Contents Preface xix Introduction xxiii Chapter 1 Real-World Computer Number Representation 1 1.1 Introduction 1 1.2 RepresentingRealNumbers 2 1.2.1 Approximations 2 1.2.2 PrecisionandError 3 1.3 Floating-PointNumbers 4 1.3.1 Review:ScientificNotation 4 1.3.2 ARestrictedScientificNotation 5 1.4 Binary“ScientificNotation” 6 1.5 IEEE754Floating-PointStandard 9 1.5.1 BasicRepresentation 9 1.5.2 RangeandPrecision 11 1.5.3 ArithmeticOperations 13 1.5.4 SpecialValues 16 1.5.5 VerySmallValues 19 1.5.6 CatastrophicCancelation 22 1.5.7 DoublePrecision 24 1.6 Real-WorldFloating-Point 25 1.6.1 InternalFPUPrecision 25 1.6.2 Performance 26 1.6.3 IEEESpecificationCompliance 29 1.6.4 GraphicsProcessingUnitsandHalf-Precision Floating-PointFormats 31 1.7 Code 32 1.8 ChapterSummary 33 ix