|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Matrix Math |
|
![]() Anonymous Poster |
||||
|
||||
| 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; } |
||||
|
||||
![]() bkt Member since: 3/8/2004 From: Tuckerton, NJ, United States |
||||
|
|
||||
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] |
||||
|
||||
![]() superpig GDNet Technical Lead Member since: 5/26/2001 From: Oxford, United Kingdom |
||||
|
|
||||
| bkt, nope. When i or j are 4, the conditional expressions (i < 4, j < 4) will fail, so the loop stops. |
||||
|
||||
![]() 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. |
||||
|
||||
![]() Drew_Benton GDNet+ Member since: 7/29/2004 From: Katy, TX, United States |
||||
|
|
||||
Quote: 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 |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| 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< |
||||
|
||||
![]() Anonymous Poster |
||||
|
||||
| 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 < |
||||
|
||||
![]() jlboss Member since: 11/8/2005 From: Wichita, KS, United States |
||||
|
|
||||
| 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 ); } } |
||||
|
||||
![]() Raziaar Member since: 9/23/2008 From: Carrollton, TX, United States |
||||
|
|
||||
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? [Edited by - Raziaar on March 27, 2009 9:11:52 AM] |
||||
|
||||
![]() daviangel Member since: 6/13/2000 From: Marina, CA, United States |
||||
|
|
||||
Quote: 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? |
||||
|
||||
![]() Esys Member since: 7/6/2008 From: Millville, NJ, United States |
||||
|
|
||||
| n/m |
||||
|
||||
![]() Burnt_Fyr Member since: 8/25/2009 From: Edmonton, Canada |
||||
|
|
||||
| [/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.
|
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|