Vous êtes sur la page 1sur 16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

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. Starting Grass Wolfire Answers Community Questions

Linear algebra for game developers ~ part 2


51 comments By David Rosen on July 3rd, 2009 Now that I've introduced vectors in Part 1, we need to look at some of the fundamental tools for working with them. The most important tools to understand are length, normalization, distance, the dot product, and the cross product. Once you wrap your mind around these concepts, and write functions to use them, you can solve most vector problems you might encounter.

Length
If we have a ship with velocity vector V (4,3), we might also want to know how fast it is going, in order to calculate how much the screen should shake or how much fuel it should use. To do that, we need to find the length (or magnitude) of vector V. The length of a vector is often written using || for short, so the length of V is |V|. We can think of V as a right triangle with sides 4 and 3, and use the Pythagorean theorem to find the hypotenuse: x2 + y2 = h2. That is, the length of a vector H with components (x,y) is sqrt(x2+y2). So, to calculate the speed of our ship, we just use: |V| = sqrt(42+32) = sqrt(25) = 5

blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

1/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

This works with 3D vectors as well -- the length of a vector with components (x,y,z) is sqrt(x2+y2+z2).

Distance
If the player P is at (3,3) and there is an explosion E at (1,2), we need to find the distance between them to see how much damage the player takes. This is easy to find by combining two tools we have already gone over: subtraction and length. We subtract P-E to get the vector between them, and then find the length of this vector to get the distance between them. The order doesn't matter here, |E-P| will give us the same result. Distance = |P-E| = |(3,3)-(1,2)| = |(2,1)| = sqrt(22+12) = sqrt(5) = 2.23

Normalization
When we are dealing with directions (as opposed to positions or velocities), it is important that they have unit length (length of 1). This makes life a lot easier for us. For example, let's say there is a gun pointing in the direction of (1,0) that shoots a bullet at 20 m/s. What is the velocity of the bullet? Since the direction has length 1, we can just multiply the direction and the bullet speed to get the bullet velocity: (20,0). If the direction vector had any other length, we couldn't do this -- the bullet would be too fast or too slow. A vector with a length of 1 is called "normalized". So how do we normalize a vector (set its length to 1)? Easy, we divide each component by the vector's length. If we want to normalize vector V with components (3,4), we just divide each component by its length, 5, to get (3/5, 4/5). Now we can use the pythagorean theorem to prove that it has length 1: (3/5)2 + (4/5)2 = 9/25 + 16/25 = 25/25 = 1

Dot product
What is the dot product (written )? Let's hold off on that for a second, and look at how we calculate it. To get the dot product of two vectors, we multiply the components, and then add them together. (a1,a2)(b1,b2) = a1b1 + a2b2 For example, (3,2)(1,4) = 3*1 + 2*4 = 11. This seems kind of useless at first, but lets look at a few examples:

blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

2/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

Here, we can see that when the vectors are pointing the same direction, the dot product is positive. When they are perpendicular, the dot product is zero, and when they point in opposite directions, it is negative. Basically, it is proportional to how much the vectors are pointing in the same direction. This is a small taste of the power of the dot product, and it's already pretty useful! Let's say we have a guard at position G (1,3) facing in the direction D (1,1), with a 180 field of view. We have a hero sneaking by at position H (3,2). Is he in the guard's field of view? We can find out by checking the sign of the dotproduct of D and V (the vector from the guard to the hero). This gives us: V = H-G = (3,2)-(1,3) = (3-1,2-3) = (2,-1) DV = (1,1)(2,-1) = 1*2+1*-1 = 2-1 = 1 Since 1 is positive, the hero is in the guard's field of view!

We know that the dot product is related to the extent to which the vectors are pointing in the same direction, but what is the exact relation? It turns out that the exact equation for the dot product is: AB = |A||B|cos Where (pronounced "theta") is the angle between A and B. This allows us to solve for if we want to find out the angle: = acos([AB]/[|A||B|]). As I mentioned before, normalizing vectors makes our life easier! If A and B are normalized, then the equation is simply:
blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/ 3/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

