How can I make the camera move to the direction of which I’m rotating?
I use W,A,S,D to move, UP and DOWN to rotate.
I’m drawing a quad as wall but it just blocks everything when I move around.
You have to click the windows to gain focus.
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, 200.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);
gl.glColor3f(1, 1, 1);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex3i(0, 100, -500);
gl.glVertex3i(0, 100, 0);
gl.glVertex3i(500, 100, 0);
gl.glVertex3i(500, 100, -500);
gl.glEnd();
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.glutSolidIcosahedron();
gl.glBegin(GL2.GL_QUADS);
// Right-face
gl.glColor3f(100.0f, 0.0f, 100.0f); // violet
gl.glVertex3f(100.0f, 100.0f, -100.0f);
gl.glVertex3f(100.0f, 100.0f, 100.0f);
gl.glVertex3f(100.0f, -100.0f, 100.0f);
gl.glVertex3f(100.0f, -100.0f, -100.0f);
gl.glEnd();
}
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) {
}
}