Camera move on z

Hi!
Im trying to make camera moving on z axis (zooming in and out) so the text will be visible or not

So for example i have this text: 123 and when I zoom in i wont see it, I will just have some random letters, but when I zoom out i will have my text again

String [] abc = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};


int n = 0; 



void setup () {

  size (1280, 720, P3D);
  background(0);
  camera(width/2.0, height/2.0, ((height/2.0) / tan(PI*30.0 / 180.0)) , width/2.0, height/2.0, 0, 0, 1, 0);
}

void draw () {
  
}




void mouseDragged () {

  float r = random (0, 100);

  textSize(32);
  text (abc[int(random(0, 25))], mouseX, mouseY, r);
}


void keyPressed(){

  if (key == '=') {
    n ++;
  } else if (key == '-') {
  
    n --;
  }

}

hi! welcome to the forum!

I guess you are using n to control the zooming but it’s not used in the code you posted - can you check if that’s the right version?

My initial guess was because of the clipping plane - objects closer than the “near” plane and farther than the “far” plane will not show up
https://processing.org/reference/frustum_.html

But in your case you are drawing it in mouseDragged so perhaps it’s unrelated. What is previously drawn in mouseDragged won’t be affected by camera - it’s like afterimage and the object is not there anymore. If you want the previous texts to be affected by the camera position, you need to store them (the random value and position) in an arraylist and draw all of them every frame with the updated camera parameters.

1 Like

to avoid clipping just call this:

void avoidClipping() {
  // avoid clipping (at camera): 
  // https : // 
  // forum.processing.org/two/discussion/4128/quick-q-how-close-is-too-close-why-when-do-3d-objects-disappear
  perspective(PI/3.0, (float) width/height, 1, 1000000);
}//func 

Remark

in theory, you need to use background at the start of draw(), then set camera with n as your eye-Z and the avoidClipping

Of course, with background at the start of draw(), you need to redraw all your letters in a loop in draw(), so you need to store them in a class and an ArrayList

1 Like

here is an example

place mouse in lower part of the Sketch, drag up and down



float camZ ; 

void setup() {
  size(1640, 860, P3D);

  // camZ =  ((height/2.0) / tan(PI*30.0 / 180.0));
}

void draw() {
  avoidClipping(); 
  background(152);
  lights();

  camZ =  map(mouseY, 0, height, -300, 300);

  camera(width/2.0, height/2.0, camZ, 
    width/2.0, height/2.0, camZ-200, 
    0, 1, 0);

  drawFloor(); 

  textMode(SHAPE);
  textAlign(CENTER, CENTER);
  fill(255); 
  text("Hello", width/2, height/2);
}

// ---------------------------------------------------------------------------
// Tools

void avoidClipping() {
  // avoid clipping (at camera): 
  // https : // 
  // forum.processing.org/two/discussion/4128/quick-q-how-close-is-too-close-why-when-do-3d-objects-disappear
  perspective(PI/3.0, (float) width/height, 1, 1000000);
}//func 

void drawFloor() {

  pushMatrix();

  //stroke(111);  // gray 
  noStroke(); 
  fill(0, 2, 255); // blue
  float factor1=80.0; // dist between boxes

  translate(width/2, height/2, 0); 

  for (int x=-55; x<55; x++) {
    for (int z=-55; z<55; z++) {

      pushMatrix(); 
      translate(x*factor1 - (4*factor1/2), 
        height-111, 
        z*factor1 - (4*factor1/2) );
      fill(map(z, -55, 55, 100, 255), 0, 0);
      box(40);  // size of 40 (width, height and depth) 
      popMatrix();
    }
  }
  popMatrix();
}
//

1 Like

oh yea i see. I had n there:
camera(width/2.0, height/2.0, ((height/2.0) / tan(PI*30.0 / 180.0) + n) , width/2.0, height/2.0, 0, 0, 1, 0);

I didnt know that it wont affect the mouseDraged things. Thx a lot!

Thanks a lot! Will try to rewrite it. It turns out to be more tricky than I thought :stuck_out_tongue:

1 Like

camera command needs to be at the start of draw()