PDA

View Full Version : I'm not The One ...



SomeGuy
10-20-2003, 05:29 PM
Hi all!

I've got a problem.
Before I can tell you what the problem is, there's something you should know about me.

I'm a guy that doesn't take peace at using something ofwich I don't understand how it works.
I've being living with that lifestyle for quite some time.

However there's one thing I never fully understoud.
And those are matrices.

I know how to use them, and I know how to fiddle with them to make them do their magic, but there are some parts of simple matrix algorithms I don't fully understand the 'why it works' behind them.

The thing I'm having problems with, is this;

To apply a matrix M to a Vector XYZ you should do this;




M = |M11,M12,M13,M14|
|M21,M22,M23,M24|
|M31,M32,M33,M34|
|M41,M42,M43,M44|

newX = (X*M11)+(Y*M21)+(Z*M31)+M41
newY = (X*M12)+(Y*M22)+(Z*M32)+M42
newZ = (X*M13)+(Y*M23)+(Z*M33)+M43


My question is simple;
Why am I adding an X, Y and Z coordinate together (multiplied with a scalar in the matrix) to get, for example, my new X coordinate?

Where's the logic in that?
The last value added to to the equation, in thise case M41, M42 and M43 are the Translation to add to any vector to be transformed by the matrix. That's pretty simple to grasp; Add a number to the axis of a vector and it will be moved by that amount on that axis.

To explain the thing i'm having trouble understanding with in another way;
I know that if I want to translate my point on the X axis my moving it 5 units, that I do this;

newX = (X*1)+(Y*0)+(Z*0)+5
newY = (X*0)+(Y*1)+(Z*0)+0
newZ = (X*0)+(Y*0)+(Z*1)+0

My question could also be stated as:
"What happens when I multiply multiple axes by 1 to get a new axes?"

Hope you lived through my inane babbling to enrich my feeble deteriorating mind with your awnsers.

http://www.opengl.org/discussion_boards/ubb/tongue.gif

SirKnight
10-20-2003, 06:01 PM
Learn how to multiply a matrix by a vector (which is also a matrix) and this will be clear to you. Also I'd suggest re-reading exactly what a matrix is. This is just simple matrix math.

Here's a start: http://mathworld.wolfram.com/

-SirKnight

[This message has been edited by SirKnight (edited 10-20-2003).]

rgpc
10-20-2003, 06:07 PM
This is not an advanced question.

Get yourself a copy of the Red book. It describes the effect on the Matrices each transformation/rotation/scale has and will help you to better understand why you are adding x/y/z values together.

The simple answer to your question of "why" is "because that is how you multiply matrices".

One way to think of it is given you have rotations in your matrix. If you have a point on the X axis (say 1,0,0), and you rotate it by 90degs around the Y axis, you get a point that is on the Z axis.

Because of this it's fairly obvious that the equation that performs this rotation must contain elements of both X & Z. From memory the equations to perform the above rotation is something like P'.z = (P.x * sinA) - (P.z * cosA) and P'.x = (P.x * cosA) - (P.z * sinA).

This follows for the other rotations as well. A Matrix is just a better way of representing these equations, and of including transformation and scaling in one neat package. Plus it ends up being more efficient and versatile.

Tom Nuydens
10-20-2003, 09:54 PM
http://www.flipcode.com/geometry/

-- Tom

tfpsly
10-20-2003, 11:55 PM
Why am I adding an X, Y and Z coordinate together (multiplied with a scalar in the matrix) to get, for example, my new X coordinate?

A X, Y or Z coordinates is not defined absolutely, it is always define in a vector base (or is it basis in English ?), that is in a coordinate system.

A coordinate system is a point and 3 vectors (X, Y, Z vectors). So when you have for example a vector v = (1,2,3), that means in fact that your vector is:
v = 1*X + 2*Y + 3*Z, whatever the current X, Y, Z axis are.

When you multiply a vector by a matrix, what you really do is changing the current vector base: you say something like "the X vector is no more (1,0,0), but (a,b,c) in the new coordinate system".
So you rotate your v vector doing a vector-matrix multiplication :
v = 1*(a,b,c) + 2*(d,e,f) + 3*(g,h,i)
and then you add the translation component.

