Linear algebra for game developers ~ part 1 - Wolfire Games Blog
-
Upload
ryan-murray -
Category
Documents
-
view
37 -
download
0
description
Transcript of Linear algebra for game developers ~ part 1 - Wolfire Games Blog
-
9/19/14 8:09 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 1 of 15http://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 Overgrowthand other new stuff. Be sure to subscribe to get the latest news! Also, be sure to check out the forums foreven more up to date news - or get on IRC for up to the second updates. Overgrowth alpha 33Starting Grass
Linear algebra for game developers ~ part 1Add 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 postsabout linear algebra as it applies to game development. I decided if I'm going to write about that, I might aswell start at the beginning! This will be review to many of you who have written games before or takenclasses in kinematic physics, so please bear with me for this introductory post -- I will get to more advancedtopics 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 directionof a camera, or the velocity of a race car, you will have to use vectors. The better you understand linearalgebra, 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-Dimensionalexamples:
The position vector indicates that the man is standing two meters east of the origin, and one meter north. Thevelocity vector shows that in one minute, the plane moves three kilometers up, and two to the left. Thedirection vector tells us that the pistol is pointing to the right.
-
9/19/14 8:09 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 2 of 15http://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. Forexample, the vector (1,0) could be the direction for the gun as shown, but it could also be the position of abuilding one mile to the east of your current position, or the velocity of a snail moving right at a speed of 1mph.
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'tmean much by itself. Three what? Five what? In Overgrowth, positions are always given in meters, andvelocities in meters per second. The first number is east, the second is up, and the third is north. Negativenumbers represent the opposite directions: west, down, and south. The position represented by (3,5,2) is 3meters 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 additionTo 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 additionis physics integration. Any physically-based object will likely have vectors for position, velocity, andacceleration. For every frame (usually 1/60th of a second), we have to integrate these vectors -- that is, addthe 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), becausegravity 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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 3 of 15http://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 addhis 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 addhis 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 gamecalculates the new velocity and position using physics integration (via vector addition). Fun fact: this is thesame kind of integration problem that you solve using integral calculus - we are just using an approximatebrute-force approach. I found it much easier to pay attention to calculus classes by thinking about physicalapplications like this.
Vector subtractionSubtraction works in the same way as addition -- subtracting one component at a time. Vector subtraction isuseful for getting a vector that points from one position to another. For example, let's say the player isstanding at (1,2) with a laser rifle, and an enemy robot is at (4,3). To get the vector that the laser must travelto 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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 4 of 15http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Scalar-vector multiplicationWhen we talk about vectors, we refer to individual numbers as scalars. For example, (3,4) is a vector, 5 is ascalar. In games, it is often useful to multiply a vector by a scalar. For example, we can simulate basic airresistance by multiplying the player's velocity by 0.9 every frame. To do this, we just multiply eachcomponent 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 timeThat is all anyone needs to know about vectors to make something like Mario, Pong, or Space Invaders, butthere is still a lot left! For part two I would like to get to dot products, cross products, normalization, andreflection, 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!
TweetTweet 89
Overgrowth alpha 33Starting Grass
76 Comments Wolfire Blog Login
Sort by Best Share
Join the discussion
Favorite
474ShareShare
-
9/19/14 8:09 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 5 of 15http://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^2X = X0 + Vx0*t + 1/2*Ax*t^2
Here (Y0) and (X0) are the starting position, (Vy0) and (Vx0) are the starting velocities alongthose coordinates (you had cos/sin-thing, it's the same) and (Ay) and (Ax) are the accelerationsalong x and y. Now this function pair works as your teacher has told you, and this is what theMario 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, youcan 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 NiggurathShub Niggurath, thanks for such huge post =DAnd 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 postwas about vectors so i thinked that Mario's jump example is to explain vectors and notfor real algorithm). Haven't thinked that vectors are equal to these formulas, now i seethat =)Thanks again =)
Reply
romani 3 years agoWhy do you need to subtract these ((4,3)-(1,2) = (4-1, 3-2) = (3,1).) vectors when you alread havethe destination of robot (4,3)?! In opengl you just draw a line to represent the laser with twovertices, A and B, A = (1,2), B = (4,3). The line is drawn from A to B and no subtraction isrequired. 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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 6 of 15http://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 thebullet's velocity vector.
Also, as he mentions in part 2, vector-subtraction is needed to find the distance betweentwo points.
4
Reply
Guy 3 years ago> romaniWhy know how to add numbers when you can put them into your calculator?
1
Reply
atomic1fire 3 years ago> Guyit's not about knowing the answer, it's about understanding the problem.
8
Reply
Danno 3 years ago> atomic1fireThat was his point, I suspect; he was replying to romani, who asked whyyou'd want to subtract vectors, when you can just toss the vertices intoOpenGL.
1
Reply
Guy2 3 years ago> Guyyou're stupid you dont know how to add.
3
Reply
Uberpwn 5 years agoThis is why I love the Wolfire blog. It's a learning experience for aspiring game developers. Aninside look into the steps of development.
Also because David actually manages to make things like this interesting. xD 3
Reply
eric 2 years agoI took university level liner algebra and never understood what the purpose of all this operationswere, 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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 7 of 15http://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 agoVery easy explanation which is useful!!!!!!!!!!!
1
Reply
daGrevis 3 years agoSimple, but genial. Without your article I would be blind... i got only x and y as coordinates.Thanks, mate!
1
Reply
daniellla 4 years agoI like the way you have explained it ...For beginners it is really helpfull.
1
Reply
Sophie Houlden 5 years agoyou arent going too slow at all, this is the perfect pace IMO, I know people who get stumped byvectors 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 areexplained like this I'll have them in no time :D
1
Reply
ChevyRay 5 years ago> Sophie HouldenTotally agreed. This speed is perfect. Fast/knowledgable people can skip ahead, butslow people are screwed if the tutorial goes too fast. I know many beginning gamedevelopers 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 ofthe early and mid-level stuff. But if they're explained as simply as this, with all thewonderful supporting diagrams, then I'm stoked.
1
Reply
tomascokis 5 years ago> ChevyRayto be fair, skipping ahead is hard when the head hasn't been posted yet.
matto1990 5 years agoWhat you've covered there is something that took my maths teacher a few weeks to teach. Niceone :P
Share
Share
Share
Share
Share
Share
Share
-
9/19/14 8:09 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 8 of 15http://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'ssomething 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 findout 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 tounderstand.
Looking forward to the next parts. I've done dot products becore but some of the others I dontthink I've done.
1
Reply
konsnos 5 years ago> matto1990Correct me if I'm wrong but 10 miles per hour is a vector. It involves two numbers.
Reply
matto1990 5 years ago> konsnosI thought the definition of a vector was that it had magnitude and direction. 10miles 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> matto1990Speed is a scalar quantity, but velocity is technically a vector, with theaddition of direction. Lots of times the two are used interchangeably,though.
1
Karl 5 years ago> konsnosI 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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 9 of 15http://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 onedimensional vector space.
Reply
Overkill 5 years ago> konsnosIt 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 agoI am a little late to this blog post, but this is exactly what I have been looking for. Thank you verymuch!!
Reply
mohammed 4 months agoyour are legend very nice explantion ^_^
Reply
nicoenarg 8 months agoThank you! This is the first article that I have read that tells me that I actually do know everythingyou explained in this part. I honestly wasn't aware whether I knew about vectors when it comesto their relation to games or not. People usually throw out stuff like, "You need to know vectorsbecause vectors are how everything in a game works..." in a threatening way that has so farmade 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 agoI really like the way you explain thinks, event when you are already know it, it's a big pleasure toread it cose you make thinks so easy, thank you :)
Reply
Natasha Dieppa a year agoThanks for sharing! I will be teaching these concepts next year to my 9th grade students. I amexcited to show them how our "normal" math concepts relate to gaming.
VlOlz Elxenoanizd a year agoI 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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 10 of 15http://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 incollege, I was literally the best Linear Algebra student. I understood everything [from an abstractmath 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 andteach us how to apply LA. thanks again! May god reward you for this!
Reply
Krivochenko 3 years ago
Reply
Tunc 3 years agoI didn't know what vectors were, so this helped a lot. Thanks!
Reply
Mr. picture plaques 4 years agoInformative & educational. I never did understand those things back then but thanks to yoursimple yet effective explanations & drawing, I think I finally get it.
Reply
Nema Mansuri 5 years agoYou don't really need to study linear algebra for making games... you just need to know thegeneral properties of vectors quantities.
Reply
Dakaggo 5 years agoI've never taken this in school but it makes perfect sense so far so I'm glad you. It's surprisinglyeasy. Someone said it took two weeks for their teacher to teach this... how? Takes like 30-45minutes to understand.....
Some people might think you're going too slow but rather have some people learn patiencethan have some people unable to keep up.
Reply
Dakaggo 5 years ago> DakaggoX_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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 11 of 15http://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 moreadvanced tutorial, though! We are quickly approaching the limits of what I know about vectors :-)
Reply
BierLiebHaber 5 years agohey after reading this I directly tried to make a little programm to let a "#" fall in the console withpython ^^ (it took me ages due to my crapy programming skills and the fact that its 2:42amhere xD)here is the code if you want to try it: http://pastebin.com/f60fd38e9i 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 agoEast, up, north? What happened to the right-hand rule?
Reply
David 5 years agoMod > xI'm a vector southpaw.
Reply
Wilbefast 5 years agoAcceleration is the variation of speed, which is the variation of position, and in the end it's allvectors - great post, it is a good idea to start with something simple before moving on to thetricky stuff, though the whole distance between two points thing might be useful (eg - length ofa 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) butthat'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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 12 of 15http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Reply
Phillip 5 years agoMod > Wilbefastproduit scalaire = dot productproduit vectoriel = cross productproduit tensoriel = tensor product
Reply
Wilbefast 5 years ago> Phillipwait... "un produit" you're right of course, it's masculin. I'm getting better at thiswhole "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 Idon'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 operationswork in general is much more helpful. Especially if you ever have to do equation solving, it'sgood to know how linear systems and matrices work. It also helps to have general matrixknowledge, 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 whichblend 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 aprogram's runtime = bad, solving generalized systems can be helpful to reducing complexity ofyour game's various algorithms.
Karl 5 years ago> OverkillI don't like the "definition" either, but to be fair, Matrices ARE vectors. They satisfy theaxioms of a vector space. I think it should be pretty clear that David doesn't really carehow some of the mathematical objects he works with are defined mathematically unlessit 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 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 13 of 15http://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 agoWhat about eigenvalues/eigenvectors, hermitian matrices, and SVD?
Reply
benblo 5 years agoToo slow... but I guess you're training your future modders eh ;) ?
Reply
TheBigCheese 5 years agoVery 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 explainwhat exactly the dot product is doing, so it makes it seem like just an arbitrary calculation.
Reply
Noobos 5 years agoThanx, seems a a good serial. I finally understand why games use vectors instead of magnitudeand 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'rea 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 agoThis 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 neverseen things presented in this mannor. It's really neat to see how things are done for the VideoGame end of things.
Each industry seems to have their own system for doing things, some go with a simple stepmeathod and others with a more complicated conceptual process. I'm not sure which I'dcategorize this as.
I'm really looking forward to more discussion on this. I'll have to read over it more carefullywhen I have a little more time on my hands. I'm actually about to go turn in a Lab report for aPhysics-ish type Lab Class lol
Share
Share
Share
Share
Share
Share
-
9/19/14 8:09 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 14 of 15http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Load more comments
Reply
Reply
FishyBoy 5 years agoOh.
I figured this shit out when I was like 12 using Game Maker, and yet the whole vector basedthing 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 sited
Share
Share
Subscribe
Email Updates
Sign Up
Search Search
CategoriesAlphaArtBusinessDesign TourGame DesignGame TechOtherOvergrowthPre-OvergrowthPress
-
9/19/14 8:09 AMLinear algebra for game developers ~ part 1 - Wolfire Games Blog
Page 15 of 15http://blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-1/
Videos
Blog Roll2D BoyCryptic SeaDanLabGamesEdmund McMillenFun Motionicculus.orgIndieGames BlogInfinite AmmoNimblebitPositech BlogTIGSource
Join UsFacebookModDBSteamTwitterYouTube
2013 Wolfire Games