Tải bản đầy đủ (.pdf) (649 trang)

Mathematics for game developers

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (14.82 MB, 649 trang )

SVP, Thomson Course Technology
PTR: Andy Shafran
Publisher:
Stacy L. Hiquet
Senior Marketing Manager:
Sarah O’Donnell
Marketing Manager:
Heather Hurley
Manager of Editorial Services:
Heather Talbot
Acquisitions Editor:
Mitzi Koontz
Senior Editor:
Mark Garvey
Associate Marketing Managers:
Kristin Eisenzopf and Sarah Dubois
Project Editor:
Kate Shoup Welsh
Technical Reviewers:
Kelly Dempski and Dave Astle
Thomson Course Technology PTR
Market Coordinator:
Amanda Weaver
Copy Editors:
Kate Shoup Welsh and Sean Medlock
Interior Layout Tech:
Susan Honeywell
Cover Designer:
Mike Tanamachi


CD-ROM Producer:
Brandon Penticuff
Indexer:
Sharon Shock
Proofreader:
Sean Medlock
© 2004 by Thomson Course Technology PTR. All rights reserved. No
part of this book may be reproduced or transmitted in any form or by
any means, electronic or mechanical, including photocopying, record-
ing, or by any information storage or retrieval system without written
permission from Thomson Course Technology PTR, except for the
inclusion of brief quotations in a review.
The Premier Press and Thomson Course Technology PTR logo and
related trade dress are trademarks of Thomson Course Technology PTR
and may not be used without written permission.
Maple 9.5 is a registered trademark of Maplesoft. All other trademarks
are the property of their respective owners.
Important: Thomson Course Technology PTR cannot provide software
support. Please contact the appropriate software manufacturer’s techni-
cal support line or Web site for assistance.
Thomson Course Technology PTR and the author have attempted
throughout this book to distinguish proprietary trademarks from
descriptive terms by following the capitalization style used by the
manufacturer.
Information contained in this book has been obtained by Thomson
Course Technology PTR from sources believed to be reliable. However,
because of the possibility of human or mechanical error by our sources,
Thomson Course Technology PTR, or others, the Publisher does not
guarantee the accuracy, adequacy, or completeness of any information
and is not responsible for any errors or omissions or the results obtained

from use of such information. Readers should be particularly aware of
the fact that the Internet is an ever-changing entity. Some facts may have
changed since this book went to press.
Educational facilities, companies, and organizations interested in multi-
ple copies or licensing of this book should contact the publisher for
quantity discount information. Training manuals, CD-ROMs, and por-
tions of this book are also available individually or can be tailored for
specific needs.
ISBN: 1-59200-038-X
Library of Congress Catalog Card Number: 2003105366
Printed in the United States of America
04 05 06 07 08 BH 10 9 8 7 6 5 4 3 2 1
Thomson Course Technology PTR, a division of
Thomson Course Technology
25 Thomson Place
Boston, MA 02210

To the only things that truly matter:
family and friends
S
o many people were involved in bringing this book to life, thanking each person
individually would be a book in itself. Instead of subjecting you to that, I’ll break the
deserving into groups. From the early-on mathematicians to the old-school demo
programmer gurus who taught me the secrets of the PC, I thank you all for the invaluable
technical knowledge. Special thanks to the University of Ottawa professors who could
actually teach, a rarity in an educator (especially in the engineering field). Thanks to my
personal stress relievers (that is, the Quake3 knobs on XO server), which were like caffeine
when the hours were late. Special thanks to everyone who shepherded this book through
production; I think the end result speaks for itself. Finally, I would like to thank every sin-
gle reader out there, without whom none of this would be possible. After you’ve mastered

the material here, I call on you to create some kick-ass demo to make us all proud!
iv
Acknowledgments
CHRISTOPHER T
REMBLAY
lives in the California Bay Area, where he works for Motorola
building a 3D graphics engine for cell phones to power next-generation games. He holds
a degree in Software Engineering from the University of Ottawa, Canada, and is currently
completing final courses for a mathematics degree. His work in the game industry
includes game AI, core-networking, software rendering algorithms, 3D geometry algo-
rithms, and optimization. Although most of his work is PC-based, a fair amount of it was
done on embedded devices ranging from bottom-line TI-calculators to Z80 and 68K Palm
processors to speedy PocketPC strong-arm processors, with games such as LemmingZ.
About the Series Editor
ANDRÉ LAMOTHE, CEO, Xtreme Games LLC, has been involved in the computing indus-
try for more than 25 years. He wrote his first game for the TRS-80 and has been hooked
ever since! His experience includes 2D/3D graphics, AI research at NASA, compiler
design, robotics, virtual reality, and telecommunications. His books are top sellers in the
game programming genre, and his experience is echoed in the Thomson Course Technol-
ogy PTR Game Development series.
v
About the Author
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii
Part I: The Basics 1
Chapter 1 Equation Manipulation and Representation. . . . . . . . . . . . . 3
Choosing a Coordinate System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4
Equation Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
Using Polynomial Roots to Solve Equations . . . . . . . . . . . . . . . . . . . .18
Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25
Chapter 2 Baby Steps: Introduction to Vectors . . . . . . . . . . . . . . . . . . 29

