Você está na página 1de 15

9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog

Page 1 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Welcome to the Wolfire Blog! This is where we keep everyone up to date on our progress on Overgrowth
and other new stuff. Be sure to subscribe to get the latest news! Also, be sure to check out the forums for
even more up to date news - or get on IRC for up to the second updates.
Overgrowth alpha 33
Starting Grass
Linear algebra for game developers ~ part 1
Add Comment! By David Rosen on July 1st, 2009
When I posted about decals last week, a number of readers commented that they would be interested in posts
about linear algebra as it applies to game development. I decided if I'm going to write about that, I might as
well start at the beginning! This will be review to many of you who have written games before or taken
classes in kinematic physics, so please bear with me for this introductory post -- I will get to more advanced
topics later.
Why do we care about linear algebra?
Linear algebra is the study of vectors. If your game involves the position of an on-screen button, the direction
of a camera, or the velocity of a race car, you will have to use vectors. The better you understand linear
algebra, the more control you will have over the behavior of these vectors.
What is a vector?
In games, vectors are used to store positions, directions, and velocities. Here are some 2-Dimensional
examples:
The position vector indicates that the man is standing two meters east of the origin, and one meter north. The
velocity vector shows that in one minute, the plane moves three kilometers up, and two to the left. The
direction vector tells us that the pistol is pointing to the right.
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 2 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
As you can see, a vector by itself is just a set of numbers -- it is only given meaning by its context. For
example, the vector (1,0) could be the direction for the gun as shown, but it could also be the position of a
building one mile to the east of your current position, or the velocity of a snail moving right at a speed of 1
mph.
For this reason, it's important to keep track of your units. Let's say we have a vector V (3,5,2). This doesn't
mean much by itself. Three what? Five what? In Overgrowth, positions are always given in meters, and
velocities in meters per second. The first number is east, the second is up, and the third is north. Negative
numbers represent the opposite directions: west, down, and south. The position represented by (3,5,2) is 3
meters east, 5 meters up, and 2 meters north, as shown here:
Now that we've gone over the basics of vectors, we need to know how to use them.
Vector addition
To add vectors together, you just add each component together separately. For example:
(0,1,4) + (3,-2,5) = (0+3, 1-2, 4+5) = (3,-1,9)
Why do we want to add vectors together? One of the most common applications in games for vector addition
is physics integration. Any physically-based object will likely have vectors for position, velocity, and
acceleration. For every frame (usually 1/60th of a second), we have to integrate these vectors -- that is, add
the velocity to the position, and the acceleration to the velocity.
Let's consider the example of Mario jumping. He starts at position (0,0). As he starts the jump, his velocity is
(1,3) -- he is moving upwards quickly, but also to the right. His acceleration throughout is (0,-1), because
gravity is pulling him downwards. Here is what his jump looks like over the course of seven more frames.
The black text specifies his velocity for each frame.
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 3 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
We can walk through the first couple frames by hand to see how this works.
For the first frame, we add his velocity (1,3) to his position (0,0) to get his new position (1,3). Then, we add
his acceleration (0,-1) to his velocity (1,3) to get his new velocity (1,2).
We do it again for the second frame. We add his velocity (1,2) to his position (1,3) to get (2,5). Then, we add
his acceleration (0,-1) to his velocity (1,2) to get (1,1).
Usually in games the player controls a character's acceleration with the keyboard or gamepad, and the game
calculates the new velocity and position using physics integration (via vector addition). Fun fact: this is the
same kind of integration problem that you solve using integral calculus - we are just using an approximate
brute-force approach. I found it much easier to pay attention to calculus classes by thinking about physical
applications like this.
Vector subtraction
Subtraction works in the same way as addition -- subtracting one component at a time. Vector subtraction is
useful for getting a vector that points from one position to another. For example, let's say the player is
standing at (1,2) with a laser rifle, and an enemy robot is at (4,3). To get the vector that the laser must travel
to hit the robot, you can subtract the player's position from the robot's position. This gives us:
(4,3)-(1,2) = (4-1, 3-2) = (3,1).
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 4 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Scalar-vector multiplication
When we talk about vectors, we refer to individual numbers as scalars. For example, (3,4) is a vector, 5 is a
scalar. In games, it is often useful to multiply a vector by a scalar. For example, we can simulate basic air
resistance by multiplying the player's velocity by 0.9 every frame. To do this, we just multiply each
component of the vector by the scalar. If the player's velocity is (10,20), the new velocity is:
0.9*(10,20) = (0.9*10, 0.9*20) = (9,18).
Next time
That is all anyone needs to know about vectors to make something like Mario, Pong, or Space Invaders, but
there is still a lot left! For part two I would like to get to dot products, cross products, normalization, and
reflection, and then part three can be about transformations and vector spaces. Does this make sense so far?
Am I going too slow?
Click here for part 2 or part 3!
Tweet Tweet 89
Overgrowth alpha 33
Starting Grass
76 Comments Wolfire Blog Login
!
Sort by Best Share
"
Join the discussion
Favorite
#
474 Share Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 5 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
Shub Niggurath ! 5 years ago
see more
Too simple for me.. but it's a good start ;)
puuh.. @GreenFlame:
You want to start with the equations that give the position in the Cartesian coordinate system of
(X,Y) :
Y = Y0 + Vy0*t + 1/2*Ay*t^2
X = X0 + Vx0*t + 1/2*Ax*t^2
Here (Y0) and (X0) are the starting position, (Vy0) and (Vx0) are the starting velocities along
those coordinates (you had cos/sin-thing, it's the same) and (Ay) and (Ax) are the accelerations
along x and y. Now this function pair works as your teacher has told you, and this is what the
Mario example is based upon; it just used a brute force approach.
Your question is why didn't you see this version of these equations anywhere there.
What you did see was the derivative of those functions put to USE (hence the mention of
"integral calculus" and "brute force" in the original post). Once you know how things work, you
can apply the idea without messing with the equations directly. Derivative is the rate-of-change:
the rate-of-change of position is velocity (and r-o-c of velocity is acceleration).
7
% &

! Reply !
GreenFlame ! 5 years ago $ Shub Niggurath
Shub Niggurath, thanks for such huge post =D
And yeah... i missed starting coords because i meaned they equal to 0 =)
So i asked my question because i want to know how to make that calculations(this post
was about vectors so i thinked that Mario's jump example is to explain vectors and not
for real algorithm). Haven't thinked that vectors are equal to these formulas, now i see
that =)
Thanks again =)

