title: How to make the camera move in the direction of the rotation?
body: <p>How can I make the camera move in the direction of the rotation? How do I calculate the position of the camera to follow the rotation? 
  This is my code: </p>
  
  <pre><code>import javax.media.opengl.*;
  import javax.media.opengl.awt.*;
  import com.sun.opengl.util.*;
  import com.sun.opengl.util.gl2.GLUT;
  import javax.media.opengl.glu.*;
  
  import java.awt.event.*;
  import java.awt.*;
  import javax.swing.*;
  
  public class Rotation2 extends JFrame implements GLEventListener, KeyListener
  {
      GLCanvas canvas = null;
      Animator an;
  
      public Rotation2()
      {   
          canvas=new GLCanvas();
          an=new Animator(canvas);
          add(canvas);
          canvas.addGLEventListener(this);
          canvas.addKeyListener(this);
          setSize(1280,900);
          setVisible(true);
          setDefaultCloseOperation(EXIT_ON_CLOSE);
          an.start();
          requestFocus();
  
      }
      float xPosition = 0;
      float zPosition = 0;
  
      float red = 0;
      float green = 0;
      float blue = 1;
  
  
      public void init(GLAutoDrawable drawable)
      {
          GL2 gl = drawable.getGL().getGL2();
          GLU glu = new GLU();
          gl.glMatrixMode(GL2.GL_PROJECTION);
          gl.glLoadIdentity();
  
          double w = drawable.getWidth();
          double h = drawable.getHeight();
          double aspect = w/h;
          glu.gluPerspective(60.0, aspect, 2.0, 20.0);
  
      }
      double zRot = 0;
      double xRot = 0;
  
      public void display(GLAutoDrawable drawable)
      {
          GL2 gl=drawable.getGL().getGL2();
          GLU glu = new GLU();
          GLUT glut = new GLUT();
  
          gl.glMatrixMode(GL2.GL_MODELVIEW);
          gl.glLoadIdentity();
          gl.glClearColor(0f,0f,0f,0f);
          //xPosition+=0.005;
          //zPosition+=0.005;
          gl.glRotated(zRot, 0, 1, 0);
          gl.glRotated(xRot, 1, 0, 0);
          glu.gluLookAt(xPosition, 0, zPosition,
                        xPosition, 0, (zPosition+20),
                        0, 1, 0);
  
          gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
  
          red = 0.0f;
          green = 0.0f;
          blue = 0.9f;
          gl.glColor3f(red, green, blue);
  //transforming the place the next shape
  //will be drawn.
          gl.glTranslated(2, 0, 2);
  //We use wire here because default
  //lighting is not good enough to
  //use when rendering the solid version
          glut.glutWireIcosahedron();
  //more shapes to navigate through
          gl.glTranslated(-4, 0, 0);
          glut.glutWireIcosahedron();
          red = 0.0f;
          green = 0.9f;
          blue = 0.1f;
          gl.glColor3f(red, green, blue);
          gl.glTranslated(4, 0, 4);
          glut.glutWireIcosahedron();
          gl.glTranslated(-4, 0, 0);
          glut.glutWireIcosahedron();
          red = 0.9f;
          green = 0.0f;
          blue = 0.1f;
          gl.glColor3f(red, green, blue);
          gl.glTranslated(4, 0, 4);
          glut.glutWireIcosahedron();
          gl.glTranslated(-4, 0, 0);
          glut.glutWireIcosahedron();
          red = 0.9f;
          green = 0.0f;
          blue = 0.9f;
          gl.glColor3f(red, green, blue);
          gl.glTranslated(4, 0, 4);
          glut.glutWireIcosahedron();
          gl.glTranslated(-4, 0, 0);
          glut.glutWireIcosahedron();
  
  
      }
  
      public void reshape(GLAutoDrawable drawable,int x,int y,int width,int height)
      {}
      public void dispose(GLAutoDrawable drawable)
      {}
  
  
      public static void main(String[] ar)
      {
  
          new Rotation2();
      }
  
      @Override
      public void keyTyped(KeyEvent e) {
  
      }
  
      @Override
      public void keyPressed(KeyEvent e) {
          if (e.getKeyCode() == KeyEvent.VK_W) {
              zPosition++;
          }
          else if (e.getKeyCode() == KeyEvent.VK_S) {
              zPosition--;
          }
          else if(e.getKeyCode() == KeyEvent.VK_A) {
              xPosition++;
          }
          else if (e.getKeyCode() == KeyEvent.VK_D) {
              xPosition--;
          }
          if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
              zRot+=5;
          }
          else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
              zRot-=5;
          }
          else if (e.getKeyCode() == KeyEvent.VK_UP) {
              xRot-=5;
          }
          else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
              xRot+=5;
          }
          canvas.repaint();
      }
  
      @Override
      public void keyReleased(KeyEvent e) {
  
      }
  }
  </code></pre>
  
  <p>p.s. You have to click the canvas for things to start working.</p>