title: Draw trajectory of an object in OpenGL
body: <p>I want to make the codes that draw trajectory as the object's moving when I clicked any position on the screen.</p>
  
  <p>I set the initial center point as a starting point. And I want to set the other point with mouse button clicked (destination point) as variables but I can't figure out how to do this.</p>
  
  <pre><code>float v1[3] = { -35.0f,  22.5f, 0.0f };
  float v2[3] = { -35.0f, -22.5f, 0.0f };
  float v3[3] = { 0.0f,  42.5f, 0.0f };
  float v4[3] = { 0.0f, -42.5f, 0.0f };
  float v5[3] = { 35.0f,  22.5f, 0.0f };
  float v6[3] = { 35.0f, -22.5f, 0.0f };
  </code></pre>
  
  <p>This is the initial position of the object. (6-point star with 2 triangles)</p>
  
  <pre><code>float px, py;
  float center_s[3] = { 0.0f, 0.0f, 0.0f };
  float center_d[3] = { px, py, 0.0f };
  </code></pre>
  
  <p><code>center_s</code> is the starting point and <code>center_d</code> is the destination point with variable <code>px</code>, <code>py</code>.</p>
  
  <pre><code>void lines(void) {
      glColor3f(1.0, 0.0, 0.0);
  
      glPointSize(5);
      glBegin(GL_POINTS);
      glVertex3fv(center_s);
  
      glLineWidth(1);
      glBegin(GL_LINES);
      glVertex3fv(center_s);
      glVertex3fv(center_d);
  }
  </code></pre>
  
  <p>This function draws trajectory with red lines from <code>center_s</code> to <code>center_d</code>. Also it draws a center point.</p>
  
  <pre><code>case GLUT_LEFT_BUTTON:
      if (state == GLUT_DOWN) {
          x = mx;
          y = glutGet(GLUT_WINDOW_HEIGHT) - my;
          px = x + 35.0;
          py = y + 42.5;
          glutIdleFunc(lines);
      }
      glutPostRedisplay();
      break;
  </code></pre>
  
  <p>Here is the problem. When the left mouse button is pressed, the star has to move to the clicked position and calculates the center point of the position and draw the line. But if I run these codes, the movement trajectory is not drawn.</p>
  
  <p>Please let me know what the problem is. Plus, the star must move at a constant speed to the clicked location. (Not teleportation)</p>
  
  <p>Following is the full codes:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;GL/glut.h&gt;
  
  float v1[3] = { -35.0f,  22.5f, 0.0f };
  float v2[3] = { -35.0f, -22.5f, 0.0f };
  float v3[3] = { 0.0f,  42.5f, 0.0f };
  float v4[3] = { 0.0f, -42.5f, 0.0f };
  float v5[3] = { 35.0f,  22.5f, 0.0f };
  float v6[3] = { 35.0f, -22.5f, 0.0f };
  
  float px, py;
  float center_s[3] = { 0.0f, 0.0f, 0.0f };
  float center_d[3] = { px, py, 0.0f };
  
  static GLfloat spin = 0.0;
  
  float x = 400.0f, y = 442.5f;
  
  float color1[3] = { 1.0f, 1.0f, 1.0f };
  float color2[3] = { 1.0f, 1.0f, 1.0f };
  
  int mode = 1;
  int rotate = 1;
  
  void init(void);
  void triangle_1(void);
  void triangle_2(void);
  void lines(void);
  void display(void);
  void spinDisplay_1(void);
  void spinDisplay_2(void);
  void reshape(int, int);
  void changeColor(int);
  void mouse(int, int, int, int);
  
  ////////////////////////////////////////////////////////////////////
  
  int main(int argc, char **argv) {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
      glutInitWindowSize(500, 500);
      glutInitWindowPosition(300, 300);
      glutCreateWindow("6-Point Star");
  
      init();
  
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutMouseFunc(mouse);
      glutMainLoop();
  
      return 0;
  }
  
  ////////////////////////////////////////////////////////////////////
  
  void init(void) {
      glClearColor(0.0, 0.0, 0.0, 0.0);
      glShadeModel(GL_FLAT);
  }
  
  void triangle_1(void) {
      glColor3fv(color1);
  
      glBegin(GL_TRIANGLE_FAN);
      glVertex3fv(v1);
      glVertex3fv(v4);
      glVertex3fv(v5);
  
      glEnd();
  }
  
  void triangle_2(void) {
      glColor3fv(color2);
  
      glBegin(GL_TRIANGLE_FAN);
      glVertex3fv(v2);
      glVertex3fv(v3);
      glVertex3fv(v6);
  
      glEnd();
  }
  
  void lines(void) {
      glColor3f(1.0, 0.0, 0.0);
  
      glPointSize(5);
      glBegin(GL_POINTS);
      glVertex3fv(center_s);
  
      glLineWidth(1);
      glBegin(GL_LINES);
      glVertex3fv(center_s);
      glVertex3fv(center_d);
  }
  
  void display(void) {
      glClear(GL_COLOR_BUFFER_BIT);
      glPushMatrix();
  
      glTranslatef(x, y, 0.0f);
      glRotatef(spin, 0.0, 0.0, 1.0);
  
      triangle_1();
      triangle_2();
  
      glPopMatrix();
  
      glutSwapBuffers();
  }
  
  void spinDisplay_1(void) {
      spin = spin + 2.0;
  
      if (spin &gt; 360.0) {
          spin = spin - 360.0;
      }
  
      glutPostRedisplay();
  }
  
  void spinDisplay_2(void) {
      spin = spin - 2.0;
  
      if (spin &lt; 360.0) {
          spin = spin + 360.0;
      }
  
      glutPostRedisplay();
  }
  
  void reshape(int w, int h) {
      glViewport(0, 0, (GLsizei)w, (GLsizei)h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(0.0, 500.0, 0.0, 500.0);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
  }
  
  void changeColor(int n) {
      if (n == 1) {
          color1[0] = 0.0f, color1[1] = 0.0f, color1[2] = 1.0f;
          color2[0] = 0.0f, color2[1] = 1.0f, color2[2] = 0.0f;
      }
      else if (n == 2) {
          color1[0] = 1.0f, color1[1] = 1.0f, color1[2] = 1.0f;
          color2[0] = 1.0f, color2[1] = 1.0f, color2[2] = 1.0f;
      }
  }
  
  void mouse(int button, int state, int mx, int my) {
      switch (button) {
      case GLUT_LEFT_BUTTON:
          if (state == GLUT_DOWN) {
              x = mx;
              y = glutGet(GLUT_WINDOW_HEIGHT) - my;
              px = x + 35.0;
              py = y + 42.5;
              glutIdleFunc(lines);
          }
          glutPostRedisplay();
          break;
      case GLUT_MIDDLE_BUTTON:
          if (state == GLUT_DOWN) {
              if (mode == 1) {
                  changeColor(mode);
                  mode = 2;
              }
              else if (mode == 2) {
                  changeColor(mode);
                  mode = 1;
              }
          }
          glutPostRedisplay();
          break;
      case GLUT_RIGHT_BUTTON:
          if (state == GLUT_DOWN)
              if (rotate == 1) {
                  glutIdleFunc(spinDisplay_1);
                  rotate = 2;
              }
              else if (rotate == 2) {
                  glutIdleFunc(spinDisplay_2);
                  rotate = 1;
              }
          break;
      default:
          break;
      }
  }
  </code></pre>