% &

! Reply !
romani ! 3 years ago
Why do you need to subtract these ((4,3)-(1,2) = (4-1, 3-2) = (3,1).) vectors when you alread have
the destination of robot (4,3)?! In opengl you just draw a line to represent the laser with two
vertices, A and B, A = (1,2), B = (4,3). The line is drawn from A to B and no subtraction is
required. Since a line from A to B is more efficient why bother with all the subtraction?!
3
% &

BlueRaja ! 3 years ago $ romani
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 6 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
BlueRaja ! 3 years ago $ romani
@romani: If you want to draw a bullet that travels from the rifle to the robot, the vector
(robot_position - rifle_position) gives you the direction the bullet needs to travel in.
Normalize this vector (see part 2) and multiply by the speed-scaler, and you get the
bullet's velocity vector.
Also, as he mentions in part 2, vector-subtraction is needed to find the distance between
two points.
4
% &

! Reply !
Guy ! 3 years ago $ romani
Why know how to add numbers when you can put them into your calculator?
1
% &

! Reply !
atomic1fire ! 3 years ago $ Guy
it's not about knowing the answer, it's about understanding the problem.
8
% &

! Reply !
Danno ! 3 years ago $ atomic1fire
That was his point, I suspect; he was replying to romani, who asked why
you'd want to subtract vectors, when you can just toss the vertices into
OpenGL.
1
% &

! Reply !
Guy2 ! 3 years ago $ Guy
you're stupid you dont know how to add.
3
% &

! Reply !
Uberpwn ! 5 years ago
This is why I love the Wolfire blog. It's a learning experience for aspiring game developers. An
inside look into the steps of development.
Also because David actually manages to make things like this interesting. xD
3
% &

! Reply !
eric ! 2 years ago
I took university level liner algebra and never understood what the purpose of all this operations
were, so I just studied for the test and forgot everything....now it all makes sense... thank you
2
% &

