How to make Grid out of Boxes (3D)

great and nice working

with keyboard in-order to add all variable when you have a time make it if it dose not bother you

Can you please tell me more?

Do you mean click cube by mouse and change with keys?

I am on a journey now, maybe Monday

all with keyboard as menu when you finish it i am going to try to replace the keyboard with sliders

have fun no matter when you have time it is fine :kissing_heart: :kissing_heart:

How selection a cube with keyboard?

within its number in the cell

Here is an example.

  • You can click cursor left / right to select a cube

  • You can click cursor up/ down to change the height of the selected cube

  • You can click 0…9 to select cube number directly.

Chrisir

Cube[] cubes;

float angle1; 

int [][] gridHeight =  {
  { 1, 2, 3, 4 }, 
  { 8, 7, 6, 5 }, 
  { 8, 2, 8, 2 }, 
  { 1, 2, 3, 4 }
};  

int selected; 

void setup() { 
  size(1600, 960, P3D);

  int unit = 90; 
  int count; 

  int rows = 4; 
  int columns = 4;

  count =  rows * columns;  // wideCount * highCount * depthCount; 
  cubes = new Cube[count];

  // setting up the grid 
  int index = 0; 
  for (int z = 0; z < rows; z++) {
    //for (int y = 0; y < highCount; y++) { 
    for (int x = 0; x < columns; x++) { 
      cubes[index++] = new Cube(
        x*unit-(4*unit), // X
        0, // Y 
        z*unit-(5*unit), // Z  
        30*gridHeight[x][z], 
        true);                         // visible
    }
    //  }
  }
}

void draw() { 
  background(0); 
  lights();

  translate (width/2, height/2+90, -330);

  //rotateX(-0.4); 
  //rotateZ(angle1); 
  //angle1+=0.004;

  rotateY(map(mouseX, 0, width, -TWO_PI, TWO_PI));
  rotateX(-map(mouseY, 0, height, -TWO_PI, TWO_PI));

  for (int i = 0; i < cubes.length; i++) { 
    if (i==selected) 
      cubes[i].display(true);
    else 
    cubes[i].display(false);
  }
}

void keyPressed() {
  if (key==CODED) {

    switch(keyCode) {
      //
    case UP: 
      cubes[selected].heightCube+=10; 
      break; 

    case DOWN:
      cubes[selected].heightCube-=10;
      break;

    case LEFT:
      selected--;
      if (selected<0) 
        selected=cubes.length-1; 
      break; 

    case RIGHT:
      selected++;
      if (selected>cubes.length-1) 
        selected=0; 
      break;
    }

    return;
  }

  // ---

  if (key>='0'&&key<='9') {
    selected = int(key+"");
  }
}


// ==============================================================================================================

class Cube { 
  int posX; 
  int posY; 
  int posZ; 

  color col = color(255, 0, 0); // color(random(255), random(255), random(255));
  boolean exist = false; 

  float sizeCube = 90;

  float heightCube = 10 + int(random(1) * 300); 

  // Contructor 
  Cube(int xOffsetTemp, int yOffsetTemp, int zOffsetTemp, 
    float d, 
    boolean existTemp) { 
    posX = xOffsetTemp; 
    posY = yOffsetTemp;
    posZ = zOffsetTemp;

    heightCube = d;  

    exist = existTemp;
  }// Contructor 

  // Custom method for drawing the object 
  void display( boolean isSelected) { 
    if (exist) {
      stroke(0); 

      if (isSelected) 
        fill(0, 255, 0);
      else
        fill(col);

      pushMatrix();
      translate(posX, posY + heightCube/2, posZ);
      box(sizeCube, sizeCube + heightCube, sizeCube);
      popMatrix();
    }//if
  }//method
  //
} //class
//

1 Like

@Chrisir
thanks for you and thanks for your time

1 Like

@ Chrisir
I keep tracking your posts with previous versions of the forum. Always you have cooperation and help others. I saw you comments that span many years ago.

Bless you and grateful for your continuous efforts

1 Like