O Vector, What Art Thou? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29
Basic Operations and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . .31
Advanced Operations and Properties . . . . . . . . . . . . . . . . . . . . . . . .36
Vector Spaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .45
Chapter 3 Meet the Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Meat, the Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .54
Basic Operations and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . .55
Advanced Operations and Properties . . . . . . . . . . . . . . . . . . . . . . . .64
Matrix Decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81
Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89
vi
Contents
Diagonalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91
Vector Revision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92
Chapter 4 Basic Geometric Elements . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Creating Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96
Generating Planes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98
Constructing Spheres . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101
When Elements Collide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103
Know Your Distances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .115
3D File Formats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123
Chapter 5 Transformations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
It’s All About the Viewpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .126
Linear Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128
Multiple Linear Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . .138
Projections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .144
Non-Linear Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151
Chapter 6 Moving to Hyperspace Vectors: Quaternions . . . . . . . . . . 153
Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154
Basic Quaternions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .158

Advanced Operations on Quaternions . . . . . . . . . . . . . . . . . . . . . . .161
Part II: Physics 167
Chapter 7 Accelerated Vector Calculus for the Uninitiated . . . . . . . . 169
The Concept of Limits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170
Derivatives (The Result of Differentiation) . . . . . . . . . . . . . . . . . . .175
Reversing: Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .191
Chapter 8 Gravitating Around Basic Physics . . . . . . . . . . . . . . . . . . . 209
Move That Body . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210
Physical Force . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .215
Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .227
Chapter 9 Advanced Physics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Angular Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .236
Contents vii
Oscillations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242
Center of Mass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .257
Angular Forces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .259
Chapter 10 And Then It Hits You: You Need Collision Detection . . . . 263
Environmental Collisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264
Collisions of Objects Among Themselves . . . . . . . . . . . . . . . . . . . . .269
Picking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287
Part III: Playing with Numbers 293
Chapter 11 Educated Guessing with Statistics and Probability. . . . . . 295
Basic Statistics Principles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .296
Random Number Generation (Uniform Deviates) . . . . . . . . . . . . . .302
Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .310
Intelligence Everywhere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .319
Chapter 12 Closing the Gap for Numerical Approximation. . . . . . . . . 323
Solution Approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .324
2D Function Approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .327
Multivariable Function Approximation . . . . . . . . . . . . . . . . . . . . . .358

Chapter 13 Exploring Curvy Bodies . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .363
Surfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .385
Part IV: Rendering and Lighting 389
Chapter 14 Graphics-Generation Engines . . . . . . . . . . . . . . . . . . . . . . 391
Decals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .392
Billboards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .397
Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .402
Chapter 15 Visibility Determination: The World of the Invisible . . . . 405
GPU-Level Visibility Determination . . . . . . . . . . . . . . . . . . . . . . . . .406
CPU Culling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .409
Grid Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .427
Contentsviii
Chapter 16 Space Partitioning: Cleaning Your Room . . . . . . . . . . . . . 427
Quadtrees and Octrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .430
k-D Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .431
Binary Space Partitioned (BSP) Trees . . . . . . . . . . . . . . . . . . . . . . . .437
Portal Subdivision . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .441
Chapter 17 Filling the Gaps with Rendering Techniques. . . . . . . . . . . 447
Studying the 3D Pipeline . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .448
Texture Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .450
Cube Map Tricks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .463
Cel Shading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .466
Shadows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .468
Chapter 18 Building a Light Show . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Studying the Rasterization Pipeline . . . . . . . . . . . . . . . . . . . . . . . . .476
Light a Ray Tracing Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .483
Light Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489
Lighting Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .492
Part V: Optimizations 501

