java - How to make the camera move in the direction of the rotation? -


how can make camera move in direction of rotation? how calculate position of camera follow rotation? 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 place next shape //will drawn.         gl.gltranslated(2, 0, 2); //we use wire here because default //lighting not enough //use when rendering solid version         glut.glutwireicosahedron(); //more shapes 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) {      } } 

p.s. have click canvas things start working.

elect right, have understand basic concepts, can't tinker no understanding of notions on computer graphics. can read opengl red book, code examples have been ported jogl , available on github here.

those classes of own first person shooter's oldest alpha version show how implement camera except cannot or down: gameglview gamecontroller gamemodel

p.s: have update make work jogl 2.3.2 , later. works jogl 2.3.1.

p.p.s: updated source code above make work jogl 2.3.2 yesterday (february, 4th, 2016).


Comments