= acos(AB) Let's revisit the guard scenario above, except the guard's field of view is only 120. First, we get the normalized vectors for the direction the guard is facing (D'), and the direction from the guard to the hero (V'). Then, we check the angle between them. If it is greater than 60 (half of the field of view), then the hero is not seen. D' = D/|D| = (1,1)/sqrt(12+12) = (1,1)/sqrt(2) = (0.71,0.71) V' = V/|V| = (2,-1)/sqrt(22+(-1)2) = (2,-1)/sqrt(5) = (0.89,-0.45) = acos(D'V') = acos(0.71*0.89 + 0.71*(-0.45)) = acos(0.31) = 72 The angle between the center of the guard's vision and the hero is 72, so the guard does not see him!

I know this looks like a lot of work, and it is, because I'm doing it by hand. However, in a program, this is pretty simple. Here is what this would like in Overgrowth using the C++ vector libraries I wrote (inspired by GLSL syntax).
/ / I n i t i a l i z es t a r t i n gv e c t o r s v e c 2g u a r d _ p o s=v e c 2 ( 1 , 3 ) ; v e c 2g u a r d _ f a c i n g=v e c 2 ( 1 , 1 ) ; v e c 2h e r o _ p o s=v e c 2 ( 3 , 2 ) ; / / P r e p a r en o r m a l i z e dv e c t o r s v e c 2g u a r d _ f a c i n g _ n=n o r m a l i z e ( g u a r d _ f a c i n g ) ; v e c 2g u a r d _ t o _ h e r o=n o r m a l i z e ( h e r o _ p o s-g u a r d _ p o s ) ; / / C h e c ka n g l e f l o a ta n g l e=a c o s ( d o t ( g u a r d _ f a c i n g _ n ,g u a r d _ t o _ h e r o ) ) ;

Cross Product
Let's say you have a boat that has cannons that fire to the left and right. Given that the boat is facing along the direction vector (2,1), in which directions do the cannons fire? This is easy in 2D: to rotate 90 degrees clockwise, just flip the two vector components, and then switch the sign of the second component. (a,b) becomes (b,-a). So, if the boat is facing along (2,1), the right-facing cannons fire towards (1,-2). The leftfacing cannons fire in the opposite direction, so we flip both signs to get: (-1,2).

blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

4/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

So, what if we want to do this in 3D? Let's revisit our sailing ship. We have a vector for the direction of the mast M, going straight up (0,1,0), and the direction of the north-north-east wind W (1,0,2), and we want to find the direction the sail S should stick out in order to best catch the wind. The sail has to be perpendicular to the mast, and also perpendicular to the wind. To solve this, we can use the cross product: S = M x W.

The cross product of A(a1,a2,a3)) and B(b1,b2,b3)) is: (a2b3-a3b2, a3b1-a1b3, a1b2-a2b1) So now we can plug in our numbers and solve our problem: S = MxW = (0,1,0)x(1,0,2) = ([1*2-0*0], [0*1-0*2], [0*0-1*1]) = (2,0,-1) This is pretty ugly to do by hand. For most graphics and game work I would recommend just encapsulating it in a function like the one below, and never thinking about the details again.
v e c 3c r o s s ( v e c 3a ,v e c 3b ){ v e c 3r e s u l t ;
blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/ 5/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

r e s u l t [ 0 ]=a [ 1 ]*b [ 2 ]-a [ 2 ]*b [ 1 ] ; r e s u l t [ 1 ]=a [ 2 ]*b [ 0 ]-a [ 0 ]*b [ 2 ] ; r e s u l t [ 2 ]=a [ 0 ]*b [ 1 ]-a [ 1 ]*b [ 0 ] ; r e t u r nr e s u l t ; }

Another common use for the cross product in games is to find surface normals -- the direction that a surface is facing. For example, let's take a triangle with vertex vectors A, B and C. How do we find the direction that the triangle is facing? It seems tricky, but we have the tools to do it now. We can use subtraction to get the direction from A to C (C-A) 'Edge 1' and A to B (B-A) 'Edge 2', and then use the cross product to find a new vector N perpendicular to both of them... the surface normal.

Here is what that would like in code:


v e c 3G e t T r i a n g l e N o r m a l ( v e c 3a ,v e c 3b ,v e c 3c ){ v e c 3e d g e 1=b a ; v e c 3e d g e 2=c a ; v e c 3n o r m a l=c r o s s ( e d g e 1 , e d g e 2 ) ; r e t u r nn o r m a l ; }

Fun fact: the basic expression for lighting in games is just NL, where N is the surface normal, and L is the normalized light direction. This is what makes surfaces bright when they face towards the light, and dark when they don't.

Next time
We covered what vectors are in Part 1, and now we have a solid toolbox for working with them. Next, I would like to talk about vector spaces and matrix transformations. This post was a lot more complicated than the last one -- is there anything confusing here that you have questions about?
blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/ 6/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

Click here for part 3!


Share

59 7

Tw eet

Starting Grass Wolfire Answers Community Questions

51 comments Leave a message...


Best Community jph
4 years ago

11

Share

Avatar

Great stuff,. nicely explained. For those of us that where not figuring math had much use to us, at the time we should have been learning it,. . then went on to try to build a game. Math teatures in highschool really should teach simple game dev. to get kids interested.
5 Reply M. Carabas Share
jph 4 years ago

that's so weird... I'm a private tutor and when tutoring Grade 11s and 12s for Algebra I usually ask them if they would like to write a simple computer game like pong (or tetris for the more adventurous)... usually the response is very enthusiastic... I've found this is the best way to get kids to take Algebra seriously... it's so much easier to explain vectors in terms of bullets ricocheting off walls instead of canoes being paddled against currents.
3 Reply Share
jph 4 years ago

P hil (S umguy 720) 2 Reply

You guys should release better math teachers along with the alphas.
Share
jph 4 years ago

TheB igChees e

Yeah, honestly I don't know why they don't take that approach. It really brings a lot of abstract down to earth.
Reply Share

Avatar

jarav

4 years ago

Here is an applet illustrating the cross-product ( requires Flash 10 ): http://www.physics101online.co...


2 Reply Share

Avatar

A ugus t Toman-Y ih

4 years ago
7/16

blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

14/10/13

Avatar

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

Your explanation of the dot and cross products were really good; I learned more about them from your article than a semester of calculus.
2 Reply

Share

Avatar

Mat hieu

4 years ago

Excellent information here! Thank you for taking the time to write all this, I would lova having all this explained when I started game development 8 years ago, but it's still definitely useful even now.
2 Reply

Share
2 years ago

Avatar

Fedhaghafoor

Product of two Vectors Product of vectors, cross product, scalar product, dot product, vector product, properties of vector product. http://www.infoaw.com/article....
1 Reply

Share

Avatar

CraigS t ern

4 years ago

Hi, this is very helpful! You lost me during the second paragraph of the section on normalizing a vector, though. I feel like there was supposed to be a paragraph in the middle explaining how the latter paragraph relates to the first, or defining the term "normalizing a vector," for that matter. :S
1 Reply Share
CraigStern 2 years ago

A donis _paradis e

Yes, I got lost on the normalize section too: "For example, let's say there is a gun pointing in the direction of (1,0) that shoots a bullet at 20 m/s. What is the velocity of the bullet?" ... 20 m/s ?? What have I missed?
Reply K orban3 Share
Adonis_paradise 2 years ago

Kind of an old one to reply to, but hell why not? 20 m/s is the speed. Speed is just a unit and a time frame. Velocity is a unit, a timeframe of travel for that unit and a direction, expressed as a normalized (Length of one/Unit Length) vector who's magnitude is the speed. That portion was about taking the direction of the vector, (1,0), and then making the magnitude one. If the vector were (10, 0), then multiplying it by 20 m/s (Read "Applying the speed to the direction to create a velocity) would give us a bullet traveling 200m/s rather than 20 m/s. It is a bit confusing first time through, but I love how much better he does compared to how other things handle it. Like algebra class.
Reply Share

Avatar

Xeirx es

4 years ago
8/16

blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

14/10/13 Avatar

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

I was not aware of how useful the dot and cross products were :) I always thought that they were the harder to use functions. I guess now I can imagine how useful they would be in physics applications as well. A falling object bouncing off of a slanted surface, for instance, could be easily calculated with the cross product. Thanks for the interesting read! I'm looking forward to seeing more from you guys in the future.
1 Reply