Chapter 19 The Quick Mind: Computational Optimizations . . . . . . . . 503
Fixed Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504
IEEE Floating Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .517
Condition Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524
Chapter 20 Using SIMD to Move Linear Algebra Functions onto
the Highway . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 537
Matrix Storage Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .538
Matrix Transposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .538
Dot Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .540
Vector Length and Normalization . . . . . . . . . . . . . . . . . . . . . . . . . .542
Matrix-Vector Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544
Matrix-Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .547
Matrix Determinant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .551
Cross Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .555
Matrix Division (Inverse) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .556
Contents ix
Chapter 21 Kicking the Turtle: Approximating Common and
Slow Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 563
Transcendental Function Evaluation . . . . . . . . . . . . . . . . . . . . . . . .564
Physical Model Approximation . . . . . . . . . . . . . . . . . . . . . . . . . . . .584
Appendixes 589
Appendix A Notation and Conventions . . . . . . . . . . . . . . . . . . . . . . . .591
Trigonometric Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .593
Appendix B Trigonometry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .593
Symmetry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .594
Pythagorean Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .597
Exponential Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .598
Cosine and Sine Laws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .600
Inverse Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .601
Appendix C Integral and Derivative Tables . . . . . . . . . . . . . . . . . . . . . .605

Appendix D What’s on the CD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .611
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 613
Contentsx
Sometimes it seemed like this day—the day that Mathematics for Game Developers would
be sent to press—would never come. Of the Game Development series, this title has been
one the most challenging books to develop. Not only was it difficult to decide just what
the book should include, but finding the perfect author—one who was knowledgeable in
both mathematics and game development and at the same time a fantastic writer—was
virtually a statistical impossibility! After a lot of searching, however, I did find the perfect
author: Christopher Tremblay. This book exceeds my expectations and I hope it exceeds
yours.
Within Mathematics for Game Developers, not only will you find the entire landscape of
relevant, practical mathematics laid out in such a way that you can understand, you will
also see how it is connected to game programming. The book begins by covering vectors,
matrices, and complex number theory, and then shows how those fields of study can be
applied to real 3D problems. When this framework is in place, the book then covers
physics modeling and collision detection, followed by approximations, statistics, and
probability—which are especially important when you consider that 99 percent of all
computer graphics are based on simplified models or approximations. The core of the
book, on 3D graphics, includes coverage of such topics as 3D graphics algorithms, visibil-
ity, rendering, and lighting techniques and their mathematical descriptions. Finally, the
last chapters discuss mathematical optimizations as well as SIMD technology. (If you
don’t know what that means, then you’d better read this book!)
xi
Letter From the
Series Editor
In conclusion, there are a lot of game development math books out there, but none that
are as accessible as this one, that give you as much practical information for real game
development.
Sincerely,

André LaMothe
Series Editor Course PTR Game Development Series
Letter From the Series Editorxii
xiii
M
ath is a fundamental part of any game developer’s programming arsenal. With-
out a strong understanding of math, you can easily waste days solving a prob-
lem in a game that, in reality, is as simple as 1-2-3. If you’re considering
programming a game that is even slightly complicated, you must realize that it is crucial
to first master some basic concepts such as matrices and vectors.
This book is designed specifically for the game developer, not the mathematician. Most
game programmers interested in learning about the math behind their work have two
options:

They can read a “true” math book—that is, a book that is geared for mathemati-
cians. The problem with reading this type of book is that they not only tend to
delve so deeply into each equation, losing you in the process, but they also provide
you with no means to understand the material.

They can read a “plug-and-play” book, which tend to present a glut of equations
without showing how everything fits together.
This book falls somewhere in the middle. It clarifies how mathematical ideas fit together
and apply to game programming, and includes only those proofs that help elucidate use-
ful math concepts. Unlike most math books—including many math books for game pro-
grammers—this book is concerned less with why it works (for example, proving that one
plus one equals two) as with how it works and what that implies.
One way this book simplifies key mathematical concepts is by providing examples.
Another is by leveraging software to help solve algebraic equations. Instead of showing
200 ways to integrate a function or providing pages and pages of integral tables, this book
recognizes that you simply need to understand the idea behind the operation and why the

operation works.
Introduction
xiv
note
Unless otherwise stated, the logic and deduction found throughout the book will stand for real
numbers. Sometimes it will stand for complex numbers or even the more general cases, but over-
all, I won’t bother covering the more general cases.
Beyond teaching you the mathematical concepts you need as a game programmer, this
book aims to teach you to think for yourself, outside the box. In many cases, the best-
known method for solving a problem won’t be the simplest, fastest, or most efficient.
Don’t be afraid to try an unconventional approach; it just might make a dramatic differ-
ence in your game!
What You’ll Find in This Book
In this book, you will find some unique solutions for dealing with real problems you’ll
likely face when programming many types of 3D games. Not only does this book show
you how to solve these problem, it also explains why the solution works, which enables
you to apply that solution to other problems that may crop up. Put another way, this book
doesn’t just show you how to solve problems; it teaches you how to think in order to solve
problems.
The main topics that this book tackles are