Ken Hardman ! 2 years ago
Share
Share
Share
Share
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 7 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
Thanks for the brief on Linear Algebra. Here is Linear Algebra from an engineers perspective.
http://stemstories.wordpress.c...
1
% &

! Reply !
datt ! 3 years ago
Very easy explanation which is useful!!!!!!!!!!!
1
% &

! Reply !
daGrevis ! 3 years ago
Simple, but genial. Without your article I would be blind... i got only x and y as coordinates.
Thanks, mate!
1
% &

! Reply !
daniellla ! 4 years ago
I like the way you have explained it ...For beginners it is really helpfull.
1
% &

! Reply !
Sophie Houlden ! 5 years ago
you arent going too slow at all, this is the perfect pace IMO, I know people who get stumped by
vectors and this will be a great thing to point to.
plus I look forward to your later topics, all of them are currently beyond me, but if they are
explained like this I'll have them in no time :D
1
% &

! Reply !
ChevyRay ! 5 years ago $ Sophie Houlden
Totally agreed. This speed is perfect. Fast/knowledgable people can skip ahead, but
slow people are screwed if the tutorial goes too fast. I know many beginning game
developers who would benefit from this already.
I too can't wait for the later articles, as I've already got my head wrapped around most of
the early and mid-level stuff. But if they're explained as simply as this, with all the
wonderful supporting diagrams, then I'm stoked.
1
% &

! Reply !
tomascokis ! 5 years ago $ ChevyRay
to be fair, skipping ahead is hard when the head hasn't been posted yet.

% &

matto1990 ! 5 years ago
What you've covered there is something that took my maths teacher a few weeks to teach. Nice
one :P
Share
Share
Share
Share
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 8 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
one :P
One think you didnt really mention was the difference between a scalar and a vector. That's
something which helped me understand why vectors were useful.
For example:
The boat moves 10 miles is a scalar because we dont have a direction.
The boat moved 10 miles north is a vector because we can now tell its direction and hence find
out where is ends up.
The boat is traveling at 10 miles per hour is a scalar.
The boat is traveling at 10 miles per hour north is a vector.
It's a very simple thing but for someone who doesnt do maths it might not be so easy to
understand.
Looking forward to the next parts. I've done dot products becore but some of the others I dont
think I've done.
1
% &

! Reply !
konsnos ! 5 years ago $ matto1990
Correct me if I'm wrong but 10 miles per hour is a vector. It involves two numbers.

% &

! Reply !
matto1990 ! 5 years ago $ konsnos
I thought the definition of a vector was that it had magnitude and direction. 10
miles per hour has a magnitude but no direction.
I could be wrong though.
I managed to find this: http://en.wikipedia.org/wiki/S...
If you beleive wikipedia I think that mph is a scalar.
1
% &

! Reply !
Dylan Craig ! 5 years ago $ matto1990
Speed is a scalar quantity, but velocity is technically a vector, with the
addition of direction. Lots of times the two are used interchangeably,
though.
1
% &

Karl ! 5 years ago $ konsnos
I would call it a scalar unless I wanted to make a point about it residing in a one
Share
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 9 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
I would call it a scalar unless I wanted to make a point about it residing in a one
dimensional vector space.

% &

! Reply !
Overkill ! 5 years ago $ konsnos
It involves one number. A fraction in units of speed. A fraction p/q is still a scalar.
A fractional scalar, but a scalar, still.
A vector would be speed with a direction.

% &

! Reply !
Meteor Fury ! 16 days ago
I am a little late to this blog post, but this is exactly what I have been looking for. Thank you very
much!!

% &

! Reply !
mohammed ! 4 months ago
your are legend very nice explantion ^_^

% &

! Reply !
nicoenarg ! 8 months ago
Thank you! This is the first article that I have read that tells me that I actually do know everything
you explained in this part. I honestly wasn't aware whether I knew about vectors when it comes
to their relation to games or not. People usually throw out stuff like, "You need to know vectors
because vectors are how everything in a game works..." in a threatening way that has so far
made me feel like "yeah, never made a game, ergo I must not know about this stuff." Anyway,
thanks a lot for explaining it the way you did.

