Array problem (cube grid)

edited February 2016 in How To...

I've been trying to put the boxes in an order like the way in the picture but so far all the attempts failed. I can use some help from here ^^. It works when i just use random position but I need them as a grid.

https://dropbox.com/s/o4uqt074bx8mjo9/saved.png?dl=0

ArrayList<Cube> myCubes = new ArrayList<Cube>();

void setup() {
  size(800, 800, P3D);
  frameRate(60);
  for (int i = 0; i < 30; i++){
    myCubes.add(new Cube());
  }

}

void draw() {
  background(0);
  for (Cube myCube : myCubes) {
    myCube.display();
  }
}

class Cube {
  PImage tex, tex1, tex2, tex3, tex4, tex5;
  float x;
  float y;
  float scale;

  public Cube() {
    this.x = random(width);
    this.y = random(height);
    this. scale = 30;

    tex = loadImage("image0.jpg");
    tex1 = loadImage("image1.jpg");
    tex2 = loadImage("image2.jpg");
    tex3 = loadImage("image3.jpg");
    tex4 = loadImage("image4.jpg");
    tex5 = loadImage("image5.jpg");
  }

  void display() {
    noStroke();
    pushMatrix();
    translate(x, y, 0);
    rotateY(map(mouseX, 0, width, 0, PI));
    rotateX(map(mouseY, 0, height, 0, PI));
    scale(scale);


    textureMode(NORMAL);

    beginShape(QUADS);
    texture(tex);

    // +Z "front" face
    vertex(-1, -1, 1, 0, 0);
    vertex( 1, -1, 1, 1, 0);
    vertex( 1, 1, 1, 1, 1);
    vertex(-1, 1, 1, 0, 1);
    endShape();
    beginShape(QUADS);
    // -Z "back" face
    texture(tex1);
    vertex( 1, -1, -1, 0, 0);
    vertex(-1, -1, -1, 1, 0);
    vertex(-1, 1, -1, 1, 1);
    vertex( 1, 1, -1, 0, 1);

    endShape();
    beginShape(QUADS);
    // +Y "bottom" face
    texture(tex2);
    vertex(-1, 1, 1, 0, 0);
    vertex( 1, 1, 1, 1, 0);
    vertex( 1, 1, -1, 1, 1);
    vertex(-1, 1, -1, 0, 1);

    endShape();
    beginShape(QUADS);
    // -Y "top" face
    texture(tex3);
    vertex(-1, -1, -1, 0, 0);
    vertex( 1, -1, -1, 1, 0);
    vertex( 1, -1, 1, 1, 1);
    vertex(-1, -1, 1, 0, 1);

    endShape();
    beginShape(QUADS);
    // +X "right" face
    texture(tex4);
    vertex( 1, -1, 1, 0, 0);
    vertex( 1, -1, -1, 1, 0);
    vertex( 1, 1, -1, 1, 1);
    vertex( 1, 1, 1, 0, 1);

    endShape();
    beginShape(QUADS);
    // -X "left" face
    texture(tex5);
    vertex(-1, -1, -1, 0, 0);
    vertex(-1, -1, 1, 1, 0);
    vertex(-1, 1, 1, 1, 1);
    vertex(-1, 1, -1, 0, 1);
    endShape();
    popMatrix();
  }
}