Fundamentals of mathematics

Physics simulation

Playing with numbers

Rendering and lighting

Optimization

How This Book Is Organized
The chapters in this book have been divided into five parts. The first part, “The Basics: A
Professional Programmer’s Mathematical Foundation,” teaches you the basic math back-
ground that you will need to fully understand the material that comes later in the book.
In this section, you’ll examine the basic mathematical entities, their relationships, as well
as their corresponding uses in games. In Part II, “Physics Simulations,” you will explore
the grand problem of motion and discover how you can make your world come alive
using physics, collision detection, and a witty mind. The third part, “Playing with Num-
bers,” looks at mathematics from a number-crunching point of view. It tackles various
Introduction
xv
problems such as those seen in statistics for AI purposes, approximation for speedier
functions, and interpolation for an ultra-smooth transition. Part IV, “Rendering and
Lighting,” looks at the rendering pipeline and how things can be modeled in an efficient
manner. It discusses methods for rendering a game world that not only looks true-to-life
but also displays quickly and efficiently. The last part, “Optimizations,” takes a slightly less
mathematical approach, discussing various techniques that can be used to optimize your
code. It covers the use of fixed points for embedded devices, some dandy fast functions for
basic math operations, and some crazy-fast approximations for well-known functions.
Coupled with the book comes a CD that includes a link to the open source GPL’ed library
“SDL,” which is used throughout the source code that accompanies various chapters of
the book. Take your time to look at the source code, modify it, learn from it, and even
innovate with it.
Introduction
This page intentionally left blank
The Basics
Chapter 1
Equation Manipulation and Representation . . . . . . . . . . . . . . . . . . . . . .3
Chapter 2
Baby Steps: Introduction to Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . .29

Chapter 3
Meet the Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53
Chapter 4
Basic Geometric Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .95
Chapter 5
Transformations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125
Chapter 6
Moving to Hyperspace Vectors: Quaternions . . . . . . . . . . . . . . . . . . .153
PART ONE
This page intentionally left blank
3
Equation Manipulation
and Representation
chapter 1
C
ontrary to popular belief, mathematics is not a universal language. Rather, mathe-
matics is based on a strict set of definitions and rules that have been instated and
to which meaning has been given. Indeed, arguably, logic is simply the process of
someone else making you believe that what you know truly makes sense. In reality, a state-
ment such as “1 + 1 = 2” is as “logical” as the statement that a chair is “a chair as we
know it.”
Likewise, mathematics for game programming, which is primarily an algebraic field, is
also based on a set of definitions and rules. I assume that you already have reasonable
knowledge of these algebraic rules; this chapter is meant to both refresh your algebraic
knowledge and, perhaps, extend it a bit. That said, I hope that in addition to teaching you
how to apply this set of definitions and rules to game programming, this book will open
your mind to new ways of thinking about and representing problems. This chapter
assumes that you know trigonometry and that you have taken a look at Appendix A,
“Notation and Conventions,” which enumerates a few interesting identities and refreshes
your memory with regards to the relationships between trigonometric functions and a

unit circle.
This chapter covers the following:

Choosing a coordinate system

Equation representation

Using polynomial roots to solve equations

Substitution
Choosing a Coordinate System
One important thing to consider when writing a game is the coordinate system you
choose to use. As you’ll discover, every coordinate system has its own purpose; that is, each
one is geared toward performing certain tasks (this will become evident as I enumerate a
few of them). So, although an infinite number of coordinate systems exists, a few stand
out for writing games:

Cartesian coordinates

Polar coordinates

Bipolar coordinates

Cylindrical coordinates

Spherical coordinates

Linear coordinates
But wait, what exactly is a coordinate? You can define a coordinate as being a set of n vari-
ables that allows you to fix a geometric object. You should already be familiar with coor-

dinates and also a few of these systems, but chances are that some of them will be new to
you. Admittedly, not all of them are terribly useful for game programming, but I added
them to expose you to a new spatial system, a new way of thinking.
note
All coordinates will be presented in a vector form <a, b, c, > with length smaller or equal to n
corresponding to the space’s dimension.
Cartesian Coordinates
Without a doubt, the Cartesian coordinate system is the most widely known coordinate
system. As shown in Figure 1.1, the Cartesian coordinate system is a rectilinear coordinate
system—that is, a system that is rectangular in nature and thereby possesses what I will
call rectangular coordinates. Each component in the Cartesian coordinate system is
orthogonal. Geometrically speaking, this implies that each axis of the space is perpendic-
ular (90º) to the other axis. Because of its rectangular nature, this system can naturally do
translations by mere addition.
This coordinate system will be your reference point for all the other coordinate systems
discussed here, and all conversions will be done from those other coordinate systems to
this one. Note that as a convention, I will always place the x component on the horizon-
tal axis, the y component on the vertical axis, and the z component on the perpendicular
axis (that is, the axis coming out of the paper toward you). The interval of the components
Chapter 1

