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();
}
}