Okay, so I have a figure in XZ plain. I want to move it forward/backward and rotate at it's own Y axis, then move forward again in the rotation's direction, like the character in GTA 1.

Code so far:

  spaceship_position = glm::vec3(0,0,0);
  spaceship_rotation = glm::vec3(0,0,0);
  spaceship_scale = glm::vec3(1, 1, 1);

  glm::mat4 transform = glm::scale<float>(spaceship_scale) *
                        glm::rotate<float>(spaceship_rotation.x, 1, 0, 0) *
                        glm::rotate<float>(spaceship_rotation.y, 0, 1, 0) *
                        glm::rotate<float>(spaceship_rotation.z, 0, 0, 1) *
  drawMesh(spaceship, texture, transform);
   switch (key.keysym.sym) {
   case SDLK_UP:
       spaceship_position.z += 0.1;
   case SDLK_DOWN:
       spaceship_position.z -= 0.1;
   case SDLK_LEFT:
       spaceship_rotation.y += 1;
   case SDLK_RIGHT:
       spaceship_rotation.y -= 1;

So this only moves on the Z axis, but how can I move the object on both Z and X axis where the object is facing?

  • \$\begingroup\$ It would be useful to actually state the problem that you are facing though. \$\endgroup\$
    – radical
    Nov 10, 2013 at 11:10
  • \$\begingroup\$ You may want to check out this site: engineeringdotnet.blogspot.co.uk/2010/04/… It describes very basic physics of steering a car (or model in your case). If you solve it, please update your code above or answer your own question below \$\endgroup\$ Nov 10, 2013 at 16:21

1 Answer 1


IIUC, your order of matrix multiplication looks incorrect. Essentially, what you are doing is:

transform = rot * trans

Which means that the object will be translated first and then rotated. After translation, the object won't be at the origin, but your rotation matrices will rotate it around the world axis, which is not what you want. The effect that you want is to rotate the object around its local axis, for that, reverse the order, i.e., trans * rot.

  • 1
    \$\begingroup\$ Well it only goes on the Z axis now, but how can I move towards the facing of the rotation? \$\endgroup\$
    – WuXorT
    Nov 10, 2013 at 11:12
  • \$\begingroup\$ Since the translation would be "applied" after the rotation, it would be translated on the rotated axis. So, just reversing the order should help. eg, if you forward is +z, then (0, 0, 5), would work correctly after rotation also, i.e., it would move forward w.r.t the object. \$\endgroup\$
    – radical
    Nov 10, 2013 at 11:21
  • \$\begingroup\$ Okay now, it turns around it's own axis, however I have to modify the Update, where I update not only z, but x as well, some sin and cos will be needed. \$\endgroup\$
    – WuXorT
    Nov 10, 2013 at 11:45
  • \$\begingroup\$ I'm not sure that I understand. Whatever translation vector that you choose, will get applied after the rotation. \$\endgroup\$
    – radical
    Nov 10, 2013 at 11:48
  • \$\begingroup\$ The problem is calculating the translation vector, i.e when facing at upper right corner(45 degrees) I have to add 0.5x and 0.5z, facing 135 degree(upper left corner) i have to add -0.5x and 0.5z and so on. But there must be a formula for this calculation. \$\endgroup\$
    – WuXorT
    Nov 10, 2013 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .