Archived

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

Max_Payne

Matrices

Recommended Posts

So there are alot of tutorials about rotation matrices, but what about transform matrices in general. How do you calculate a translation or a scaling matrix. Can you simply multiply translation, rotation and scaling matrices together to produce a single transform matrix? Why do OpenGL and D3D use 4x4 matrices? Wouldn''t it be simpler to do this in 3x3?

Share this post


Link to post
Share on other sites
Translation matrix: Take an identity matrix, and put the translation point in the last column.

Scaling matrix: Take an identity matrix, but replace the first three ones with your scaling factors.

Can you multiply: yes. Keep in mind that the order of multiplication is important; the transformations are applied in that order. M1 * M2 is often not the same as M2 * M1.

Why 4x4 matrix: google for "opengl homogeneous coordinates". The basic idea is to represent points and vectors using the same system, and to allow for perspective.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
With 4x4 matrix you can describe rotation, translations and scaling with a matrix*vector mult. You can combine two different transformation by multiply the matrices.
With 3x3 matrix you can describe rotations and scaling but not translation.

Share this post


Link to post
Share on other sites
quote:
Original post by Max_Payne
What would keep a 3x3 matrix to represent translation?


As Sneftel said, the translation is stored in the 4th column. 3x3 means 3 rows, 3 columns. 4x4 means 4 rows, 4 columns.

Share this post


Link to post
Share on other sites
So far in my own 3D software renderer I always used 3x3 matrices and did the translation by adding a vector.

It works very good, but I wonder if this is bad code then? Should I change to 4x4?

Somewhere in the code I have to calculate the inverse of a 3x3 matrix, turning this into the inverse of a 4x4 matrix could become much more complicated, and what happens to the translation part if you calculate it's inverse?

[edited by - Boops on November 15, 2003 2:31:45 PM]

Share this post


Link to post
Share on other sites
the other advantage of 4x4 matricies besides translation is that the projection transformation can be stored in matrix form. to multiply by a 4x4 matrix, a vector must expand to 4 dimensions, its 4th element being a one (identity). After the transformation, the vector is transformed back into a 3 element vector by dividing by the 4th element, which may no longer be a one if matrix modified it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''d get a introductory linear algebra text book, it''s all very well having code to manipulate matrices but so much nicer to understand what it''s doing and why.

Share this post


Link to post
Share on other sites
Yeah, for some reason a lot of books/tutorials/papers seem to describe fundamental 3D topics in very vague or confusing ways. They either tend to simplify things to such a degree that you look at something, like a matrix, as a black box where you put numbers in and, like magic, results pop out. Or, they go the other way and describe it in confusing math jargon, with symbols and greek letters and 50 cent words that almost seem designed to confuse instead of educate.

Anyway, enough of that ranting... I''ll try to do my best to describe what a 3x3 and 4x4 matrix are and how to use them.

NOTE: I''ll talk in terms of a right-handed coordinate system. This means that if you stand at the origin, positive X is on your right, positive Y is up, and positive Z goes behind you. A left-handed system(ala DirectX) would be the same except positive Z would go forward from you.

First of all, regardless of how you''re working, the Identity of a 4x4 Matrix always looks like this:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

Next, it''s good to know that matrices can be either row or column based. Column based matrices tend to be the most common, so I''ll use that for my examples. In a colum-based matrix you put the position in the last column, like so:

1 0 0 Pos.x
0 1 0 Pos.y
0 0 1 Pos.z
0 0 0 1

For row-based matrices, the position''s xyz coordinates would be placed in the first three numbers on the bottom row.

Ok, next we''ll back track slightly to a 3x3 matrices. A 3x3 matrix is typically used to describe rotation and/or scale. The Identity of 3x3 matrix looks like this

1 0 0
0 1 0
0 0 1

Ok great, but let''s stop for a second. What do those 9 numbers actually mean? And how do they store rotation and scale? While I seldom see this mentioned, the answers to these questions are actually very intuitive. Those 9 numbers represent points in 3D map onto each 3D axis(X Y Z). In those fancy math-books, they call these "Basis Vectors" Let''s re-write the matrix in this form:

XAxis.x YAxis.x ZAxis.x
XAxis.y YAxis.y ZAxis.y
XAxis.z YAxis.z ZAxis.z

So, of course, in standard world space(Nothing transformed), the x axis points down the x axis, the y down the y, and the z down the z. Which means that the vector that describes the XAxis is (1,0,0), the Y is (0,1,0), and the Z is (0,0,1). So, now we can see quite easily why the identity matrix looks the way that it does.

Ok, so what does this mean exactly? What it means is that if you take something and want to scale it by 3 along the X axis, that the matrix to describe it would look like this:

