markww 122 Report post Posted October 13, 2007 Hi, I'm looking at some code to do 4x4 matrix multiplication. Using the two matrices: 20.0 0.0 0.0 0.0 0.0 40.0 0.0 0.0 0.0 0.0 20.0 0.0 0.0 0.0 0.0 1.0 1.0 0.0 0.0 5.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 I keep getting: 20.0 0.0 0.0 5.0 0.0 40.0 0.0 0.0 0.0 0.0 20.0 0.0 0.0 0.0 0.0 1.0 but double checking myself again online 4x4 calculators I keep getting a value of 100 in the top right element [3][0]. Is that correct?: http://www.bluebit.gr/matrix-calculator/multiply.aspx http://www.jimmysie.com/maths/matrix.php#a=res&opname=multiply&r1=4&c1=4&r2=4&c2=4&a11=20&a12=0&a13=0&a14=0&a21=0&a22=40&a23=0&a24=0&a31=0&a32=0&a33=20&a34=0&a41=0&a42=0&a43=0&a44=1&b11=1&b12=0&b13=0&b14=5&b21=0&b22=1&b23=0&b24=0&b31=0&b32=0&b33=1&b34=0&b41=0&b42=0&b43=0&b44=1 etc..... Thanks 0 Share this post Link to post Share on other sites
Zipster 2377 Report post Posted October 13, 2007 You're both correct [smile] Your code is doing the bottom matrix times the top matrix, while those websites are doing the top matrix times the bottom matrix. Since matrix multiplication isn't commutative, the results are different. I'm sure if you switch the multiplication order around for one of them you'll get consistent results. 0 Share this post Link to post Share on other sites
markww 122 Report post Posted October 13, 2007 Hmm ok I ask because in my case I want to apply a scale transform, then a translation transform. My scale:20 0 0 00 40 0 0 0 0 20 00 0 0 1my translate:1 0 0 50 1 0 00 0 1 00 0 0 1and I am multiplying like: scale * translateso shouldn't we always get:20 0 0 50 40 0 00 0 20 00 0 0 1when multiplied in that specific order?Thanks 0 Share this post Link to post Share on other sites
TheAdmiral 1122 Report post Posted October 14, 2007 Well, no. The definition of matrix multiplication demands that your 'scale * translate' has a value of 100 in the corner. But I suspect the confusion truly lies with pre- versus post-multiplication. Can you tell us how you apply the transformation to the vector?If you store row-vectors, "transformed_vector = untransformed_vector * transformation_matrix" - you are premultiplying, and so the overall transformation should be 'scale * translate'.On the other hand column-vectors transform according to "transformed_vector = transformation_matrix * untranformed_vector" and so 'translate * scale' is the required transformation.This is easy to understand if you write the transformation equation in full. By parenthesising according to the associativity law, you can see that transformations are applied in order of their proximity to the source vector in the equation. In each case:result = source * trans_1 * trans_2 * ...result = ... * trans_2 * trans_1 * sourceAdmiral 0 Share this post Link to post Share on other sites
JohnBolton 1372 Report post Posted October 14, 2007 Quote:Original post by markwwMy scale:20 0 0 00 40 0 0 0 0 20 00 0 0 1my translate:1 0 0 50 1 0 00 0 1 00 0 0 1and I am multiplying like: scale * translateso shouldn't we always get:20 0 0 50 40 0 00 0 20 00 0 0 1when multiplied in that specific order?No. When multiplying, you dot rows in the first matrix with columns in the second matrix. To get the value of the 1st row 4 column, you dot the 1st row of the first matrix with the 4th column of the second matrix: [20 0 0 0] dot [5 0 0 1] = 100 0 Share this post Link to post Share on other sites