0
$\begingroup$

I'm trying to write a little program with a bouncing cube but I can't find the right x, y, z and angle for it. I have these lines:

modelMatrix = glm::mat4(1)

modelMatrix *= Translate(x, y, z)

modelMatrix *= RotateOZ(angle)

RenderMesh(mesh["cube"], shaders["V"], modelMatrix)

I added a picture to make it easier to understand. My cube will start at x0, y0, z0(we can leave the z out of this because for now I want the cube to move only in the OX axis and OY). I added a arrow the cube so I can represent the rotation better. First he will move forward on the OX and OY axis while rotating in the range of [0 - 90] grades when he will hit X1(which is X / 2) and Y1 then he will rotate in the range of [90 - 180] grades while moving forward on the OX and descending on the OY. I hope that made sense.

enter image description here

My problem is that I can't find the right x, y and angle to give to these transformations so I can get the animation that I want. I tried to add to x, y and angle the same value such as 0.1 and the result is close but not perfect. RotateOZ return a mat4:

return glm::mat4(
        cos(angle), -sin(angle), 0, 0,
        sin(angle), cos(angle), 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1
    );

Thank you for your time!

$\endgroup$
2
  • $\begingroup$ Not sure if it solves your problem, but you should always rotate your objects first before moving them to their destination in the scene. Otherwise, the translations get "mixed" into the rotations since rotation matrices always rotate around the origin. Further information can be found here. $\endgroup$
    – wychmaster
    Dec 11 '20 at 6:25
  • $\begingroup$ Like @wychamaster said the matrix operations are not commutative wrt multiplication so you get a different result with: translate * rotate then you do with: rotate * translate. To get what you want do a "translate to origin", "then do the rotation", "then translate to position". $\endgroup$
    – pmw1234
    Dec 12 '20 at 17:50

Your Answer

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

Browse other questions tagged or ask your own question.