"parent" an object to a camera

edited June 2015 in How To...

Hello! I am trying to create some customised GUI. I am also using a camera() which puts all my GUI elements into 3D.

Can I link this GUI elements to the front of the camera, so it will be at same place even if camera gets moved? any other way to work around this?

This picture maybe can show what I mean:

The code, so far. I am using peasyCam but its just for easier visualisation: import peasy.*;

PeasyCam cam;

void setup(){
  size(360,240,P3D);
  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(500);
}

void draw(){


  background(100);
  float[] position = cam.getPosition();
  float[] rotation = cam.getRotations();
  pushMatrix();
    translate(position[0]-90,position[1]-50,position[2]-110);
    //rotateX(rotation[0]);
    //rotateY(rotation[1]);
    //rotateZ(rotation[2]);
    textSize(10);
    text(String.format("camera pos [%.1f,%.1f,%.1f]",  position[0],position[1],position[2]),10,10);
  popMatrix();

  beginShape(POINTS);
  vertex(0,-30,0);
  endShape();

  rect(-20,-20,40,40);

  beginShape(LINES);
  vertex(0,0,-20);
  vertex(0,0,10);
  endShape();
}

Best

A

Screen Shot 2015-06-10 at 09.19.37

Answers

  • edited June 2015

    Ok, life can be easier.... of course this problem is not new.

    Since I am not sure if I can delete this post, I will leave my code for reference.

    import peasy.*;
    
    PeasyCam cam;
    //PGraphics pg;
    
    void setup(){
      size(200,100,P3D);
      cam = new PeasyCam(this, 100);
      cam.setMinimumDistance(30);
      cam.setMaximumDistance(500);
      //pg = createGraphics(width, height);
    }
    
    void draw(){
    
      background(100);
      float[] position = cam.getPosition();
      float[] rotation = cam.getRotations();
    
      beginShape(POINTS);
      vertex(0,-30,0);
      endShape();
      fill(255);
      rect(-20,-20,40,40);
    
      beginShape(LINES);
      vertex(0,0,-20);
      vertex(0,0,10);
      endShape();
    
      String s = String.format("camera pos [%.1f,%.1f,%.1f]",  position[0],position[1],position[2]);
    
      cam.beginHUD();
        fill(0);
        text(s,10,10);
        line(0, 0, width, height);
      cam.endHUD();
    
    }
    
    
    /*
      pushMatrix();
        camera();
        hint(DISABLE_DEPTH_TEST);
        pg.beginDraw();
        pg.smooth();
        pg.background(100);
        pg.stroke(255);
        pg.line(0, 0, width, height);
        pg.text(s,10,10);
        pg.endDraw();    
        image(pg,0, 0); 
      popMatrix();
    */
    
  • Thank you Chrisir!

    I wasn't aware of the use of hint(DISABLE_DEPTH_TEST). Also, PeasyCam includes a method called beginHUD() endHUD().

Sign In or Register to comment.