% &

! Reply !
alex ! a year ago
I really like the way you explain thinks, event when you are already know it, it's a big pleasure to
read it cose you make thinks so easy, thank you :)

% &

! Reply !
Natasha Dieppa ! a year ago
Thanks for sharing! I will be teaching these concepts next year to my 9th grade students. I am
excited to show them how our "normal" math concepts relate to gaming.

% &

VlOlz Elxenoanizd ! a year ago
I LOVVVEEE YOU MANNNNNN!!!!!! I FREACKIN' DOO!!!!!!!
Thank you SOOOO much!! You don't know how much you just helped me. When I was in
Share
Share
Share
Share
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 10 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
Thank you SOOOO much!! You don't know how much you just helped me. When I was in
college, I was literally the best Linear Algebra student. I understood everything [from an abstract
math perspective]. But when I got to applying what I learned in games, I didn't know what to do!
I felt like I didn't learn anything at all! I wish that doctors would follow the approach you did and
teach us how to apply LA. thanks again! May god reward you for this!

% &

! Reply !
Krivochenko ! 3 years ago
" #$%& '()*+,& -.&%/01

% &

! Reply !
Tunc ! 3 years ago
I didn't know what vectors were, so this helped a lot. Thanks!

% &

! Reply !
Mr. picture plaques ! 4 years ago
Informative & educational. I never did understand those things back then but thanks to your
simple yet effective explanations & drawing, I think I finally get it.

% &

! Reply !
Nema Mansuri ! 5 years ago
You don't really need to study linear algebra for making games... you just need to know the
general properties of vectors quantities.

% &

! Reply !
Dakaggo ! 5 years ago
I've never taken this in school but it makes perfect sense so far so I'm glad you. It's surprisingly
easy. Someone said it took two weeks for their teacher to teach this... how? Takes like 30-45
minutes to understand.....
Some people might think you're going too slow but rather have some people learn patience
than have some people unable to keep up.

% &

! Reply !
Dakaggo ! 5 years ago $ Dakaggo
X_X I forgot half a sentence.
"so I'm glad you are writing this series."

% &

Loeffe ! 5 years ago
>Does this make sense so far? Am I going too slow?
Share
Share
Share
Share
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 11 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
>Does this make sense so far? Am I going too slow?
It makes perfect sense, and I think it's great that you start from scratch. I'd love to see more
advanced tutorial, though! We are quickly approaching the limits of what I know about vectors :-
)

% &

! Reply !
BierLiebHaber ! 5 years ago
hey after reading this I directly tried to make a little programm to let a "#" fall in the console with
python ^^ (it took me ages due to my crapy programming skills and the fact that its 2:42am
here xD)
here is the code if you want to try it: http://pastebin.com/f60fd38e9
i hope u have fun and im allready waiting for the next part of this (and of cause all other news ;))
cya
-BierLiebHaber
PS: i dont know it the programm works on windows (im on a linux PC here)

% &

! Reply !
x ! 5 years ago
East, up, north? What happened to the right-hand rule?

% &

! Reply !
David ! 5 years ago Mod $ x
I'm a vector southpaw.

% &

! Reply !
Wilbefast ! 5 years ago
Acceleration is the variation of speed, which is the variation of position, and in the end it's all
vectors - great post, it is a good idea to start with something simple before moving on to the
tricky stuff, though the whole distance between two points thing might be useful (eg - length of
a vector): D = sqrt((x2 - x1)^2 + (y2 - y1)^2) = sqrt(X^2 + Y^2)
If you're doing rigid body physics, it might be good to do vector multiplication (for moments) but
that's probably not good for an introduction to vectors.
edit: I've learned all this stuff in french - could someone tell me the english terms:
- "produit scalaire" = x . y
- "produit vectorielle" = x ^ y
- "produit tensorielle" = x (x) y

% &

Share
Share
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 12 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
Phillip ! 5 years ago Mod $ Wilbefast
produit scalaire = dot product
produit vectoriel = cross product
produit tensoriel = tensor product

% &

