### Linear Algebra: Math for threedees

Posted:

**Wed May 12, 2010 12:08 am**Linear Algebra is the maths that games use to do cool 3D stuff. Its easy to understand and will blow your mind with awesomeness.

Vectors: This is a way to measure 3D directions/distances. A 3D vector is written down like this: <2,3,4>. Each number stands for a different dimension (x,y, or z). This is also the exact same way to store a 3D position, but a vector is different. A vector represents a direction (right, left, up, down, somewhere in-between), and a distance (5 feet). If you walked 3 feet forward, you would write that vector like this: <0,0,3>. If you walked up some stairs, and in doing so you happened to have moved 3 feet forward and 2 feet upward, you'd write that like this: <0,2,3>. A vector is measured from the universal center of the universe, "the origin" (that's <0,0,0>). This way of writing down directions and distances is handy because there are lots of tricks you can easily do with it.

tip: imagine a vector as an arrow, starting at the origin, and ending at its components.

More: http://en.wikipedia.org/wiki/Euclidean_vector

Adding Vectors: If you walk two feet forward, and then two feet to the left, you'd be adding two vectors together. That looks like this: <0,0,2> + <0,2,0> = <0,2,2> (just do normal addition to each component separately). If you layed an arrow down for each of the two movements in the example above, the second movement's arrow would start at the end of first arrow. When you add two vectors together like this, you can imagine the resulting vector by laying down the arrows end to end like that.

More: http://en.wikipedia.org/wiki/Euclidean_ ... ubtraction

Subtracting Vectors: If you and your friend went home from school, and you walked 1 mile north and he walked 1 mile west, what would be the vector between you two by the time you both got home? That looks like this: <0,1,0> - <-1,0,0> = <1,1,0> (just do normal subtraction to each component separately). If you layed an arrow down for each you and your friend starting at the school, a subtraction can be imagined by drawing another arrow between each of the end points, with the arrow pointing toward the "first" one. E.g, if you subtracted vectors A and B (A-B), the arrow would point toward A's end point.

More: http://en.wikipedia.org/wiki/Euclidean_ ... ubtraction

Vector Length: A vector stores both "direction" and "distance", but you can't just look at a vector to see how long it is, cause its three numbers! Since all of the components (x,y,z) are at right angles to each other, you can use that old fart Pythagorean to get something out of it. length = sqrt(x*x+y*y+z*z)

More: http://en.wikipedia.org/wiki/Euclidean_vector#Length

Normalization: A vector stores both "direction" and "distance", but sometimes you don't want the distance part. In this case, you find a vector with a length of 1, aka a unit vector. This is called normalizing a vector (not to be confused with a polygon normal vector). To do this, find the length of the vector, and then divide each component by that length: l = length(V); unitVector = <Vx/l,Vy/l,Vz/l>.

More: http://en.wikipedia.org/wiki/Euclidean_ ... nit_vector

Multiplying Vectors: Vectors are a bit more complicated than scalars. There's three different things you can do that are kind of like multiplication: scaling, dot product, and cross product.

Scaling: Say you would like to have a vector that is exactly in the same direction as another vector, but is twice the length. You scale the source vector. This is the easiest kind of multiplication-type thing: scaledVector = <Vx*scale,Vy*scale,Vz*scale>.

A nice trick is if you want to find a vector that is in the same direction as another vector, but exactly so many units long, normalize the source vector (bringing its length to 1) and then scale it to how long you want it to be.

More: http://en.wikipedia.org/wiki/Euclidean_vector#Length

Dot Product: A dot product is a computationally fast operation that indicates how close in direction two vectors are. If you imagine two vectors starting at the same origin, the dot product gets the shortest angle between them. Well ... not exactly the angle, but in fact the arc-cosine multiplied by both of their lengths. But basically, what this means is if two unit vectors are pointing exactly in the same direction, their dot product is 1. If they are at exactly right-angles to each other, their dot product is 0. If they are exactly pointing away from each other, their dot product is -1. If they are somewhere in between, they will be 0.xxx if the angle between them is less than 90, and -0.xxx if the angle is greater than 90. This is surprisingly useful information, since it is basically a "percent" of how close they are. A dot product looks like this: dotProduct = Ax*Bx + Ay*By + Az*Bz. -- Yes, surprisingly simple for so complex of a result. It is definitely a thing of beauty.

More: http://en.wikipedia.org/wiki/Euclidean_ ... ot_product

Cross Product: The cross product is used to find a vector that is exactly perpendicular to two other vectors. Now hold out your right hand in a fist. Just do it fag. Straighten out your thumb and pointer like you're making a gun. Those are two vectors. Now straighten the second and third knuckles of your middle finger (but not the big knuckle). The middle finger is a vector that's perpendicular to the first two -- its the cross product.

Here's what a cross product looks like (Its quiet ugly looking to me):

crossProduct = <Ay * Bz - By * Az, Az * Bx - Bz * Ax, Ax * By - Bx * Ay>

Cross products are used in a variety of ways to construct new vectors. For instance, if you had a triangle polygon, you could get the direction that faces directly up by performing a cross product on two of the edges of the polygon. (You could get those edges by subtracting the vertices from one another). This vector sticking directly out of a polygon is called a normal! We use normals a lot in 3D graphics, so this is a nice trick to know.

