Jump to content
  • Advertisement
Sign in to follow this  
Marianne

Matrix multiplication (works with 2 but not with more)

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm making a 3D game using 3D rotation matrixes, and multiplying them together to add different effects. I've documented myself on this subject on google and made a function to mutiply matrices, etc. it works efficiently when using 2 matrices, but when using more than one i multiply like this (AxB)xC and all i get is my objet screwed up all ways on screen. How must i multiply more than 2 matrices together? thanks ;) i've not found any website mentionning those here's the method i'm using, just in case matrix format: double matrix[] 0, 1, 2, 3, 4, 5, 6, 7, 8

void mulMatrix(double* MatrixA, double* MatrixB, double* dest_matrix){
    dest_matrix[0] = MatrixA[0] * MatrixB[0] + MatrixA[1] * MatrixB[3] + MatrixA[2] * MatrixB[6];
    dest_matrix[1] = MatrixA[0] * MatrixB[1] + MatrixA[1] * MatrixB[4] + MatrixA[2] * MatrixB[7];
    dest_matrix[2] = MatrixA[0] * MatrixB[2] + MatrixA[1] * MatrixB[5] + MatrixA[2] * MatrixB[8];
    
    dest_matrix[3] = MatrixA[3] * MatrixB[3] + MatrixA[4] * MatrixB[3] + MatrixA[5] * MatrixB[6];
    dest_matrix[4] = MatrixA[3] * MatrixB[4] + MatrixA[4] * MatrixB[4] + MatrixA[5] * MatrixB[7];
    dest_matrix[5] = MatrixA[3] * MatrixB[5] + MatrixA[4] * MatrixB[5] + MatrixA[5] * MatrixB[8];
    
    dest_matrix[6] = MatrixA[6] * MatrixB[6] + MatrixA[7] * MatrixB[3] + MatrixA[8] * MatrixB[6];
    dest_matrix[7] = MatrixA[6] * MatrixB[7] + MatrixA[7] * MatrixB[4] + MatrixA[8] * MatrixB[7];
    dest_matrix[8] = MatrixA[6] * MatrixB[8] + MatrixA[7] * MatrixB[5] + MatrixA[8] * MatrixB[8];
}


Share this post


Link to post
Share on other sites
Advertisement
Matrix multiplication is associative. It shouldn't matter if you multiply A and B together first or B and C together first.

Share this post


Link to post
Share on other sites
> Matrix multiplication is associative. It shouldn't matter if you multiply A and B together first or B and C together first.

i know, but it doesn't work...
i do something like:

C= AxB
E= CxD

and i use E, however it just manages to screw up my object instead of doing nice rotations like each matrix does separately

Share this post


Link to post
Share on other sites
Quote:
Original post by Marianne
    dest_matrix[3] = MatrixA[3] * MatrixB[3] + MatrixA[4] * MatrixB[3] + MatrixA[5] * MatrixB[6];
dest_matrix[4] = MatrixA[3] * MatrixB[4] + MatrixA[4] * MatrixB[4] + MatrixA[5] * MatrixB[7];
dest_matrix[5] = MatrixA[3] * MatrixB[5] + MatrixA[4] * MatrixB[5] + MatrixA[5] * MatrixB[8];

should be

dest_matrix[3] = MatrixA[3] * MatrixB[0] + MatrixA[4] * MatrixB[3] + MatrixA[5] * MatrixB[6];
dest_matrix[4] = MatrixA[3] * MatrixB[1] + MatrixA[4] * MatrixB[4] + MatrixA[5] * MatrixB[7];
dest_matrix[5] = MatrixA[3] * MatrixB[2] + MatrixA[4] * MatrixB[5] + MatrixA[5] * MatrixB[8];
and
Quote:
Original post by Marianne
    dest_matrix[6] = MatrixA[6] * MatrixB[6] + MatrixA[7] * MatrixB[3] + MatrixA[8] * MatrixB[6];
dest_matrix[7] = MatrixA[6] * MatrixB[7] + MatrixA[7] * MatrixB[4] + MatrixA[8] * MatrixB[7];
dest_matrix[8] = MatrixA[6] * MatrixB[8] + MatrixA[7] * MatrixB[5] + MatrixA[8] * MatrixB[8];

should be
    dest_matrix[6] = MatrixA[6] * MatrixB[0] + MatrixA[7] * MatrixB[3] + MatrixA[8] * MatrixB[6];
dest_matrix[7] = MatrixA[6] * MatrixB[1] + MatrixA[7] * MatrixB[4] + MatrixA[8] * MatrixB[7];
dest_matrix[8] = MatrixA[6] * MatrixB[2] + MatrixA[7] * MatrixB[5] + MatrixA[8] * MatrixB[8];

Share this post


Link to post
Share on other sites
thanks! it works great!

the site i've been on must have been wrong (i don't reckon i made an error cause it did work for simple mul)

Share this post


Link to post
Share on other sites
quote:
>> Matrix multiplication is associative. It shouldn't matter if you multiply A and B together first or B and C together first.

>i know, but it doesn't work...

You are both wrong. And I think you mean commutative instead of associative.
AND matrix multiplication are in general NOT commutative so it does matter whether you multiply like this AxB or this BxA. A clear example of this is the multiplaication of a 2x3 matrix with a 3x2 matrix... One way around it gives a 2x2 matrix. The other way around, a 3x3 matrix comes out as result..

Share this post


Link to post
Share on other sites
For multiplication:

Associative means (a*b)*c = a*(b*c)

Commutative means a*b = b*a

so Marianne and SiCrane were correct in using the term associative, and correct in saying that it applies to matrix multiplication. Matrix multiplication is not commutative, but then that was never an issue...

Share this post


Link to post
Share on other sites
Yes I know what associative and communicative means. Reading the post again I see I missread it. I see he was actually talking about the associative rule..=)
Geez..
My mistake - sorry!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!