# Z rotation matrix inverting object

I'm currently creating a 3d game where I need to rotate shapes. To do se, I created a rotation matrix for each axis, X, Y and Z. X and Y are giving me expected results, but not Z who kind of "inverts the shape". I need it to rotate like it would rotate a 2d shape.

Here is the Z matrix :

zRotation.matrix = cosf(rotationAngles.z);
zRotation.matrix = -sinf(rotationAngles.z);
zRotation.matrix = sinf(rotationAngles.z);
zRotation.matrix = cosf(rotationAngles.z);
zRotation.matrix = 1;


Here is what it looks like : https://imgur.com/a/3YR4Nlc

PS : The game have an isometric perspective.

EDIT : Here is how I do matrix * vector

vec3 operator *(vec3 operand){
//multiply vector by matrix

vec3 result = vec3((operand.x * matrix) + (operand.x * matrix) + (operand.x * matrix) + (operand.x * matrix), (operand.y * matrix) + (operand.y * matrix) + (operand.y * matrix) + (operand.y * matrix), (operand.z * matrix) + (operand.z * matrix) + (operand.z * matrix) + (operand.z * matrix));
return result;
}


(Fourth element is always 0)

• Why is the example animated? The example code should give a single fixed rotation. Are you changing the rotationAngles.z value? – Jay Mar 13 '19 at 19:23
• Are you sure you are multiplying the vertices correctly? – Bálint Mar 13 '19 at 19:26
• @Jay, Yes, I increase it to see the rotation clearly. – Telno Mar 13 '19 at 19:36
• @Bálint I edited the post to show how I multiply vertices and matrices – Telno Mar 13 '19 at 19:38

Something's not quite right here. If you take the matrix and multiply it with the vector by hand, you'll get

$$\vec v(x\cdot cos(\theta)-x\cdot sin(\theta), y\cdot sin(\theta)+y\cdot cos(\theta), 0)$$

When it should be

$$\vec v(x\cdot cos(\theta)-y\cdot sin(\theta), x\cdot sin(\theta)+y\cdot cos(\theta), 0)$$

(notice the $$\x\$$ and $$\y\$$)

The problem actually lies in your vector multiplication function. When we multiply a matrix by a vector, we are basically taking the dot products of the vector and every row of the matrix. What you are doing is multiplying every element of a row by the same coordinate. So your code should be something like this instead:

vec3 result = vec3(operand.x * matrix + operand.y * matrix + operand.z * matrix, operand.x * matrix + operand.y * matrix + operand.z * matrix, operand.x * matrix + operand.y * matrix + operand.z * matrix);

• I was searching for that mistake for so long ! Thanks for to answering my posts ! – Telno Mar 13 '19 at 21:00