Removing objects once out of the canvas

Thanks GoToLoop!

The answer was indeed to use ArrayList instead of normal Array.
Below is the corrected code. I made a ‘death’ function that fades them when they are getting to the limits of the canvas. Checked the CPU on ActivityMonitor and indeed it decreases as the shapes disappear.


ArrayList<Shapeshift> shapes;


void setup() {
  
  size(1920, 1080, P3D);
  noCursor();
  shapes = new ArrayList<Shapeshift>();

  
}

void draw() {
  background(0);

  for (int i = shapes.size()-1; i >= 0; i--) { 

    Shapeshift kite = shapes.get(i);
    kite.display();
    if (kite.death()) {
      shapes.remove(i);
    }
  }
}

void keyPressed() {

  shapes.add(new Shapeshift(random(0.1, 1.5), random (0.1, 1), random(0.5, 3), 255, int(random (155, 255)), int(random (150, 255)), 
    int(random(0, 2)), int(random(0, 2)), int(random(0, 2))));
} 

class Shapeshift {
  int posX=0, posY=0, posZ=10;
  float mX=0, mY=0, mZ=0;
  float diameter;
  float yspeed, xspeed;
  int col, stro;
  float inc=0;
  float opa=0;
  float x1, x2, x3, x4, x5, x6, x7, x8, x9, x10;
  float y1, y2, y3, y4, y5, y6, y7, y8, y9, y10;
  float z1, z2, z3, z4, z5, z6, z7, z8, z9, z10;
  float rX, rY, rZ;
  float sp=0;
  float spe, spi, sz;
  int v1, v2, v3;
  float fade;
  float xx,yy,zz;


  Shapeshift(float speedRotation, float speedCanvas, float size, int colour, int opacity, int stroke, int vector1, int vector2, int vector3) {
    ;

    inc=0;
    posX = int(random(300, 1620));
    posY = int(random(300, 780));
    posZ = int(random(100, 300));
    spe=speedRotation;
    spi=speedCanvas;
    sz=size;
    col=colour;
    stro=stroke;
    opa=opacity;
    v1=vector1;
    v2=vector2;
    v3=vector3;


    mX = random (-5, 5);
    mY = random (-5, 5);
    mZ = random (-5, 5);

    rX = random(-0.1, 0.1);
    rY = random(-0.1, 0.1);
    rZ = random(-0.1, 0.1);

    x1 = random(-50, 50)*sz;
    x2 = random(-50, 50)*sz;
    x3 = random(-50, 50)*sz;
    x4 = random(-50, 50)*sz;
    x5 = random(-50, 50)*sz;
    x6 = random(-50, 50)*sz;
    x7 = random(-50, 50)*sz;
    x8 = random(-50, 50)*sz;
    x9 = random(-50, 50)*sz;
    x10 = random(-50, 50)*sz;

    y1 = random(-50, 50)*sz;
    y2 = random(-50, 50)*sz;
    y3 = random(-50, 50)*sz;
    y4 = random(-50, 50)*sz;
    y5 = random(-50, 50)*sz;
    y6 = random(-50, 50)*sz;
    y7 = random(-50, 50)*sz;
    y8 = random(-50, 50)*sz;
    y9 = random(-50, 50)*sz;
    y10 = random(-50, 50)*sz;

    z1 = random(-50, 50)*sz;
    z2 = random(-50, 50)*sz;
    z3 = random(-50, 50)*sz;
    z4 = random(-50, 50)*sz;
    z5 = random(-50, 50)*sz;
    z6 = random(-50, 50)*sz;
    z7 = random(-50, 50)*sz;
    z8 = random(-50, 50)*sz;
    z9 = random(-50, 50)*sz;
    z10 = random(-50, 50)*sz;
  }

  void display() {

    fade+=0.05;
    if (fade>1) {
      fade=1;
    }
    inc+=0.1;
   
   xx= posX+(inc*mX*spi);
   yy= posY+(inc*mY*spi);
   zz= posZ+(inc*mZ*spi);
    
    pushMatrix();
    lights();

    fill(col, opa*fade);
    stroke(stro, opa*fade);
    strokeWeight(1);   
    translate(xx, yy, zz);
    rotateX(inc*rX*spe);
    rotateY(inc*rY*spe);
    rotateZ(inc*rZ*spe);
    beginShape();

    //curveVertex(x1, y1, z1);
    curveVertex(x2, y2, z2);
    curveVertex(x3, y3, z3);
    curveVertex(x4, y4, z4);
    curveVertex(x5*v1, y5*v1, z5*v1);
    curveVertex(x6*v1, y6*v1, z6*v1);
    curveVertex(x7*v2, y7*v2, z7*v2);
    curveVertex(x8*v2, y8*v2, z8*v2);
    curveVertex(x9*v3, y9*v3, z9*v3);
    curveVertex(x10*v3, y10*v3, z10*v3);

    endShape(CLOSE);
    popMatrix();
  }


  boolean death() {
    
     if ((xx < 100) || (xx > width-100) || (yy > height-100) || (yy < 100) || (zz > 350) || (zz < 50)){
    opa=opa-1*(random(0.1,0.5));
     }
    if (opa < 0) {
      return true;
    } else {
      return false;
    }
  }
}

1 Like