! Reply !
Wilbefast ! 5 years ago $ Phillip
wait... "un produit" you're right of course, it's masculin. I'm getting better at this
whole "female tables" thing but it's still tricky sometimes - or I forget :S
Thanks for that - I get the feeling my whole education will be worthless because I
don't know what a "Bobine" is in English.

% &

! Reply !
Overkill ! 5 years ago
"Linear algebra is the study of vectors."
This definition is too lacking for me. It's also the study of linear systems, matrices and fields.
Vectors are a subset of matrices, which are 1xN or Mx1 in size.
Sure, while mostly Vectors are useful to game development, knowing how matrix operations
work in general is much more helpful. Especially if you ever have to do equation solving, it's
good to know how linear systems and matrices work. It also helps to have general matrix
knowledge, if for some reason, you are doing calculations related to rotation/translation/scaling.
Of course, then there's even more complex layers of math like Differential Equations which
blend Calculus and Linear Algebra.
That said, this is a nice start, for learning how vectors work. You should cover determinants,
invertable matrices, and eigenvalues/eigenvectors, too. Although, calculating of these during a
program's runtime = bad, solving generalized systems can be helpful to reducing complexity of
your game's various algorithms.

% &

Karl ! 5 years ago $ Overkill
I don't like the "definition" either, but to be fair, Matrices ARE vectors. They satisfy the
axioms of a vector space. I think it should be pretty clear that David doesn't really care
how some of the mathematical objects he works with are defined mathematically unless
it becomes an issue in his use.
I think wikipedia is closer actually to David's definition for linear algebra than yours
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 13 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
! Reply !
I think wikipedia is closer actually to David's definition for linear algebra than yours
(Linear algebra is the branch of mathematics concerned with the study of vectors...).

% &

! Reply !
gamer ! 5 years ago
What about eigenvalues/eigenvectors, hermitian matrices, and SVD?

% &

! Reply !
benblo ! 5 years ago
Too slow... but I guess you're training your future modders eh ;) ?

% &

! Reply !
TheBigCheese ! 5 years ago
Very simple, but good start for those who are complete beginners.
Make sure to find a good visual way to show the Dot Product. Many tutorials neglect to explain
what exactly the dot product is doing, so it makes it seem like just an arbitrary calculation.

% &

! Reply !
Noobos ! 5 years ago
Thanx, seems a a good serial. I finally understand why games use vectors instead of magnitude
and angle - it's just simplier.
You're slow like in "Vectors for dummies", but that's really fine. If you're not going to put the're
a gif animation of two apples saying "one apple and one apple are two apples" I'm satisfied :c)
P.S.: Love your diagrams

% &

! Reply !
mightywarriorex ! 5 years ago
This is a great article. I'm really excited to read about this.
Being an Engineering student I've done so much with Physics and math in general but never
seen things presented in this mannor. It's really neat to see how things are done for the Video
Game end of things.
Each industry seems to have their own system for doing things, some go with a simple step
meathod and others with a more complicated conceptual process. I'm not sure which I'd
categorize this as.
I'm really looking forward to more discussion on this. I'll have to read over it more carefully
when I have a little more time on my hands. I'm actually about to go turn in a Lab report for a
Physics-ish type Lab Class lol

% &

Share
Share
Share
Share
Share
Share
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 14 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Load more comments
! Reply !
% &

! Reply !
FishyBoy ! 5 years ago
Oh.
I figured this shit out when I was like 12 using Game Maker, and yet the whole vector based
thing kind of freaked me out upon mention.
As it turns out, not as impressive as it sounds.
Still, next one sounds like it could be useful. I eagerly await it.

% &

Subscribe
'
Add Disqus to your site
(
Share
Share
Subscribe
Email Updates
Sign Up
Search
Search
Categories
Alpha
Art
Business
Design Tour
Game Design
Game Tech
Other
Overgrowth
Pre-Overgrowth
Press
9/19/14 8:09 AM Linear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 15 of 15 http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Videos
Blog Roll
2D Boy
Cryptic Sea
DanLabGames
Edmund McMillen
Fun Motion
icculus.org
IndieGames Blog
Infinite Ammo
Nimblebit
Positech Blog
TIGSource
Join Us
Facebook
ModDB
Steam
Twitter
YouTube
2013 Wolfire Games