Foundations
of
Gatne Engine Developtnent
VüLUME 1
MATHEMATICS
by Eric Lengyel
Terathon
Software
LLC
Lincoln,
California
Foundations
Volume 1: Mathematics
ofGame Engine Development
ISBN-13:
978-0-9858117-4-7
Copyright
© 2016, by Eric Lengyel
reserved.
All rights
retrieval
information
mechanical,
including
the prior permission
transmitted,
scanning,
owner.
of the copyright
in an
stored
No part ofthis publication may be reproduced,
or
or utilized
in any form, electronic
system,
photocopying,
digitizing,
or recording, without
First printing
Published
www.terathon.com
by Terathon
Software
LLC
Series
website:
foundationsofgameenginedev.com
About the cover:
developed
by the author
using the Tombstone
Engine.
The cover image is a scene from a garue entitled
The 31 st,
Preface
Chapter
1 Vectors
and Matrices
1.1 Vector Fundamentals
1.2 Basic Vector Operations
1.2.1 Magnitude
and Scalar
Multiplication
1.2.2 Addition
and Subtraction
1.3 Matrix Fundamentals
1.4 Basic Matrix
Operations
1.4.1 Addition,
Subtraction,
and Scalar
Multiplication
1.4.2 Matrix Multiplication
1.5 Vector Multiplication
l. 5 .1 Dot Prod uct
1.5.2 Cross Product
1.5.3 Scalar
Triple
Product
1.6 Vector Projection
1.7 Matrix Inversion
Matrices
l. 7 .1 Identity
1.7.2 Determinants
1. 7 .3 Elementaiy
1. 7.4 Inverse
l. 7 .5 In verses
Matrices
Calculation
of Small Matrices
Exercises
for Chapter
1
ix
1
1
3
5
8
11
16
16
16
20
20
24
30
31
36
36
37
42
45
46
51
V
Contents
Chapter
2 Transforms
2.1 Coordinate Spaces
2.1.1 Transformation
2.1.2 Orthogonal
2.1.3 Transform
n
Transforms
Compositio
Matrices
2.2 Rotations
About a Coordinate Axis
About an Arbitrary
Axis
2.2.1 Rotation
2.2.2 Rotation
2.3 Reflections
2.4 Scales
2.5 Skews
2.6 Homogeneous
2. 7 Quaternions
Coordinates
2. 7 .1 Quaternion
2.7.2 Rotations
Fundamentals
With Quaternions
Exercises
for Chapter
2
Chapter
3 Geometry
3.1 Triangle
3.2 Normal Vectors
Meshes
3.2.1 CalculatingNormal
3.2.2 Transforming
Normal Vectors
Vectors
3.3 Lines and Rays
3 .3 .1 Parametric
Lines
3.3.2 Distance
3.3.3 Distance Between
a Point anda Line
Two Lines
Between
3.4 Planes
Planes
Between
3.4.1 Implicit
3.4.2 Distance
3.4.3 Reflection
3.4.4 Intersection
Through
a Plane
a Point anda Plane
of a Line anda Plane
3.4.5 Intersection
3.4.6 Intersection
of Three Planes
of Two Planes
3.4.7 Transforming
Planes
vi
55
55
56
57
58
59
59
62
65
69
71
74
82
82
87
95
97
97
99
99
102
107
107
108
110
112
113
115
117
119
120
122
124
Contents
3.5 Plücker
Coordinates
Lines
3.5.1 Implicit
3.5.2 Homogeneous
Formulas
Lines
3 .5 .3 Transforming
Exercises
for Chapter
3
Chapter
4 Advanced
Algebra
4.1 Grassmann
Algebra
4.1.1 Wedge Product
4.1.2 Bivectors
4.1.3 Trivectors
4.1.4 Algebraic
4.1.5 Complements
4.1.6 Antivectors
4.1.7 Antiwedge
Product
Structure
4.2 Projective
Geometry
4.2.1 Lines
4.2.2 Planes
4.2.3 Join and Meet
4.2.4 Line Crossing
4.2.5 Plane Distance
4.2.6 Summary and lmplementation
4.3 Matrix
4.4 Geometric
Inverses
Algebra
4.4.1 Geometric
Product
4.4.2 Vector Division
4.4.3 Rotors
4.5 Conclusion
Exercises
for Chapter
4
Index
vii
125
126
128
132
134
137
138
138
139
142
143
149
153
155
159
159
160
162
163
165
166
170
173
174
176
178
181
182
185
Preface
of the same topics elsewhere
in the literature.
Along the way, we will
with the usual approach
This book provides
a detailed
game engine programmers.
gebra (vectors
and matrices),
common to many other textbooks
iarity
expositions
make several attempts
and more correct
quarter
concepts
gebra.
intuition
and provide
else.
anywhere
earlier
of these branches
of the book endeavors
mathematical
to foreshadow
Knowledge
discussed
details
introduction
to the mathematics
cover the topics
used by modero
The first three chapters
of linear
al
transforms,
and geometry
in a conventional
on the subject.
This is done to provide
so that it's easy to make connections
to similar
manner
a famil
the discussion
of a manifestly
more elegant
model that appears
to provide
in the fourth chapter.
a deeper understanding
of many of the
Grassmann
al
algebra
also allows
and geometric
us to convey
of mathematics
The last
by introducing
in the first three chapters
that are difficult
to find
One of the goals of this book is to give practical
advice.
This is
short code listings appearing
throughout
the book
engineering
is implemented
inside
real-world
we've discussed
pages with code listings
or functions
referenced
in various
places
re
have in
that are illustratively
been left out. This happens
only when the form and behavior
of the
that de
Chapter
code listings
and operations corresponding
vector,
to a three-dimensional
For example,
1 includes
sorne data structures
through many
how the mathematics
To avoid filling
accomplished
showing
game engines.
dundant,
tentionally
missing
fine a data structure
but it doesn't
used by other code listings
cal. The complete
code is obvious.
show similar
library
code for a four-dimensional
later in the book, because
vector,
it would be largely
even though it's
identi
of code is available
on the website
cited below.
ix
Preface
X
We assurne that the reader
has a so lid understanding
of basic trigonornetry,
a
appears
A bit of calculus
is not essential
of the C++ language,
and sorne farniliarity
nurnbers.
working
knowledge
floating-point
this isolated usage
rnathernatics
sessing
first two chapters.
ter 3 before proceeding
ity with the notation
in Chapter
3, but a full grasp of
to the rest of the book. Otherwise,
all of the
pos
readers
rnay wish to skip rnuch of the
take a look at Chap
because Chapter
4 assurnes
discussed
of linear algebra
that ali readers
in Chapter
a thorough knowledge
we recornrnend
However,
to the final chapter
details
and conceptual
that we cover is built frorn the ground up. Advanced
3.
a familiar
with standard
blue outline.
to rnake it easier
Irnportant
equations
and key results
appearing
This is intended
both to highlight
in the text are boxed with a
and
the rnost valuable
information
Each chapter
to find when using the book as a reference.
concludes
with a set of exercises,
and rnany of those exercises
ask for a short proof of sorne kind. The exercises
tional
a little trickier.
but interesting
provided
educational
value,
To ensure
rnathernatical
cited below.
to provide
addi
others
and while rnany of thern have easy solutions,
are
that getting stuck doesn
of a srnall
revelation,
are designed
to all of the exercises
the answers
't deprive
any reader
are
on the website
This book is the first
volurne in a series
that covers a wide range of topics
to garne engine developrnent.
related
Game Engine Development
series
can be found at the following
of
address:
The official website
for the Foundations
foundationsofgarneenginedev
.corn
contains
This website
announcernents,
errata,
information
code listings,
and answers
to exercises.
about all of the books in the series,
including
Preface
xi
Acknowledgements
volume was made possible
owes a debt of gratitude
The first
to the hundreds
the author
the Foundations
of Game Engine Development
was written. Special thanks
extra support
by making contributions
go to the following
by a successful
of contributors
series
even a single
people in particular
before
that exceed the cost of a copy of the book.
crowdfunding
campaign,
who supported
and
for showing
word
Norbert
Nopper
Patsiouras
Petryk
Andres Hemandez Darren Ranalli
Bergmeier Hauke Hildebrandt Steen Rasmussen
Rodolphe
Martin Hurton Yuri Kunde Schlesner
Frani;ois
Martijn
Jaccard Bill Seitzinger
Joosten Michael D.
Shah
Carre
Castano
Aguado Tonci Jukic
Abreu Trevor
Heldna Mike Ramsey
Green
Guillemot Nikolaos
Gunnarsson Georges
Archard Aaron Gutierrez Ian Prest
Beckebans Timothy
Alexandre
Luis Alvarado Nicolas
Kelvin Arcelay Mattias
Daniel
Robert
Andrew Bell
Andreas
Marco Bouterse James Huntsman Guameri
Lance Bums
Daniel
Carey
Bertrand
Ignacio
S0ren Christensen TimKane
Daniel
Vicente
Courtois
Paul Demeulenaere Christopher Kingsley
Frani;ois
Jean-Francois
AshrafEassa Jean-Baptiste
Wolfgang
Fredrik
Brandon
Jean-Frani;ois
Nicholas Francis
Marcus Fritzsch Javier
Taylor
Gerpheide Michael
Nicholas Gildea Dae Myung
Collin
Cuellar HyukKim
Damien Y oungsik
Kim
Engel William Leu Joel de Vahl
Devic Kieron Lanning Justin
Brian Sharp
Sean Slavik
Engkvist Shaochun
Fogerty Yushuo Liu
Dube Kwon-il
Lee
Lars Viklund
Ken Voskuil
Tim Stewart
Brook Miles
Ian Whyte
My les
Lin
Soufi Souaiaia
Tiago Sousa
Aaron Spehr
Squirek
Moya Pérez Graham Wihlidal
Soufiane
Khiat Daniel Smith
Lepesme Runar Thorstensen
F Fortin YunlongMa Shawn Walker-Salas