That's all a matrix is in 3D engines: a way to convert vectors from one coordinate space to another one.

Dodger
10-21-2003, 09:57 AM
It may also be of help to view a vector * matrix multiplication as just a series of 4-component dot products. if you view a transformation matrix as the combination of the basis vectors of the transformed coordinate system the matrix represents and look at each column of the matrix as a unit vector, then it suddenly all makes sense:

x' = v . col1;
y' = v . col2;
z' = v . col3;

think about what the dot product does - it represents the linear projection of one vector onto the other. Hence, for the dot product of your source vector and the basis vector representing one coordinate axis of the transfromed coordinate system, the result will be? Right. The projection of that vector onto the basis vector, which is a representation of the respective coordinate of the transformed vector.
If you look at 4-component vectors and 4x4 matrices as well, i t becomes clear how translation can easily be incorporated by using 1.0 as the vector's w coordinate and inserting the translation into the 4th row of the matrix.

deshfrudu
10-21-2003, 10:39 AM
Don't think in terms of matrices. Think in terms of measuring a vector along another vector.

For example, if you have a vector sitting on the table, call it A, and you get out your tape measure in the direction of X, how long is it? You can rotate your tape measure and the length of A will be different along that direction. If you rotate it completely perpendicularly to A, A will measure 0 in that direction. This is the dot product. And so, a matrix is three tape measures (usually mutually perpendicular to each other). Transforming a vector by a matrix is finding it's measurements along all three axes (hence three dot products). The first result, "x", is it's length along the "X" tape measure, but to get it requires consideration of the both whole vectors (x, y, and z).

Hope that helps.

dorbie
10-21-2003, 03:15 PM
He's asking for a derivation or proof, he already knows the how.

You may find this useful for giving you a taste of how these matrices are derived although it doesn't cover matrix theory itself:
http://www.cs.unc.edu/~hoff/projects/comp236_ta/rotations/rotations.pdf

Matrices weren't invented for 3D graphics it's a mathematical field invented by Arthur Cayley, that's used by graphics programmers to implement the 3D equations, mainly by (once you cleverly decide you need a vector transformed using a matrix) writing down the equations and saying to yourself, ok this is the equation for x so if I want to multiply x by these terms in my matrix transform I'd better put them in this matrix field, and if I want to add this term I must put it that field etc.

Once you understand this you can easily understand what the equations in sources like the red book are explaining.

The link I provided has a simple rotation but it's a similar deal for the other more complex transformations that use larger matrices (and vectors). The translation term is simple and obvious. The trickiest part part that may be less clear is the divide by z of your typical perspective projection. To accomplish this with a matrix multiplication homogeneous coordinates that have a w = 1 and an implicit divide by w are used to get a divide using a matrix multplication. So to divide by a term you make sure that you multiply w by that term during the transformation through the matrix.

This will explain homogeneous coords and may make the above a bit clearer:
http://www.alumni.caltech.edu/~woody/docs/3dmatrix.html

For OpenGL this is essential reading:
http://research.microsoft.com/~hollasch/cgindex/math/matrix/column-vec.html

Beyond this matrices have other nice properties under concatenation that make them ideal for 3D graphics applications.

dorbie
10-21-2003, 06:06 PM
P.S. here is another document, this time on deriving the Projection Matrix, although he doesn't actually throw the equations into the matrix at the end.
http://www.cs.unc.edu/~hoff/techrep/perspective.doc

He starts by showing the matrix, then deriving the equations for x, y and z. You should then be able to see how the fixed terms of that equation are placed directly into the matrix to implement the equation (fixed terms as operands either of multipliers, additions or divisors of vector (vertex) components), he doesn't show this part but you can easily match the major terms of the equations. You just need to understand that each location in the matrix represents operands and operators based on the math of the matrix vector product. Refer back to the matrix on the front page and remember that to divide you multiply w by the divisor (see earlier ref), this will help you to see that the -1 in the matrix is actually -1 * z which then gets used to multiply w to give the divide by -eyez in the equations, the rest is fairly obvious.

[This message has been edited by dorbie (edited 10-21-2003).]