# matrix multiplication help

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

## Recommended Posts

hey, my problem is, i have a cube made up 6 sides each side of 4 points making 24 points in total, each one of these points is a vector (ie x,y,z co-ordinates). now to rotate my cube i do to everypoint xrotate[3][3] = {{1,0,0}, {0, cos(xspin), sin(xspin)}, {(0, -sin(xspin), cos(xspin))}} cubeSide1 = matrixMultiplication(xrotate,cubeSide1); CVector3 matrixMultiplication(float vect[][3], CVector3 hello) { float a = (vect[0][0] * hello.x) + (vect[1][0] * hello.y) + (vect[2][0] * hello.z); float b = (vect[0][1] * hello.x) + (vect[1][1] * hello.y) + (vect[2][1] * hello.z); float c = (vect[0][2] * hello.x) + (vect[1][2] * hello.y) + (vect[2][2] * hello.z); return CVector3(a,b,c); } Or am i going about it wrong and should somehow be rotating the hole cube ?

##### Share on other sites
what's the problem, anyway? It rotates wrongly, or something else?

##### Share on other sites
it doesnt rotate at all it seems to stretch thur the sides of it

##### Share on other sites
Probably coding bugs. Post complete code.
You should transform ALL your CORNER points using matrix.
Also,
Quote:
 cubeSide1 = matrixMultiplication(xrotate,cubeSide1);

not a good idea.
Keep original cube, and each time make new rotation matrix for your angle, just with bigger angle. Even correct rotation isn't perfect, especially with floats, and if you many times consequently rotate something, you'll get bad results.

##### Share on other sites
Why use 24 points to represent your cube when most of these are degenerate. A cube can be fully described with just 8 unique points, the corners of the cube. Are you always remembering to rotate your cube around the origin before applying any transformations?

##### Share on other sites
Quote:
 Original post by Goddenit doesnt rotate at all it seems to stretch thur the sides of it

i think i know what your problem is.

are you by any chance storing your result in you input without a temp vector?

youll need to do:

temp = matrix* vector
return temp;

this will produce the result you are describgin:

vector = matrix * vector
return vector;

##### Share on other sites
sorry i was in a hurry there.

so what you shouldnt do is:

vector multiply(vector v){v.x = v.x*matrix + v.y*matrix+v.z*matrix;v.y = v.x*matrix + v.y*matrix+v.z*matrix;v.z = v.x*matrix + v.y*matrix+v.z*matrix;return v;}

what you should do is:
vector multiply(vector v){vector temptemp.x = v.x*matrix + v.y*matrix+v.z*matrix;temp.y = v.x*matrix + v.y*matrix+v.z*matrix;temp.z = v.x*matrix + v.y*matrix+v.z*matrix;return temp;}

look closely and im sure youll see why.

if this wasnt the problem, consider this not posted :)

##### Share on other sites
returning a temp vector is going to make no different to what i am doing now

##### Share on other sites
Quote:
 Original post by MotorherpWhy use 24 points to represent your cube when most of these are degenerate. A cube can be fully described with just 8 unique points, the corners of the cube. Are you always remembering to rotate your cube around the origin before applying any transformations?

how would i go about that ?

##### Share on other sites
Quote:
Original post by Godden
Quote:
 Original post by MotorherpWhy use 24 points to represent your cube when most of these are degenerate. A cube can be fully described with just 8 unique points, the corners of the cube. Are you always remembering to rotate your cube around the origin before applying any transformations?

how would i go about that ?

Store the 8 corner verts in an array and have each face contain offsets into this array to the verts it encompases. This way you only need to tranform 8 verts instead of 24. As for the order of transformations, always rotate first then translate in this situation otherwise the rotation will also move the objects centre position which isn't what you want. If you're doing this the wrong way around though it shouldn't make your object warp and I think Eelco's on the nail with this. What he's saying is that you need a temp vert to work with whilst rotating. If you write directly in to the same vert, the result of them writings will distort the rest of your calculations since you'll be continuing your rotation maths with a vector that has some coords already rotated and some not.

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5

• 26
• 10
• 11
• 9
• 9
×