First Person Camera

This camera rotates based on the mouse, and you can move in the direction you are looking using the w,s,a,d,q,z keys.

(This code assumes you have the Shape, Group and box classes)

/* a first person shooter type camera */
import processing.opengl.*;
Group graph;
// variables for the current position and rotation of the camera
PVector cameraPos, cameraRotation;
void setup()
   size(600, 600, OPENGL); 
   cameraPos = new PVector(0, 0);
   cameraRotation = new PVector(0, 0);
   zoom = 0.0f;
  graph = new Group();
  Box box1 = new Box();
  box1.setPosition(new PVector(0, 0, 0));
  box1.setRotation(new PVector(0, 45, 0));
  box1.setScale(new PVector(200, 50, 100));
  Box box2 = new Box();
  box2.setPosition(new PVector(-300, 0, 0));
  box2.setScale(new PVector(100, 100, 100));
  Box box3 = new Box();
  box3.setPosition(new PVector(300, 0, 0));
  box3.setScale(new PVector(100, 100, 100));
 // Box box2 = new Box();
  //box2.setPosition(new PVector(0, 0, 0));
  //box2.setScale(new PVector(200, 100, 100));
// we need to move in the direction that the camera is facing
// tThat means that any camera rotation has to take the current camera rotation into account
// We have to rotate any movement vectors by the current camera rotation
// GL doesn't have a built in method to do this so this is an implementation based on the
// rotation equations
PVector transformDirection(PVector dir)
   PVector newDir = new PVector();
   // rotate about y
   newDir.x = dir.x*cos(radians(cameraRotation.y)) - dir.z*sin(radians(cameraRotation.y)); 
   newDir.z = dir.x*sin(radians(cameraRotation.y)) + dir.z*cos(radians(cameraRotation.y)); 
   // rotate about x
   newDir.y =  dir.y*cos(radians(cameraRotation.x)) + newDir.z*sin(radians(cameraRotation.x)); 
   newDir.z = -dir.y*sin(radians(cameraRotation.x)) + newDir.z*cos(radians(cameraRotation.x));
   return newDir; 
void draw()
  //directionalLight(255, 255, 255, -1, 0, 0);   
  // camera control
  // w/s for in and out
  // a/d for left right strafe
  // q/z for up and down
  // We use the transformDirection method to rotate all movement
  // vectors by the current camera rotation and then add them to 
  // the current camera position
  if (keyPressed)
   if (key == 'w')
     cameraPos.add(transformDirection(new PVector(0.0f, 0.0f, -10.0f)));
   if (key == 's')
     cameraPos.add(transformDirection(new PVector(0.0f, 0.0f,  10.0f)));
   if (key == 'a')
     cameraPos.add(transformDirection(new PVector(-10.0f, 0.0f, 0.0f)));
   if (key == 'd')
     cameraPos.add(transformDirection(new PVector( 10.0f, 0.0f, 0.0f)));
   if (key == 'q')
     cameraPos.add(transformDirection(new PVector(0.0f, -10.0f, 0.0f)));
   if (key == 'z')
     cameraPos.add(transformDirection(new PVector(0.0f,  10.0f, 0.0f)));
  // rotate the camera based on the mouse
  if (mousePressed)
    cameraRotation.x += -float(mouseY-pmouseY);
    cameraRotation.y += float(mouseX-pmouseX);
  PGraphicsOpenGL pgl = (PGraphicsOpenGL) g;  // g may changes
  GL gl = pgl.beginGL();  // always use the GL object returned by beginGL
  // work on the camera transforms
  // set the transform to the identity (do nothing)
  // use gluLook at to get it looking along z and using y as up
  pgl.glu.gluLookAt(0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  // these are the camera transforms
  // in this order they move they rotate the camera about its position
  gl.glRotatef(cameraRotation.x, 1, 0, 0);
  gl.glRotatef(cameraRotation.y, 0, 1, 0);
  gl.glTranslatef(cameraPos.x, cameraPos.y, cameraPos.z);
PDF Printer    Send article as PDF to