Loading...
Logo
Processing Forum
Hello, i'm making a program in which several cubes need to be on the screen at the same time in P3D so i created a class and an ArrayList to create the objects. The problem is is that the coordinates that i put in get messed up. Ill post my problem code bellow and describe the problem in more depth.

float xchange;
float ychange;
float zchange = 0;
boolean end = false;
ArrayList cubes = new ArrayList ();

void setup ()
{
  size (1000,500,P3D);
  cubes.add (new CubeClass (500,250,0,4)); // (Starting x,Starting y, Starting z, time to wait untill showing on screen)
  cubes.add (new CubeClass (0,250,0,120));
  cubes.add (new CubeClass (0,100,0,120));
}

void draw ()
{
background (123,32,123);
render ();
}

void render() {
  for (int i = 0; i < cubes.size(); i++) {
    CubeClass cube = (CubeClass) cubes.get(i);
    cube.display();
  }
}

class CubeClass 
{
  float x;
  float y;
  float z;
  float timerstart;
  float timer;
  
CubeClass (float xt,float yt,float zt,float timerstartt)
{
  timerstart = timerstartt;
  x = xt;
  y = yt;
  z = zt;
}

void display ()
{
  timer++;
  if (timerstart <= timer)
  {
  translate (x,y,z);
  box (30);
  }
}
}

Anyway so when i run this, all the cubes line up on the x axis. Which is a HUGE problem because i gave the first one a different x value than the other two!I'm running the latest version of processing. I'm out of answers so PLEEEEEEEEEAAAAAASSSSSSEEEE HELP.

Replies(2)

Hi it is because you are accumulating the translates.

Change the display method to the following:

Copy code
  1.         void display() {
  2.             timer++;
  3.             if (timerstart <= timer) {
  4.                 pushMatrix();
  5.                 translate(x, y, z);
  6.                 box(30);
  7.                 popMatrix();
  8.             }
  9.         }