Matrix Transformation Question

Hi there,

I'm trying to complete this exercise which involves controlling an avatar through a forest using matrix transformation. However when I press the Space button to move the view back to the camera (as opposed to FPV) I can't get it to follow the avatar.

Could someone please point me in the right direction?

Many thanks, Charles

//Avatar eye height
int viewHeight = 100;

//matrix to capture position and orientation of match
PMatrix3D eye = new PMatrix3D();

Boolean record = false;
Boolean view = true;

int treeCount = 400;
float[] treeX = new float[treeCount];
float[] treeY = new float[treeCount];
float [] treeH = new float[treeCount];

void setup()
{
  //3D rendering
  size(700, 500, P3D);
  //white fill
  fill(255);
  strokeWeight(0.5);

  //translate eye matrix
  eye.translate(100, 100, viewHeight);
  eye.rotate(PI/6);

  //initialize tree position and height
  for (int i = 0; i < treeCount; i++) {
    treeX[i] = random(width*-5, width*5);
    treeY[i] = random(width*-5, width*5);
    treeH[i] = random(200, 1000);
  }
}

void draw()
{  
  background(255);
  camera(500, 0, 200, 0, 0, 125, 0, 0, -1);

  /*
        Create a new matrix to transform the ****whole scene****
   except the match, which we're not drawing
   because we're in the head of the match!
   */
  PMatrix3D camera = new PMatrix3D();

  //(when we apply this, it will) drag the origin up to the camera
  camera.translate(500, 0, 200);

  /*
        Invert the match transformation; or, move the whole scene
   so the origin is at the avatar and it's
   */
  PMatrix3D inv =  new PMatrix3D(eye);
  inv.invert();
  camera.apply(inv);

  if (view) { // FPV state
    applyMatrix(camera);
    for (int i = 0; i < treeCount; i++) {
      drawTree(treeX[i], treeY[i], treeH[i]);
    }
    drawGrid();
  } else { // camera state
    for (int i = 0; i < treeCount; i++) {
      drawTree(treeX[i], treeY[i], treeH[i]);
    }

    drawGrid();
    applyMatrix(eye);
    avatar();
  }

  recordFrames();
  movements();
}

void drawGrid() {
  //how far should the lines go?
  int horizon = width*5;
  //line separation
  int sep = 100;

  stroke(100);
  for (int i=-horizon; i<horizon; i += sep)
  {
    //lines in x-direction
    line(horizon, i, -horizon, i);
    //lines in y-direction
    line(i, -horizon, i, horizon);
  }
}

//create a simple avatar
void avatar() {

  noStroke();
  fill(200);
  pushMatrix();
  sphere(20);
  //start at the top - translate down half the height of the match
  translate(0, 0, -viewHeight/2);
  //draw the stick
  fill(150);
  box(20, 20, viewHeight);
  popMatrix();
}

void keyPressed() {

  if (key=='f') saveFrame("images/image.png");
  if (key=='r') record = !record;
  if (key==' ') view = !view;
}

void movements() {
  if (keyPressed && keyCode==LEFT) eye.rotateZ(-0.05);
  if (keyPressed && keyCode==RIGHT) eye.rotateZ(0.05);
  if (keyPressed && keyCode==UP) eye.translate(-10, 0, 0);
  if (keyPressed && keyCode==DOWN) eye.translate(10, 0, 0);
}

void recordFrames() {
  if (record) saveFrame("frame_#####.png");
}

void drawTree(float x, float y, float h) {

  noStroke();
  fill(#715448);
  pushMatrix();
  translate(x, y, h/2);
  box(h*0.06, h*0.03, h);
  popMatrix();
  pushMatrix();
  translate(x, y, h);
  fill(#7EFF8C);
  sphere(h*0.3);
  popMatrix();
}
Sign In or Register to comment.