Keep the object top

Hi i just learned processing and i just made a 3D sketch and there is a beam on it. what puzzles me is when the cursor I pointed down the block disappears, what I want is to keep the beam always above the grid. Any suggestion would be greatly appreciated.

int baris, kolom;
int kotak = 40;
int w = 2200;
int h = 800;

void setup() {
  fullScreen(P3D); // 1366 x 768
  baris = h / kotak;
  kolom = w / kotak;
}

void draw() {
  background(255);
  axisPlot();
  objek();
}

void axisPlot() {
  pushMatrix();
  stroke(255);
  fill(#D6D6D6);
  translate(width/2, height/2);
  rotateX(PI/3);
  translate(-w/2, -height/2);
  for (int y = 0; y < baris; y++) {
    beginShape(TRIANGLE_STRIP);
    for (int x = 0; x < kolom; x++) {
      vertex(x * kotak, y * kotak);
      vertex(x * kotak, (y+1) * kotak);
    }
    endShape();
  }
  popMatrix();
}

void objek() {
  pushMatrix();
  lights();
  noStroke();
  fill(246, 225, 65);
  translate(mouseX, mouseY);
  rotateX(-PI/6);
  box(100, 50, 150);
  popMatrix();
}

Answers

  • Both your box (which follows the mouse) and your triangle strip (your white grid) exist in 3D space, and from the view point of the camera, the box gets hidden by the strip because the box passes through it. Imagine a handkerchief on a clear glass tabletop - you can hold a matchbox above the handkerchief and see it, but you can also hold it below the table where your view of it is hidden (blocked by the handkerchief) - that's what's happening in your sketch!

    There are a couple of ways you could fix this. For one, restrict the box so that it's position is always above the plane of the strip (handkerchief stays above table). Or you could draw the strip and then save an image of it, and then use that image as the background for your sketch (this might look a little weird though).

  • Thanks @TfGuy44 for your suggestion. Can you give a sample code to keep the box always on top?

  • Using which approach?

  • edited November 2017

    Forgive me, I dont get it, what approach do you mean? I want the yellow beam to be always above.

  • No. Ask me why.

Sign In or Register to comment.