Share

Avatar

abreu20011

5 months ago

Thaaaanks you!!! :D It's very, very useful your post :)


Reply Share

Avatar

V lOlz E lx enoaniz d

5 months ago

There's a little mistake you made in the Cross Product section. C-A is edge_2 not edge_1. Love what you're doing. Wish school teachers would be like you, everybody then would love math! :-)
Reply Share

Avatar

Tom S t erk enburg Reply

9 months ago

not only do i understand the math now, but i dont have to write my own functions lol
Share

Avatar

A bdul

2 years ago

What will you recommend for further reading, I found this wonderful and amazing, I am very keen to learn more on game programming and linear algebra. please recommend some books for us. thanks
Reply Share

Avatar

A bdul Mat een

2 years ago

What do you recommend for further reading, I found this astonishing and wonderful, I am very curious to learn more, can you recommend any book ?
Reply Share

Avatar

Romok u

2 years ago

The cross product is given by the determinant of i j k |x1 y1 z1| = i|y2 z2| -j|x2 z2| +k|x2 y2| |x2 y2 z2| |y3 z3| |x3 z3| |x3 y3| |x3 y3 z3| Simplify: = (y2*z3-z2*y3)i - (x2*z3-x3*z2)j + (x2*y3-x3*y2)k
Reply Share
9/16 blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

14/10/13

Reply k ay o

Share

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

Avatar

2 years ago

Direction vectors are normalized which seems logical, but then why do you use (2,1) in the Boat example as the Direction Vector ?
Reply Share

Avatar

Ugk wan

3 years ago

Thanks^^
Reply Share

Avatar

daniellla

3 years ago

thanks a lot :-)


Reply Share

Avatar

Mohamed idris

4 years ago

Hi everyone plz I'm new here i mean it's my first year in the university so i want to understand this subject cause it's very important for me really cause i didn't understand anything in the university so if anyone can explain it to me from begging i will be thanks full for him or her this is my email:maed_89@hotmail.com
Reply Share

Avatar

uk uk

4 years ago

Fantastically well explained. Thumbs up! Everything suddenly became clear and simple... Looking forward to reading the next part.
Reply Share

Avatar

P et e

4 years ago

AWESOME post. This is the best I have seen covering vector math with good examples. More more more plz
Reply Share

Avatar

Oliver

4 years ago

Really keen for Vector Spaces and Matrix Transformations. You have a great way of explaining things to people. Is there somewhere I can learn the basics of Matrices? Recommendations anyone?
Reply M Share

Oliver 4 years ago

The library is a good place to start, this article is a rare find in its quality. Books are generally better than most internet resources.
Reply Share

Avatar

S ophie Houlden

4 years ago

Just as motivation to continue with these in case you need it, I was just in need of a way to get diagonal movement to move at the right speed, and then I remembered this post and it
blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/ 10/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

made it extra easy for me :D keep it up david, my input scripts will forever be a paragraph shorter :D
Reply Share

Avatar

k ons nos

4 years ago

I must say, I couldn't understand it at first, and reread it many times (got confused by the absence of coordinations in images), but now I thing I can catch on. Keep it up.
Reply David Share
konsnos 4 years ago

Mod

I added some labels to the grid lines -- please let me know if that helps!
Reply k ons nos Share
David 4 years ago

Yes, very helpful. Thank you.


Reply David
Mod

Share

konsnos 4 years ago

Sorry about that, I kept moving the origin around to try and keep the images from wasting too much space. Maybe I should label the grid lines next time!
Reply Share

Avatar

gs t amp

4 years ago

Excellent. More of this thanks. Most of the articles on this stuff have overly technical explanations. This wasn't hard to follow at all.
Reply Share

Avatar

Rory

4 years ago

A future topic I suggest is optimisations! For example always compare distance squared values instead of distance. Using distance squared saves doing a computationally expensive square root which is quite a saving when you are doing hundreds of thousands a second.
Reply David Share
Rory 4 years ago

Mod