Answers

  • Answer ✓
    ArrayList<Cube> myCubes = new ArrayList<Cube>();
    
    void setup() {
      size(800, 800, P3D);
      //frameRate(60);
      for (int i = 0; i < 30; i++) {
        for (int j = 0; j < 30; j++) {
          myCubes.add(new Cube(  i * 44+23, j * 44 +23 ));
        }
      }
    }
    
    void draw() {
      background(0);
      for (Cube myCube : myCubes) {
        myCube.display();
      }
    }
    
    // ==================================
    
    class Cube {
      PImage tex, tex1, tex2, tex3, tex4, tex5;
      float x;
      float y;
      float scale;
    
      public Cube (  float x, float y ) {
        //this.x = random(width);
        //this.y = random(height);
        this.x = x;
        this.y = y;
        this. scale = 5.6;
    
        //tex = loadImage("image0.jpg");
        //tex1 = loadImage("image1.jpg");
        //tex2 = loadImage("image2.jpg");
        //tex3 = loadImage("image3.jpg");
        //tex4 = loadImage("image4.jpg");
        //tex5 = loadImage("image5.jpg");
      }
    
      void display() {
        //    noStroke();
        strokeWeight(.2); 
        pushMatrix();
        translate(x, y, 0);
        rotateY(map(mouseX, 0, width, 0, PI));
        rotateX(map(mouseY, 0, height, 0, PI));
        scale(scale);
    
    
        textureMode(NORMAL);
        fill(255, 2, 2); 
        beginShape(QUADS);
        // texture(tex);
    
        // +Z "front" face
        vertex(-1, -1, 1, 0, 0);
        vertex( 1, -1, 1, 1, 0);
        vertex( 1, 1, 1, 1, 1);
        vertex(-1, 1, 1, 0, 1);
        endShape();
        beginShape(QUADS);
        // -Z "back" face
        //   texture(tex1);
        vertex( 1, -1, -1, 0, 0);
        vertex(-1, -1, -1, 1, 0);
        vertex(-1, 1, -1, 1, 1);
        vertex( 1, 1, -1, 0, 1);
    
        endShape();
        beginShape(QUADS);
        // +Y "bottom" face
        //  texture(tex2);
        vertex(-1, 1, 1, 0, 0);
        vertex( 1, 1, 1, 1, 0);
        vertex( 1, 1, -1, 1, 1);
        vertex(-1, 1, -1, 0, 1);
    
        endShape();
        beginShape(QUADS);
        // -Y "top" face
        //  texture(tex3);
        vertex(-1, -1, -1, 0, 0);
        vertex( 1, -1, -1, 1, 0);
        vertex( 1, -1, 1, 1, 1);
        vertex(-1, -1, 1, 0, 1);
    
        endShape();
        beginShape(QUADS);
        // +X "right" face
        //   texture(tex4);
        vertex( 1, -1, 1, 0, 0);
        vertex( 1, -1, -1, 1, 0);
        vertex( 1, 1, -1, 1, 1);
        vertex( 1, 1, 1, 0, 1);
    
        endShape();
        beginShape(QUADS);
        // -X "left" face
        //   texture(tex5);
        vertex(-1, -1, -1, 0, 0);
        vertex(-1, -1, 1, 1, 0);
        vertex(-1, 1, 1, 1, 1);
        vertex(-1, 1, -1, 0, 1);
        endShape();
        popMatrix();
      }
    }
    
  • edited February 2016

    thank you for the help but i still have a small problem :D the spacing couldn't figure out the logic of this part

    for (int i = 0; i < 30; i++) {
      for (int j = 0; j < 30; j++) {
        myCubes.add(new Cube( i * 44 + 23, j * 44 + 23));
      }
    }
    
  • edited February 2016 Answer ✓

    it is a nested for loop which means the outer for loop with i is the columns

    and for EACH column we do j rows

    so the inner for loop is done multiple times since it is called again and again by the outer for loop

    Thus we get a grid.

    here we init a new cube and give it its position as parameters x,y:

    ..........new Cube( i * 44+23, j * 44 +23 )
    

    (the first part is x: i * 44+23, then y)

    the 44 is the spacing, the 23 is the distance from screen border

  • edited February 2016

    Everything works perfect now :) thank you a lot. Will it be too hard to process if I use 6 different video for each surface as texture ?

  • Answer ✓

    No

  • Yes

    depends on the video. 6 x 4k videos resized onto a tiny cube is a LOT of processing.

  • well the its really hard and laggy :D running 486 videos at once is killing the computer :D I have 32gb ram and amd 10k processor ssd drive yet still hard :D at least i know the code works :D

Sign In or Register to comment.