'How do I move a polygon's vertices in the direction of the front face based on its present rotation?

I want the user to have the ability to use the arrow keys to manipulate the polygon. The top arrow key is supposed to move all of the vertices in the direction of the top face. The left arrow key does this - > angleOfRotation += 5. The right arrow key does this - > angleOfRotation -= 5.

Screenshot of Game

In this image you will find my polygon. There is a box and arrow pointing out the top face. Also, you will notice an arrow pointing away from the polygon illustrating the direction I want it to move with its current angleOfRotation.

game.cpp

#include <iostream>

#include <GL/freeglut.h>


int i;

static int r = 25;
static int screen[] = {640, 480};
static float angleOfRotation = 0.0;

int pos[] = {15, 1};

struct Box {
  GLint x = -1; GLint y = -1;
  int rx = 2.5, ry = 5;
  GLfloat pos[4][2] = {
    {x, y}, {x, y-ry}, {x+rx, y-ry}, {x+rx, y}
  };
};

Box box;

void reshape(GLint w, GLint h)
{
  glViewport(0,0,w,h);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluOrtho2D(-r,r,-r,r);
}

void display()
{
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef((box.pos[0][0]+box.pos[1][0])/2, (box.pos[0][1]+box.pos[1][1])/2, 0.0);
  glRotatef(angleOfRotation, 0.0, 0.0, 1.0);
  glTranslatef((-box.pos[0][0]+-box.pos[1][0])/2, (-box.pos[0][1]+-box.pos[1][1])/2, 0.0);

  glBegin(GL_POLYGON);
  for (i=0;i<4;i++)
    glVertex2f(box.pos[i][0], box.pos[i][1]);
  glEnd();


  glutSwapBuffers();
}

void timer(int v)
{
  glutTimerFunc(1000/60, timer, v);
  glutPostRedisplay();
}

void special(int key, int, int)
{
  switch (key)
  {
    case GLUT_KEY_LEFT:
      angleOfRotation += 5;
      break;
    case GLUT_KEY_RIGHT:
      angleOfRotation -= 5;
      break;
    case GLUT_KEY_UP:
      for (i=0;i<4;i++)
        box.pos[i][1] += 0.36;
      break;
    case GLUT_KEY_DOWN:
      for (i=0;i<4;i++)
        box.pos[i][1] -= 0.36;
      break;
    default: return;
  }
}


int main(int argc, char** argv)
{
  glutInit(&argc, argv);

  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

  glutInitWindowPosition(100, 100);
  glutInitWindowSize(screen[0], screen[1]);

  glutCreateWindow("game");

  glutReshapeFunc(reshape);
  glutDisplayFunc(display);

  glutSpecialFunc(special);

  glutTimerFunc(0,timer,0);

  glutMainLoop();
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source