3 0 0
0 1 0
0 0 1

If you wanted to mirror something along the Y axis, you''d get a matrix that looks like this:

1 0 0
0 -1 0
0 0 1

Again, since all these numbers do is tell you the direction and length of each axis, it means you can also describe rotation with a 3x3 matrix. So, if you are standing at the origin and you turn 90 degrees to your left then the X axis, from your vantage point, would point down the world''s -Z axis. The Y axis would be the same as the world''s, and the Z axis would now point down the world''s positive X axis. We can represent that 90 degree rotation with this matrix:

0 0 1
0 1 0
-1 0 0

You can combine rotation and scale as well. So, if you did that same rotation and did the same scale by 3 in X that we did above, you''d get this matrix:

0 0 1
0 1 0
-3 0 0

Again, these 3 sets of 3 numbers simply represent how the XYZ coordinates map into another set of XYZ coordinates. In the examples above, we''ve mapped your viewpoint to that of the world.

Now, the intersting thing to be aware of here is that so far we''ve kept each axis'' direction the same relative to the other two axis(except in the mirror on Y example). The fact of the matter is that for most normal transforms this always holds true. However, you can also distort the axis to achieve interesting effects. For example, if you used a matrix where you left the X and Z axis the same as the identity, but bent the Y axis you could skew your object. A matrix to do that would look something like this:

1 1 0
0 1 0
0 0 1

One thing to note in the matrix above is that if you calculate the length of the Y vector(1,1,0) it is not 1. So, in this example, we skewed and scaled instead of just skewing. I just used 1''s and 0''s to make it easier to read

Ok, so that''s how 3x3 matrices work effectively. Just to recap again, a 3x3 matrix is a set of 3 vectors that describe how 3D values in one "space" transform into the 3 vectors in another "space".

Ok, next let''s quickly gloss over something they call "homogenous coordinates". In 3D space, we represent a point as 3 numbers(XYZ). We also represent a vector as 3 numbers(XYZ). However, in reality, vectors and points are not exactly the same thing. A vector describes a direction, but has no position. In contrast, a point has a position but no direction. Normally, although the two look exactly the same when written, we tend to do the appropriate thing with them based on the context we use them in.

However, if we move into 4D homogenous space, then there is a visible difference. In this space, we add a new component "w". A vectors w component is 0, and a point''s w component is 1. So, in this space, if we would describe a point and a vector like so:

Point( 2, 3, 7, 1 )
Vector( 2, 3, 7, 0 )

Also, you''ll notice that in a non-perspective-distorted 4x4 Matrix the bottom row is always ( 0 0 0 1 ). That''s because the bottom row is the "w" component. And since the first three columns are the XYZ vectors, their w component is 0. And since the last row represent''s position, which is a point, it''s w component is 1.

So, now we finally get to the meat of it all. Basically, a 4x4 matrix lets you represent a mapping of each of the 3 vectors(XYZ) as well as an offset position. Like so:

XAxis.x YAxis.x ZAxis.x OffsetPoint.x
XAxis.y YAxis.y ZAxis.y OffsetPoint.y
XAxis.z YAxis.z ZAxis.z OffsetPoint.z
XAxis.w YAxis.w ZAxis.w OffsetPoint.w

When you look at it like that, hopefully you''ll find that it''s actually pretty easy to understand

Well, I didn''t mean for this to get this long, but hopefully it helped!

-John

Share this post


Link to post
Share on other sites
hope i didnt miss that part. the inverse of a 4x4 matrix is still easy, as long you just translate and rotate with it (ie, it should be orthonormal). you can still simply transpose the rotation part (3x3), and the translation part would be [-dot(oldXaxis, position), -dot(oldYaxis, position), -dot(oldZaxis, position)]

and dont worry, its amazing how often you realize obvious things that wouldnt have been anything new if one would have paid attention in all those linear algebra lectures *lol*. simple things like the difference between a local and global transformation being nothing but a question of multiplying it from the left or right side.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Trienco
hope i didnt miss that part. the inverse of a 4x4 matrix is still easy, as long you just translate and rotate with it (ie, it should be orthonormal). you can still simply transpose the rotation part (3x3), and the translation part would be [-dot(oldXaxis, position), -dot(oldYaxis, position), -dot(oldZaxis, position)]

What do you mean by ''orthonormal''? And while I''m at it, what does ''orthogonal'' mean aswell?

Share this post


Link to post
Share on other sites
if you consider your 3x3 rotation matrix as what it is, a collection of 3 vectors, then orthogonal means that the angle between all those vectors is 90°. orthonormal means that in addition they are all unit length.

Share this post


Link to post
Share on other sites