Drawing multiple 3D box plots

Hi,
I need to draw a 3D shape, specifically a cube at different locations on a grid and to simulate that, I’m using the randomize function to give me random values for the parameters of “translate()” and also the “box()” function to draw the cube but all I get is the cube jumping from one location to another. I know the reason why, and its basically because as the translate location changes, the box location changes but what I actually want is for the box to stay at the old location and for a new box to be drawn at the new randomized location. Is there any simple solution I could use, because I tried some complicated solutions and I didn’t get anywhere.
Thanks in advance for your help

vec_x = random(grid_size); 
vec_y = random(grid_size); 
vec_z = random(100); 

pushMatrix();
translate(vec_x,vec_y,vec_z);
fill(0);
stroke(4);
box(100);
popMatrix();

your code sniplet looks good,
but not tells the whole story

if you do a

void draw() {
  background(255);

}

you delete all and
might draw a grid and axis and ONE box.
( WHAT looks like jumping )

in that case you must remember the random positions like to a PVector[]
and do them in a loop all over again.

but if you not use the background thing it should work??

1 Like

Hey,
unfortunately, my grid isn’t stationary and I can rotate it in different directions with the mouse so I can’ t get rid of the background(255) and by “jumping” , I mean the box is just redrawn at different locations on the grid.
I tried the idea of saving the locations to a PVector arraylist but it wasn’t redrawing the whole thing. I’ll try it again and post the code anyway to see I’m doing it right

Heey again, I stayed up to work on it and the PVector array worked after I tuned the code up a bit. i also needed to add another “translate” in the negative under the box() function to offset the cumulative nature of the main “translate” function. I got some help on the PVector from this link https://forum.processing.org/one/topic/sort-arraylist-with-pvector-on-distance-to-a-point.html
Thanks so much for your help, I really appreciate it:grinning:

Look into using pushMatrix() and popMatrix() instead of doing the inverse translate.

@kikisarp: could you post your entire code please?

1 Like

instead of

ArrayList<PVector> dots = new ArrayList();

you could use your own class Box3D

ArrayList<Box3D> boxes = new ArrayList();

and add many boxes to the ArrayList

like this (short form):

class Box3D {

  PVector posBox; 

  int sizeBox = int (random(2, 55)); 

  color colorBox = color(random(256), random(256), random(256)); 

  // no constructor required

  void display() {
    pushMatrix();
    translate(posBox.x, posBox.y, posBox.z);
    fill(colorBox);
    box(sizeBox);
    popMatrix();
  }//method
}//class

full sketch without ArrayList :

Box3D box1;

void setup() {
  size(1100, 800, P3D);
  background(0);
  box1 = new Box3D();
}

void draw() {
  background(0);
  box1.display();
}

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

class Box3D {

  float dist = 800;

  PVector posBox = new PVector(
    random(0, dist), 
    random(0, dist), 
    random(-dist, 0)); 

  int sizeBox = int (random(22, 55)); 

  color colorBox = color(random(256), random(256), random(256)); 

  // no constructor required

  void display() {
    pushMatrix();
    //posBox.x=440;
    //posBox.y=440;
    //posBox.z=-330;
    translate(posBox.x, posBox.y, posBox.z);
    fill(colorBox);
    box(sizeBox);
    popMatrix();
  }//method
}//class

hey unfortunately, the entire code is part of a group project but I can post the part about the box creation.

ArrayList<PVector> data_points = new ArrayList<PVector>();        //dynamic PVector array
PVector new_p,old_p;

void setup(){
 size(1200,900,P3D); 
}


void draw(){
new_p = new PVector(random(grid_size),random(grid_size), random(100));
pushMatrix();
if(new_p != old_p)
{
 data_points.add(new_p); 
}
 for(int a = 0; a < data_points.size()-1; a++)
 {
   //data_points.add(new_p);
   new_p = data_points.get(a);
   //vec_x = new_p.x;// - old_p.x;
   //vec_y = new_p.y;// - old_p.y;
   //vec_z = new_p.z ;//- old_p.z;
  translate(new_p.x,new_p.y,new_p.z);

  //println(old_p.x);
  fill(0);
  noStroke();
  box(2);
  translate(-new_p.x,-new_p.y,-new_p.z);
 }
popMatrix();
old_p = new_p;


}

Nicely done.

If you’d use my approach with the class you can have boxes of different sizes and colors.

Not a big step.

Also for Z-Value: instead of

random(100));

better

random(-500,100));

Chrisir

1 Like

Amazing…got it

1 Like