Home » Community » Forums » » Matrix Math
  Intel sponsors gamedev.net search:   
[Control Panel] [Register] [Bookmarks] [Who's Online] [Active Topics] [Stats] [FAQ] [Search]

Add Forum to Favorites |  Send Topic To a Friend | View Forum FAQ | Track this topic


 Last Thread Next Thread 
 Matrix Math
Post Reply 
I am not sure 100%; BUT I think there is a mistake with indexes.


Matrix Matrix_Multiply(Matrix mat1, Matrix mat2) {
int i, j, k;
Matrix mat;

// For speed reasons, use the unroll-loops option of your compiler
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
for(k = 0, mat[i][j] = 0; k < 4; k++)
mat[i][j] += mat1[i][k] * mat2[k][j]; //Wrong
mat[j][i] += mat1[j][k] * mat2[k][j]; //Right
return mat;
}


 User Rating: 1015    Report this Post to a Moderator | Link

Once again just nitpicking a bit:

 // For speed reasons, use the unroll-loops option of your compiler
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++)
for(k = 0, mat[i][j] = 0; k < 4; k++)
mat[i][j] += mat1[i][k] * mat2[k][j]; //Wrong
mat[j][i] += mat1[j][k] * mat2[k][j]; //Right
return mat;
}


Shouldn't that be?

 // For speed reasons, use the unroll-loops option of your compiler
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
for(k = 0, mat[i][j] = 0; k < 3; k++)
mat[i][j] += mat1[i][k] * mat2[k][j]; //Wrong
mat[j][i] += mat1[j][k] * mat2[k][j]; //Right
return mat;
}


If you run until 4 you'll overrun right? [0][1][2][3][4] ; it's a 4x4 not a 5x5. Just making sure; I'm writing my own matrix objects right now for my engine.

-John "bKT" Bellone [homepage] [email]

 User Rating: 1052   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

bkt, nope. When i or j are 4, the conditional expressions (i < 4, j < 4) will fail, so the loop stops.

 User Rating: 2118   |  Rate This User  Send Private MessageView ProfileView JournalView GD Showcase Entries Report this Post to a Moderator | Link

Starts off talking like were newbies ( Which I am). Then starts talking about sin/cos bla bla blah. Completely losses track. Not usefull, sorry.

 User Rating: 1015    Report this Post to a Moderator | Link

Quote:
Original post by Anonymous Poster
Starts off talking like were newbies ( Which I am). Then starts talking about sin/cos bla bla blah. Completely losses track. Not usefull, sorry.


You learn the basics of sin/cos in Geometry and then the matricies in Algebra 2 (US Education Systems in TX at least). For us it was, Alg1, Geom, Alg2, PreCalc, Calc in high school. If you are looking into 3D Math, and do now know your basic trig (the sin/cos blah blah), then I highly reccomend going back and learning what you are missing. In those examples, it clearly shows what you have to do. If you want to rotate about the X axis, then for the X Rotation matrix, given as:
1  	0  	0  	0
0 	cos(x) 	-sin(x}	0
0 	sin(x) 	cos(x) 	0
0 	0 	0 	1

You simply replace X with the radians/degress of what angle you want to rotate by (depending on your math systems), so if you want to rotate by 180 degress (which is PI radians) you will have:
1  	0  	0  	0
0 	cos(3.1415) 	-sin(3.1415}	0
0 	sin(3.1415) 	cos(3.1415) 	0
0 	0 	0 	1


Now you have your matrix! Regardless of your programming API, there should be some math functions that can convert cos(3.1415) to -1 and sin(3.1415) to 0 and so on. That's all there is to it, it's very, very useful. For a conversion from degress to radians take a look at the Unit ciricle (something you should have to memorize in Pre-Calc). Other than that, it's just plug and chug stuff. For sin/cos/trig, all you need to know is SOHCAHTOA.


"But I, being poor, have only my dreams. I have spread my dreams under your feet; tread softly, because you tread on my dreams." - William Butler Yeats

 User Rating: 1933   |  Rate This User  Send Private MessageView ProfileView Journal Report this Post to a Moderator | Link

I'm believe that the function is correct as stated in the article( mat[i][j] +=).
Just look at how the loops build out. And yes i<

 User Rating: 1015    Report this Post to a Moderator | Link

