• Advertisement

Archived

This topic is now archived and is closed to further replies.

4 * 4 Matrices

This topic is 5026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What are 4 * 4 matrices and when/why would I ever use them? In my book, 'Programming RPGs With DirectX' it says that matrices 'make the math easier'. (sarcasm) Gee thanks! That helped, and I now totally understand how to use them! (/sarcasm) How can only 16 nodes control a whole world of verteces, and howcome I need those imaginary matrices, or matrices that store color information (why not just RGB?)....? The true general first seeks victory, then seeks battle - Sun Tzu [edited by - Drakkcon on May 9, 2004 10:55:24 PM] [edited by - Drakkcon on May 9, 2004 10:55:50 PM]

Share this post


Link to post
Share on other sites
Advertisement
Your ''world'' is made up of vertices. All your vertices will be multiplied by a matrix at some point. A matrix multiplied with a vertex results in a new vertex. If you want to rotate your entire world 90 degrees you must rotate all the vertices of your world by 90 degrees. You can do this by multiplying all of your vertices by a rotation matrix. A rotation matrix is a matrix with a specific set of numbers in each position. All vertices mutltiplied by this matrix will result in a 90 degree rotation.

If you think about it you can now define your camera as a matrix, rotate your camera 90 degrees and your world rotates 90 degrees. You can do much more but that''s the basic idea, you use matrices to manipulate (move, rotate, scale, project{don''t worry about this one}) a group of vertices.

Also, matrices don''t store colour information as far as I know.

You might try reading a game programming book with a linear algebra section, a linear alegbra textbook, or taking a class.

------------
- outRider -

Share this post


Link to post
Share on other sites
Thank you. So:

4567
1234
5647
8567

if the camara turned right the vertices would be multiplied by three? I''m afraid I still have no idea. I''m too young to attend a class, but really want to understand this.

The true general first seeks victory, then seeks battle
- Sun Tzu

Share this post


Link to post
Share on other sites
I haven''t really learnt matrices properly yet either, but I know what they are used for and do, so this might make a bit more sense to someone like yourself.

Imagine a matrix as a magic box which takes each vertex and performs a transformation on it, be in scaling, rotation or translation. Kinda like a function, actually (no doubt the maths buffs will severely harm me for saying this, but it makes sense to me).

When you put each vertex into one of these boxes, the world matrix, it takes your model''s local co-ordinates (the model''s center is based around the origin) and puts the vertices into the world.

The second magic box, the view matrix, takes these vertices, which are now in the world, and puts them in front of the camera.

The third magic box, the projection matrix, is kinda hard to explain. It defines how the 3D co-ordinates appear on your 2D screen. For example, you''d use this matrix to do a fish-eye style of view.

Sorry if I sound patronizing, but this is the best way for me to think of it.

If you want, and if you''re using DirectX, you can use the D3DX library to set up your matrices for you, all you need to do is define where you want them to put your vertices in 3D space, what scale, what rotation, the maximum viewable distance, and view angle. For more information, see MSDN archives, your DirectX documentation, or someone else''s code or tutorials.

Stay Clausal,

Red Sodium

Share this post


Link to post
Share on other sites
Thanks everyone (especially red_sodium) for explaining it to me. Here''s how I understand it now:
The world matrix for instance, takes each vertex, and applies the closest number it has to that particular vertex (based on its position), and it takes that value and runs it through a function to determine a vertex''s transformation according to the number. Now I see how they''re useful, you don''t have to perform calculations on each vertex, just have them automatically assign to a value in the matrix, and be changed correctly. Thanks again!

Share this post


Link to post
Share on other sites
I think you've misunderstood something here.
quote:
The world matrix for instance, takes each vertex, and applies the closest number it has to that particular vertex

What closest number? I don't fully understand what you mean but I'm quite sure you have it wrong (no offense meant, I was quite confused myself in the beginning).
quote:
Now I see how they're useful, you don't have to perform calculations on each vertex

No, you still have to perform the calculations (matrix-vector multiplication) on each vertex. I think the most usefull thing about matrices is that you can combine different transformations into one matrix. Thus, for example, you first combine the rotation and projection matrices into one and apply that on each vertex as opposed to transforming each vertex two times. (You could do this without matrices, too, but matrices make representing stuff easier.)

Let's take an example. You have a vertex at (1,2,3). You want to first move it one unit to the right (positive X axis) and then two units up (positive Y axis). Here are the according matrices:

Move one to the right:
[1 0 0 1]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]

Move two units up:
[1 0 0 0]
[0 1 0 0]
[0 0 1 2]
[0 0 0 1]

To combine these transformations we multiply the matrices:
[1 0 0 1][1 0 0 0] [1 0 0 1]
[0 1 0 0][0 1 0 0] = [0 1 0 0]
[0 0 1 0][0 0 1 2] [0 0 1 2]
[0 0 0 1][0 0 0 1] [0 0 0 1]

Now, since we use 4x4 matrices, we must represent the vertex
with a 4x1 matrix. We'll fill the last component with a '1'
(search for homogenous coordinates to find out why). To apply
the transformation to the vertex we multiply the transformation
matrix by the vertex matrix:
[1 0 0 1][1] [2]
[0 1 0 0][2] = [2]
[0 0 1 2][3] [5]
[0 0 0 1][1] [1]

And so we have a new vertex.

That wasn't much of a good example but you should get some idea about it. I'm quite sure the tutorials in the GameDev.net resources section are better at explaining this (see my last post for the links).

EDIT: Fixed a bug

[edited by - nonpop on May 10, 2004 5:50:14 PM]

[edited by - nonpop on May 10, 2004 5:52:17 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Don''t look at matrices as some kind of lookup tables, because they aren''t. Look at them as a set of "directions" or vectors specifying up, right, and forward, plus position and some perspective crap that you don''t use much At least that''s a good way to view them as a newbie.

Share this post


Link to post
Share on other sites
Quoting myself...
quote:
I think the most usefull thing about matrices is that you can combine different transformations into one matrix.

Also, D3D and OpenGL use matrices for transformations. So, at least you have to build the matrices and feed them to the API.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement