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[0][0] = cosf(rotationAngles.z);
zRotation.matrix[1][0] = -sinf(rotationAngles.z);
zRotation.matrix[0][1] = sinf(rotationAngles.z);
zRotation.matrix[1][1] = cosf(rotationAngles.z);
zRotation.matrix[2][2] = 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[0][0]) + (operand.x * matrix[1][0]) + (operand.x * matrix[2][0]) + (operand.x * matrix[3][0]), (operand.y * matrix[0][1]) + (operand.y * matrix[1][1]) + (operand.y * matrix[2][1]) + (operand.y * matrix[3][1]), (operand.z * matrix[0][2]) + (operand.z * matrix[1][2]) + (operand.z * matrix[2][2]) + (operand.z * matrix[3][2]));
    return result;

(Fourth element is always 0)

  • \$\begingroup\$ Why is the example animated? The example code should give a single fixed rotation. Are you changing the rotationAngles.z value? \$\endgroup\$ – Jay Mar 13 '19 at 19:23
  • \$\begingroup\$ Are you sure you are multiplying the vertices correctly? \$\endgroup\$ – Bálint Mar 13 '19 at 19:26
  • \$\begingroup\$ @Jay, Yes, I increase it to see the rotation clearly. \$\endgroup\$ – Telno Mar 13 '19 at 19:36
  • \$\begingroup\$ @Bálint I edited the post to show how I multiply vertices and matrices \$\endgroup\$ – 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[0][0] + operand.y * matrix[1][0] + operand.z * matrix[2][0], operand.x * matrix[0][1] + operand.y * matrix[1][1] + operand.z * matrix[2][1], operand.x * matrix[0][2] + operand.y * matrix[1][2] + operand.z * matrix[2][2]);
|improve this answer|||||
  • \$\begingroup\$ I was searching for that mistake for so long ! Thanks for to answering my posts ! \$\endgroup\$ – Telno Mar 13 '19 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.