That's a good idea: I always use distance squared for comparisons, as well as only normalizing vectors when necessary, and generally avoiding sqrt()s.
Reply Share

Avatar

Mas handar

4 years ago

Thank you so much for that! I always wondered how the normal fit into things and how the cross product worked, and you've explained it fabulously! I'm looking forward to future posts
Reply Share
11/16

blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

Avatar

W hit erabbit

4 years ago

I would just like to say thank you David. I'd like to think my self as a game developer (although only really got my toes in the pool so far) And I'm not the greatest with math (shot in the foot right ?) this whole look at vectors in games is amazingly understandable for me. They totally need to teach math in terms of game design as a staple :D
Reply Share

Avatar

A JS

4 years ago

Great article! I was feeling a bit rusty on these topics until this little gem; can't wait to read the next part! Btw, small typo in the post-- I think the code should read: //Check angle float angle = acos(dot(guard_facing_normalized, guard_to_hero));
Reply Share
4 years ago

Avatar

jamemc m03

I know most of this from A-level mathematics but you really are great to make posts like this. It encourages people to learn mathematics and shows them the reality of programming basic games. If only they did this in schools.
Reply Share
jamemcm03 4 years ago

t imidt eac her Reply

They usually do. You just forgot.


Share

Avatar

A hruman

4 years ago

= acos(DB) should read = acos(DV). Well, acksherly, it should read = acos(D' V') or = acos(D V). Why yes, I am a typography geek. Also, your sailing example is utterly wrong. Sails dont work like that. ;-) Keep it up, though!
Reply John
Mod

Share
Ahruman 4 years ago

David was referring to the angle that would maximize the surface area of the sail presented to the wind. He never claimed to be calculating the optimum sail trim angle for maximizing boat speed. Though catching as much wind as possible as David suggested is definitely optimal if you are running dead down wind.
2 Reply Share
ago

David M o d Ahruman 4 years blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

12/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

David

Mod

Ahruman 4 years ago

You got me, I don't know anything about sails or math typography :) Fixed the error you noticed, good catch!
1 Reply Share

Avatar

Net

4 years ago

Thanks a lot for this. Looking forward to the next part, it's what I need to learn most.
Reply Share

Avatar

Raz Zz iel

4 years ago

Should't S be WxM instead of MxW? According to the Right Hand Rule (http://en.wikipedia.org/wiki/C... S'=MxW would face the opposite direction.
Reply Share
RazZziel 4 years ago

G. Muds k ipper

I think the reason the cross products are backwards is because these examples use a left-handed coordinate system (X east, Y up, Z north). If Y was north and Z was up, the Right Hand Rule would apply.
Reply Share
G. Mudskipper 3 years ago

K ip Robins on

Is a left-handed coordinate system common in the game industry? (I know I'm a year late, so maybe I won't get an answer, but oh well.)
Reply Share
Kip Robinson 2 years ago

S hay ne B ut t s

I'm a year late, too :D Left hand coordinate system is common for world coordinates. Screen coordinates are a bit different. The Y axis is pointed downward and the Z axis points into your face (I think, could be the other way).
Reply Share

Avatar

Ragdolls oft

4 years ago

Be sure to check that distance is not 0 when you normalize, or you'll get some hard to find bugs that happen every once in a while.
Reply David Share
Ragdollsoft 4 years ago

Mod

That's a good point, it can be frustrating to track down why your vectors are becoming INF or NaN!
Reply Share

Avatar

mat t o1990

4 years ago

A nice post. I like the addition od code in this post :)


blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/ 13/16

Avatar
14/10/13 Linear algebra for game developers ~ part 2 - Wolfire Games Blog

Reply

Share

Load more comments

Su b s cri b e

Ad d D i s q u s to yo u r s i te

blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

14/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

Subscribe Email Updates


Sign Up

Search
Search

Categories
Alpha Art Business Design Tour Game Design Game Tech Other Overgrowth Pre-Overgrowth Press 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
blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/ 15/16

14/10/13

Linear algebra for game developers ~ part 2 - Wolfire Games Blog

YouTube 2013 Wolfire Games

blog.wolfire.com/2009/07/linear-algebra-for-game-developers-part-2/

16/16