4 * 4 Matrices

Started by
14 comments, last by Drakkcon 19 years, 11 months ago
Okay, start with the vector dot product operation

Given two vectors, V1 and V2, the dot product
V1 . V2 = (V1x*V2x + V1y*V2y + V1z*V2z) 


Note that the dot product returns ONE value. It''s a scalar, not a vector. So
(1, 0, 1) . (2, 0, -1) = 2*1 + 0*0 + 1*-1 = 2-1 = 1. 

I''m going to start by looking at 3x3 matrices, for simplicity.

Take a matrix
|A B C||D E F||G H I| 


This can be thought of as three vectors:
(A, B, C)(D, E, F)and(G, H, I) 


What a 3x3 matrix multiplication is is simply taking three dot products.

So multiplying the matrix above (M) by a vector V you would be doing (this is written as a 3x3 matrix multiplied by a 1x3 matrix):

|A B C| |Vx||D E F| |Vy| = (V . (A,B,C), V . (D,E,F), V . (G,H,I))|G H I| |Vz| 


Note that each output component (Say the output vector is called P)

Px = V . (1st row of matrix)
Py = V . (2nd row of matrix)
Pz = V . (3rd row of matrix)

These rows can be though of as axes (like X, Y, and Z). So if you have a matrix that''s
|1 0 0||0 1 0||0 0 1| 

then multiplying it and V together would return V.
This matrix is called the Identity Matrix, because vectors (and other 3x3 matrices) multiplied by it are unchanged.


A 3x3 matrix can represent many things, but it''s mostly used to represent rotations.

What can a 3x3 matrix NOT represent? Translation (moving a point in space) and perspective projection (making objects farther from the origin get smaller to simulate distance).

So we use 4x4 matrices.

But because we use 4x4 matrices, we need to have a 4-element coordinate, too, so a fourth coordinate is added (w).

S our vectors are written as (x, y, z, w).

These are homogenous vectors. Generally, you don''t have to deal with them, they''re implied. When you take a vector that''s a 3-part vector (x, y, z) and make it a 4-part vector, w is 1.
(x, y, z, 1). Don''t worry about the reason right now, it''ll come up later.

So now you have a 4x4 matrix. They''re generally arranged like this (for a rigid transform):

|R R R T||R R R T||R R R T||0 0 0 1| 


In this, the "R"s are where the rotation information is (note that it''s the 3x3 portion of it), and the "T"s define the translation.

Remember, each component of the vector is dot-producted (dot-productized? I dunno) with it''s corresponding row in the matrix (1st component (x) dot 1st matrix row, etc).

Perspective matrices get a little bit more tricky. I''m just going to touch them for now. The basic idea behind perspective is (simply), making
P = [x/z, y/z, 1/z, 1] 


That is, x and y (given the center of the screen is (0,0)) are divided by the distance from the screen. However, in a matrix multiplication you can''t do a divide directly, so you cheat by using the homogenous coordinate (w).

Yes, that little bastard. The basic idea is this:

if you multiply all of the (4) coordinates by any number (except 0, of course), the vector stays the same.

So (1, 2, 3, 1) = (2, 4, 6, 2) = (3, 6, 9, 3), etc. And all of those four-component vectors are the same as (1, 2, 3), the three-component vector.

Confusing? Course it is.

Basically, you can take whatever you have (using the (2, 4, 6, 2) example above), and divide everything by the w component (2) to get the three-component version.

So (x/w, y/w, z/w) is the three-component vector.

You''d get (2/2, 4/2, 6/2) = (1, 2, 3). See?

So the perspective matrix accomplishes the division by making the 4-vector:

(x'', y'', z'', w'') = (x, y, 1, z). Thus, doing (x''/w'', y''/w'', z''/w'', w''/w'') gets you (x/z, y/z, 1/z, z/z) = (x/z, y/z, 1/z, 1).

Which is exactly what you wanted.

---

Whew. That''s the worst of it. Now, why use 4x4 vectors?

Simple. You can represent almost any type of transformation you can think of in ONE 4x4 matrix. If you take two matrices and multiply them together (it''s a similar operation to matrix * vector), you get a combined transform. So Rotation * Translation would make one single matrix that''s (Rotation And Translation).

Etc. All, then, transforming a single point takes would be four 4-component dot products (x1*x2 + y1*y2 + z1*z2 + w1*w2), done four times, one for each row of the matrix.

Then you run that output through the "/w" routine (x/w, y/w, z/w) to get the actual output coordinate.

Complex? Yes.

But once you understand how matrices work, they''re an invaluable tool.

Hope you get some of that, and I hope I didn''t mess up anything too badly in there Don''t forget to ask questions about things you don''t understand, I''ll explain further (unless someone else beats me to the punch)!

Good luck,


Josh
Advertisement
Take a look at a linear algebra book.

Matrices store linear transformations. Rotation or dilation are examples of linear transformations. Translation is not a linear transformation.
To handle translations with matrices one has to add an extra dimension to all calculations.

4x4 matrices are a convenient way to represent translation + rotation-dilation in a 3d-world.
Thank you drillian, it''s a little clearer. I''m fine with multiplying them, I learned that in my 8th grade Algebra 1 class (maybe it''s the age....) I still don''t see why transformations have to be represented my something like this. What IS a transformation? Can''t everything about the vertex be defined when you create it? How do you lay a matrix over a vertex (apply it). Exactly what do the values DO to the vertex? Which value does what? Also, my book mentioned matrices that don''t effect anything. The idea was to multiply these by another matrix when you HAD to multiply something. Why would I need a matrix that doesn''t effect anything? It''s like multiplying by 1! I think they were called identity matrices.
Hmm, give me a crack at it (although you should just pick up a linear algebra book.)

A transformation does what it sounds like, it transforms.

Say you have some vertices, and you want to move them (translate). You can:

Go through all of them and add to the x,y,z values in order to move them all in the same direction.

Now let's say you wanted to rotate them, well you could:

Go throgh all of them and multiply them by the trig rotation formulas to rotate your vertices.

Now let's say you want to scale them? ...

and so on and so on...

What you see is that for each transformation (move, scale, rotate) there are different methods needed. You have to do the first, then the second, then the third But... a matrix multiplication (multiply a matrix by a vector) can represent all these transformations. (If you want to know how read above) So why use this representation? Because each transformation is now one operation, matrix*vector

you can have trans_matrix*vector or scale_matrix*vector and so on..

but there's more. you can multiply these matricies to create one matrix that does all three. Now you don't have to have 3 operations per vertex, just multiply this uber one. So it's always just one matrix multiply per vertex? Why transform? How can you move in a scene? In actuallity, if you move left, you really just move all the points to the right. If you turn left, you actually just turn all the points to the right. Etc... Why have an identiy, it's useful for re-setting things. Suppose you wanted to move all points to the left, but then you want to move them to the right. First you have to reset your matrix, set it as the identity, now you can work with it again.





[edited by - ataru on May 13, 2004 11:05:06 AM]
That''s it, perfect post! I understand them now - thank you ataru.
You are making the same mistake I made. Stay in 2D graphics for now. (something like tetris or galaga) Add vectors when you completely understand them to make an astroids game with 360 degrees of motion. THEN study trig, and linear algebra to see what a matrix REALLY is (not in terms of game programming) Then you''ll see why they are useful. Baby steps man. I tried doing what you are doing and it didn''t go over until i took the time to learn it all correctly
I love me and you love you.

This topic is closed to new replies.

Advertisement