Rotation and translation like in GTA 1 OpenGL

Posted by user1876377 on Game Development See other posts from Game Development or by user1876377
Published on 2013-11-10T10:50:08Z Indexed on 2013/11/10 16:13 UTC
Read the original article Hit count: 279

Filed under:
|

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:

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

Draw:
  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) *
                        glm::translate<float>(spaceship_position);
  drawMesh(spaceship, texture, transform);
Update:
   switch (key.keysym.sym) {
   case SDLK_UP:
       spaceship_position.z += 0.1;
       break;
   case SDLK_DOWN:
       spaceship_position.z -= 0.1;
       break;
   case SDLK_LEFT:
       spaceship_rotation.y += 1;
       break;
   case SDLK_RIGHT:
       spaceship_rotation.y -= 1;
       break;
    }

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?

© Game Development or respective owner

Related posts about opengl

Related posts about rotation