Why is my 3D object alignment off?

I can't figure out why these 2 rows of boxes aren't perfectly aligned to one another the way the boxes in the rows are... in other words, why is there a gap between the rows? Any help would be much appreciated. Thanks.

int dim = 50; 

void setup() {
  size(400, 200, P3D);
  background(255);
  smooth(4);
}

void draw() {
  lights();

  fill(255, 128);
  stroke(255, 128);
  ortho(0, width, 0, height); // same as ortho()

  pushMatrix();
  translate(100, 100, 0);
  rotateX(-PI/6);
  rotateY(PI/6);
  box(dim);
  translate(dim, 0, 0);
  box(dim);
  translate(dim, 0, 0);
  box(dim);
  translate(dim, 0, 0);
  box(dim);
  translate(dim, 0, 0);
  box(dim);
  popMatrix();

  pushMatrix();
  translate(100, 100+dim, 0);
  rotateX(-PI/6);
  rotateY(PI/6);
  box(dim);
  translate(dim, 0, 0);
  box(dim);
  translate(dim, 0, 0);
  box(dim);
  translate(dim, 0, 0);
  box(dim);
  translate(dim, 0, 0);
  box(dim);
  popMatrix();
}

Answers

  • Line 47 dim/2

  • Thanks for the reply. There is no line 47 in the code so I'm not clear what you're suggesting. Can you please elaborate? Thanks.

  • Answer ✓

    The problem is that the 'row-moving translation' should be done after the rotation. See the adapted code. Also, you should really look into for loops. Then you can shorter your code a lot.

    Adapted Code

    int dim = 50;
    
    void setup() {
      size(400, 200, P3D);
      background(255);
      smooth(4);
    }
    
    void draw() {
      lights();
    
      fill(255, 128);
      stroke(255, 128);
      ortho(0, width, 0, height); // same as ortho()
    
      pushMatrix();
      translate(100, 100, 0);
      rotateX(-PI/6);
      rotateY(PI/6);
      box(dim);
      translate(dim, 0, 0);
      box(dim);
      translate(dim, 0, 0);
      box(dim);
      translate(dim, 0, 0);
      box(dim);
      translate(dim, 0, 0);
      box(dim);
      popMatrix();
    
      pushMatrix();
      translate(100, 100, 0);
      rotateX(-PI/6);
      rotateY(PI/6);
      translate(0, dim, 0);
      box(dim);
      translate(dim, 0, 0);
      box(dim);
      translate(dim, 0, 0);
      box(dim);
      translate(dim, 0, 0);
      box(dim);
      translate(dim, 0, 0);
      box(dim);
      popMatrix();
    }
    
  • Thank you!

Sign In or Register to comment.