try this again.

I'm believe that the function is correct as stated in the article( mat[i][j] +=).
Just look at how the loops build out. And yes ( i <

 User Rating: 1015    Report this Post to a Moderator | Link

I'm having difficulty with rotation. When I rotate a Quad by the z Axes it gits smaller. When I rotate it by the x or y wide or long according to the access. I'm using OpenGL and I think yours is DirectX, not sure if make difference. Anyway here is my rotation function.

const float degtorad = 3.1415/180; //convert to degrees.

void rotate ( GLfloat x, GLfloat y, GLfloat z )
{
if ( x )
{
x*=degtorad; //convert to degrees.
matrix[1][1]+=cos ( x );
matrix[2][1]-=sin ( x );
matrix[1][2]+=sin ( x );
matrix[2][2]+=cos ( x );
}
if ( y )
{
y*=degtorad; //convert to degrees.
matrix[0][0]+=cos ( y );
matrix[2][0]+=sin ( y );
matrix[0][2]-=sin ( y );
matrix[2][2]+=cos ( y );
}
if ( z )
{
z*= degtorad; //convert to degrees.
matrix[0][0]+=cos ( z );
matrix[1][0]-=sin ( z );
matrix[0][1]+=sin ( z );
matrix[1][1]+=cos ( z );
}
}



 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by Drew_Benton
Quote:
Original post by Anonymous Poster
Starts off talking like were newbies ( Which I am). Then starts talking about sin/cos bla bla blah. Completely losses track. Not usefull, sorry.


You learn the basics of sin/cos in Geometry and then the matricies in Algebra 2 (US Education Systems in TX at least). For us it was, Alg1, Geom, Alg2, PreCalc, Calc in high school. If you are looking into 3D Math, and do now know your basic trig (the sin/cos blah blah), then I highly reccomend going back and learning what you are missing. In those examples, it clearly shows what you have to do. If you want to rotate about the X axis, then for the X Rotation matrix, given as:
1  	0  	0  	0
0 	cos(x) 	-sin(x}	0
0 	sin(x) 	cos(x) 	0
0 	0 	0 	1

You simply replace X with the radians/degress of what angle you want to rotate by (depending on your math systems), so if you want to rotate by 180 degress (which is PI radians) you will have:
1  	0  	0  	0
0 	cos(3.1415) 	-sin(3.1415}	0
0 	sin(3.1415) 	cos(3.1415) 	0
0 	0 	0 	1


Now you have your matrix! Regardless of your programming API, there should be some math functions that can convert cos(3.1415) to -1 and sin(3.1415) to 0 and so on. That's all there is to it, it's very, very useful. For a conversion from degress to radians take a look at the Unit ciricle (something you should have to memorize in Pre-Calc). Other than that, it's just plug and chug stuff. For sin/cos/trig, all you need to know is SOHCAHTOA.


This is confusing. I know matrix algebra, and I know trigonometry and stuff... but I was hoping for an insightful article on matrices as they pertain to 3D Math.

You didn't clarify at all why the matrix for rotation along the X axis is that, and why the matrix form of rotation along the Y axis is that... and so forth and so on.

I'm looking at the matrix and I'm wondering why the rotation for the X axis has the trigonometric functions located in the center four elements. And why the Y rotation has the functions located where they are, and why the Z rotation has the functions located in the upper left four elements.


Why is that the case? The article doesn't make it clear. Is that supposed to be something I should know, even though I know about vectors, trigonometry, and matrix algebra?

What particular subject should discuss this aspect?

[Edited by - Raziaar on March 27, 2009 9:11:52 AM]

 User Rating: 1015   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

Quote:
Original post by Raziaar
Quote:
Original post by Drew_Benton
Quote:
Original post by Anonymous Poster
Starts off talking like were newbies ( Which I am). Then starts talking about sin/cos bla bla blah. Completely losses track. Not usefull, sorry.


You learn the basics of sin/cos in Geometry and then the matricies in Algebra 2 (US Education Systems in TX at least). For us it was, Alg1, Geom, Alg2, PreCalc, Calc in high school. If you are looking into 3D Math, and do now know your basic trig (the sin/cos blah blah), then I highly reccomend going back and learning what you are missing. In those examples, it clearly shows what you have to do. If you want to rotate about the X axis, then for the X Rotation matrix, given as:
1  	0  	0  	0
0 	cos(x) 	-sin(x}	0
0 	sin(x) 	cos(x) 	0
0 	0 	0 	1

You simply replace X with the radians/degress of what angle you want to rotate by (depending on your math systems), so if you want to rotate by 180 degress (which is PI radians) you will have:
1  	0  	0  	0
0 	cos(3.1415) 	-sin(3.1415}	0
0 	sin(3.1415) 	cos(3.1415) 	0
0 	0 	0 	1


Now you have your matrix! Regardless of your programming API, there should be some math functions that can convert cos(3.1415) to -1 and sin(3.1415) to 0 and so on. That's all there is to it, it's very, very useful. For a conversion from degress to radians take a look at the Unit ciricle (something you should have to memorize in Pre-Calc). Other than that, it's just plug and chug stuff. For sin/cos/trig, all you need to know is SOHCAHTOA.


This is confusing. I know matrix algebra, and I know trigonometry and stuff... but I was hoping for an insightful article on matrices as they pertain to 3D Math.

You didn't clarify at all why the matrix for rotation along the X axis is that, and why the matrix form of rotation along the Y axis is that... and so forth and so on.

I'm looking at the matrix and I'm wondering why the rotation for the X axis has the trigonometric functions located in the center four elements. And why the Y rotation has the functions located where they are, and why the Z rotation has the functions located in the upper left four elements.


Why is that the case? The article doesn't make it clear. Is that supposed to be something I should know, even though I know about vectors, trigonometry, and matrix algebra?

What particular subject should discuss this aspect?

Yeah the only reason I understood 1/2 the article is because I've take a Linear Algebra course so I know how the those matrixes are derived since we did them in class.
More examples would be helpful to say the least and why throw loop unrolling in to add to the confusion?




 User Rating: 1288   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

n/m

 User Rating: 1014   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

[/quote]
This is confusing. I know matrix algebra, and I know trigonometry and stuff... but I was hoping for an insightful article on matrices as they pertain to 3D Math.

You didn't clarify at all why the matrix for rotation along the X axis is that, and why the matrix form of rotation along the Y axis is that... and so forth and so on.

I'm looking at the matrix and I'm wondering why the rotation for the X axis has the trigonometric functions located in the center four elements. And why the Y rotation has the functions located where they are, and why the Z rotation has the functions located in the upper left four elements.


Why is that the case? The article doesn't make it clear. Is that supposed to be something I should know, even though I know about vectors, trigonometry, and matrix algebra?

What particular subject should discuss this aspect?[/quote]
Well, with a bit of tinkering I'm sure you would have got it but lets look together. For our purposes, we will use a 2D example.

A point, P, has a position of (4,-3). If that is rotated 90 degrees around the Origin(0,0) what will it's new position be? 

P(4,-3) rotated 90 around Origin = P'(3,4). 

What just happened? Well, our x coordinate was replaced with; 

cos(90)*x + sin(90)*Y*(-1) 

and our y coordinate with:
sin(90)*X + cos(90)*Y.

hrmm... if only there was a way to store all this math... THE MATRIX!!!

or at least... A matrix.

Our vector [x,y] multiplied by a matrix [cos(theta), sin(theta)]
                                        [-sin(theta),cos(theta)]

gave us our vector rotated by (theta). now when we jump to 3d, it looks like this,

P[x,y,z] * M [cos(theta), sin(theta), 0]
             [-sin(theta),cos(theta), 0]
             [0,          0,          1]

That's right our trivial 2d rotation was actually a rotation around the z axis.
seen this way our matrix has a column for each axis, and since this is a zaxis rotation, the Z coordinate of our point doesn't change, hence:

 z'= x*0+y*0+z*1.



 User Rating: 1016   |  Rate This User  Send Private MessageView Profile Report this Post to a Moderator | Link

All times are ET (US)

Post Reply
 Last Thread Next Thread 
Forum Rules:
You may not post new threads
You may post replies
You may not edit your posts
You may not use HTML in your posts
Jump To:
Administrative Options: