Removing objects once out of the canvas

Hello Forum!
I made an object and class sketch to randomly generate curved vertex object in P3D.
This is eventually goes over a movie. it works fine BUT after about a hundred or so times it gets called up (with the space bar) the CPU overloads and the movie starts to stutter. I thought by removing the objects once they’ve gone off screen than it will solve the problem. How do I do that? The movie is about 25 min - and in practice maybe 500 objects get called up.
I’ve just got the object part of the code below (not the movie). I saw in some other comments that garbage collection should do that. - but how do that work. Or can I just “.remove” objects?


Shapeshift[] cShape1 = new Shapeshift[1000];

int tot1 = 0;

void setup() {

  size(1920, 1080, P3D);
  noCursor();

  for (int i=0; i <1000; i++) {
    //(speedRotation,speedCanvas,size,colour, opacity, stroke, vector1,vector2, vector3)
    cShape1[i] = new Shapeshift (random(0.1, 1.5), random (0.1, 1.5), 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)));
  }
}
void draw() {
  background(0);
  for (int i=0; i <tot1; i++) { 
    if (tot1>1000) {
      tot1=tot1-1;
    } 
    cShape1[i].display();
  }
}

void keyPressed() {

  if (key == ' ') {
    tot1=tot1+1;
  }
} 

class Shapeshift {
  int posX=0, posY=0, posZ=10;
  float mX=0, mY=0, mZ=0;
  float diameter;
  float yspeed, xspeed;
  int col, opa, stro;
  float inc=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;


  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.01;
    if (fade>1) {
      fade=1;
    }
    inc+=0.1;
    if (key == '-') {
      fade-=0.05;
    }  
    pushMatrix();
    lights();

    fill(col, opa*fade);
    stroke(stro, opa*fade);
    strokeWeight(1);   
    translate(posX+(inc*mX*spi), posY+(inc*mY*spi), posZ+(inc*mZ*spi));
    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();
  }
}

Hello,

Please format your code:
https://discourse.processing.org/faq#format-your-code

I helps us help you.

:)

Thanks for the tip glv - the code did look a mess. I’ve edited my original post and pasted it bewteen <>.
Thanks in advance for any help!

1 Like

By that would you mean the movie video library?

If it’s hardware-related, like video for example, you’re also gonna need to invoke some method which releases those resources.

For the Movie class that method is dispose():
https://github.com/processing/processing-video/blob/r8-2.0/src/processing/video/Movie.java#L111-L126

Hi GoToLoop -
I’ll check this code out - that’s useful also. But still without the movie - when I run the code as pasted in the original post - I see with Activity Monitor that the CPU increases gradually as I call up a new object - and it doesn’t decrease when the objects are out of the canvas…would just like a method to be able to remove them from the array when they have gone out of sight…
Thanks!

In that case you’re better off w/ an ArrayList, which got the remove() method, instead of a plain array:

Aah! Eureka! I didn’t realise there was a difference between plain array and an ArrayList. This makes sense…I’ll check it out!
Thanks!!

You can read more about the ArrayList container on the link below:
Processing.org/reference/ArrayList.html

Alternatively you can keep using a vanilla array and call Processing’s shorten() to remove its tail index:
Processing.org/reference/shorten_.html

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