More: http://en.wikipedia.org/wiki/Euclidean_ ... ss_product

Next time: How to figure out how long your shadow is.

Vectors: This is a way to measure 3D directions/distances. A 3D vector is written down like this: <2,3,4>. Each number stands for a different dimension (x,y, or z). This is also the exact same way to store a 3D position, but a vector is different. A vector represents a direction (right, left, up, down, somewhere in-between), and a distance (5 feet). If you walked 3 feet forward, you would write that vector like this: <0,0,3>. If you walked up some stairs, and in doing so you happened to have moved 3 feet forward and 2 feet upward, you'd write that like this: <0,2,3>. A vector is measured from the universal center of the universe, "the origin" (that's <0,0,0>). This way of writing down directions and distances is handy because there are lots of tricks you can easily do with it.

tip: imagine a vector as an arrow, starting at the origin, and ending at its components.

More: http://en.wikipedia.org/wiki/Euclidean_vector

Adding Vectors: If you walk two feet forward, and then two feet to the left, you'd be adding two vectors together. That looks like this: <0,0,2> + <0,2,0> = <0,2,2> (just do normal addition to each component separately). If you layed an arrow down for each of the two movements in the example above, the second movement's arrow would start at the end of first arrow. When you add two vectors together like this, you can imagine the resulting vector by laying down the arrows end to end like that.

More: http://en.wikipedia.org/wiki/Euclidean_ ... ubtraction

Subtracting Vectors: If you and your friend went home from school, and you walked 1 mile north and he walked 1 mile west, what would be the vector between you two by the time you both got home? That looks like this: <0,1,0> - <-1,0,0> = <1,1,0> (just do normal subtraction to each component separately). If you layed an arrow down for each you and your friend starting at the school, a subtraction can be imagined by drawing another arrow between each of the end points, with the arrow pointing toward the "first" one. E.g, if you subtracted vectors A and B (A-B), the arrow would point toward A's end point.

More: http://en.wikipedia.org/wiki/Euclidean_ ... ubtraction

Vector Length: A vector stores both "direction" and "distance", but you can't just look at a vector to see how long it is, cause its three numbers! Since all of the components (x,y,z) are at right angles to each other, you can use that old fart Pythagorean to get something out of it. length = sqrt(x*x+y*y+z*z)

More: http://en.wikipedia.org/wiki/Euclidean_vector#Length

Normalization: A vector stores both "direction" and "distance", but sometimes you don't want the distance part. In this case, you find a vector with a length of 1, aka a unit vector. This is called normalizing a vector (not to be confused with a polygon normal vector). To do this, find the length of the vector, and then divide each component by that length: l = length(V); unitVector = <Vx/l,Vy/l,Vz/l>.

More: http://en.wikipedia.org/wiki/Euclidean_ ... nit_vector

Multiplying Vectors: Vectors are a bit more complicated than scalars. There's three different things you can do that are kind of like multiplication: scaling, dot product, and cross product.

Scaling: Say you would like to have a vector that is exactly in the same direction as another vector, but is twice the length. You scale the source vector. This is the easiest kind of multiplication-type thing: scaledVector = <Vx*scale,Vy*scale,Vz*scale>.

A nice trick is if you want to find a vector that is in the same direction as another vector, but exactly so many units long, normalize the source vector (bringing its length to 1) and then scale it to how long you want it to be.

More: http://en.wikipedia.org/wiki/Euclidean_vector#Length

Dot Product: A dot product is a computationally fast operation that indicates how close in direction two vectors are. If you imagine two vectors starting at the same origin, the dot product gets the shortest angle between them. Well ... not exactly the angle, but in fact the arc-cosine multiplied by both of their lengths. But basically, what this means is if two unit vectors are pointing exactly in the same direction, their dot product is 1. If they are at exactly right-angles to each other, their dot product is 0. If they are exactly pointing away from each other, their dot product is -1. If they are somewhere in between, they will be 0.xxx if the angle between them is less than 90, and -0.xxx if the angle is greater than 90. This is surprisingly useful information, since it is basically a "percent" of how close they are. A dot product looks like this: dotProduct = Ax*Bx + Ay*By + Az*Bz. -- Yes, surprisingly simple for so complex of a result. It is definitely a thing of beauty.

More: http://en.wikipedia.org/wiki/Euclidean_ ... ot_product

Cross Product: The cross product is used to find a vector that is exactly perpendicular to two other vectors. Now hold out your right hand in a fist. Just do it fag. Straighten out your thumb and pointer like you're making a gun. Those are two vectors. Now straighten the second and third knuckles of your middle finger (but not the big knuckle). The middle finger is a vector that's perpendicular to the first two -- its the cross product.

Here's what a cross product looks like (Its quiet ugly looking to me):

crossProduct = <Ay * Bz - By * Az, Az * Bx - Bz * Ax, Ax * By - Bx * Ay>

Cross products are used in a variety of ways to construct new vectors. For instance, if you had a triangle polygon, you could get the direction that faces directly up by performing a cross product on two of the edges of the polygon. (You could get those edges by subtracting the vertices from one another). This vector sticking directly out of a polygon is called a normal! We use normals a lot in 3D graphics, so this is a nice trick to know.

More: http://en.wikipedia.org/wiki/Euclidean_ ... ss_product

Next time: How to figure out how long your shadow is.