Equation Manipulation and Representation4
is [Ϫϱ, ϱ]. For the sake of example, I’ve plotted <1, 2, 3> on a 3D Cartesian system (feel
free to do the same); the results are shown in Figure 1.2.
Choosing a Coordinate System 5
Figure 1.1
A 2D Cartesian coordinate system
Figure 1.2
<1, 2, 3> plotted on a Cartesian system
Interestingly enough, this by no means implies that the Cartesian coordinate system you

use should be such that x is horizontal, y is vertical, and so on. You could easily build a
Cartesian coordinate system where, for example, the z and y axes are inverted. Just make
sure to take this into account when plotting the coordinate. Similarly, you can define
which side of the axis is negative and which one is positive, also called handedness, as illus-
trated in Figure 1.3. Typically, this only involves a change of sign in the depth axis, and this
line is usually only drawn from the origin to the positive side of the axis, thus generating
what looks like a house corner from the perspective of the inside or outside. For example,
in 2D, using a 2D rendering library, the screen is arranged such that the y axis diminishes
when moving up, but the x axis stays the same. This implies that the origin is at the top-
left corner of the screen, instead of being at the bottom-left like a Cartesian coordinate
system would yield. Math books sometimes like to place the 3D y axis as the depth com-
ponent and the z axis as the height, but here I will stick to what the 3D libraries use.
tip
In Maple, you can use the plot([<functions(
x
)>],
x
=
a

b
, <options>) or plot3d([<functions>(
x
,
y
)],
x
=
a


b
,
y
=
c

d
) to plot graphs using Cartesian coordinates.
Polar Coordinates
Thanks to trigonometric primitives, the polar coordinate system is probably the second
best-known coordinate system. As shown in Figure 1.4, the polar coordinate system is a
radial coordinate system—that is, a system that is characterized by its distance relative to
Chapter 1

Equation Manipulation and Representation6
Figure 1.3
Right- and left-hand Cartesian coordinate system
the center of the coordinate system. The polar coordinate system is a 2D coordinate sys-
tem, and has the property of being cyclic in one component. It possesses two components:
<r, ␾>. r is the radial component, and it specifies the distance from the origin; ␾ is the
angular coordinate, and represents the angle from an arbitrarily defined starting point.
Because of its circular nature, the polar coordinate system is very well adapted to rota-
tions, which are performed naturally with an addition to the angular component. The
range for this coordinate system is <[0, 2␲), [0, ϱ]>.
You can easily convert from polar coordinates to Cartesian coordinates with the following
relationship:
Conversely, you can convert from Cartesian coordinates to polar coordinates with the fol-
lowing relationship:
Choosing a Coordinate System 7
Figure 1.4

A polar coordinate system
Let’s plot <1, 2> on a polar coordinate system; Figure 1.5 shows the results.
tip
In Maple, you can append the coords=polar option to the plot function in order to plot graphs using
polar coordinates.
Bipolar Coordinates
In a polar coordinate system, all coordinates are described with an angle and a length. A
bipolar system, on the other hand, is described with two lengths or two angles. Although
the bipolar coordinate system is not a very popular coordinate system for gaming pur-
poses, it does have its uses and makes for a great way to look at things differently. Just as
its name suggests, a bipolar coordinate system is minimally equipped with two centers, the
distance between which can be a. For mathematical simplicity, let a = 2c.
Consider only the bipolar coordinate system, which is described with two lengths, r
1
and
r
2
, as illustrated in Figure 1.6. It may be a little confusing to see how you can pinpoint a
coordinate with two lengths, but it is actually quite simple. The coordinate described by
< r
1
, r
2
> is the point at which the two circles of radius r
1
and r
2
, separated by a, intersect.
As illustrated in Figure 1.6, for any two circles, there should be two intersections (assum-
ing there is an intersection at all). As with the polar coordinate system, the range of the

lengths is [0, ϱ].
Chapter 1

Equation Manipulation and Representation8
Figure 1.5
<1, 2> plotted on a polar system

×