0

I am trying to allocate my model a position in scene according to the 9DoF parameters. Out of which, the rotations are giving me a hard time. If I am keeping x and z rotations zero then the roty seems to work fine and model revolve around y axis only. But as soon as I am giving x and y rotations any value, then model moves around other two axis while doing y rotation.

Below is the code I am using to give the rotx, roty, rotz values. I feel like there is some problem with matrix multiplication. Because 0 * x + roty * y + 0 * z = roty only but anything * x + roty * y + anything * z = anything.

model = glm::translate(model, glm::vec3(transX, transY, transZ));
model = glm::rotate(model, toRadians * rotX, glm::vec3(1, 0, 0));
model = glm::rotate(model, toRadians * rotY, glm::vec3(0, 1, 0));
model = glm::rotate(model, toRadians * rotZ, glm::vec3(0, 0, 1));
Rabbid76
  • 177,135
  • 25
  • 101
  • 146
  • 4
    Matrix multiplications are not [Commutative](https://en.wikipedia.org/wiki/Commutative_property). The order matters. – Rabbid76 Mar 27 '21 at 13:35

1 Answers1

-2

For orienting an object, apply the rotation first, only after that think about translation/rotating about another object:

 model = glm::rotate(model, toRadians * rotX, glm::vec3(1, 0, 0));
 model = glm::rotate(model, toRadians * rotY, glm::vec3(0, 1, 0));
 model = glm::rotate(model, toRadians * rotZ, glm::vec3(0, 0, 1));
 model = glm::translate(model, glm::vec3(transX, transY, transZ));
  • *"apply the rotation first,"*. If you want to rotate first, the command `glm::rotate` must be the last line in code, because `glm::roatate` creates a rotation matrix and multiplies the input matrix with the rotation matrix. Rotation first means `translate * rotate`. See [OpenGL move object and keep transformation](https://stackoverflow.com/questions/46641995/opengl-move-object-and-keep-transformation/46650784#46650784) – Rabbid76 Jul 31 '21 at 08:46