I'm trying to make an array of perlin-guided worms, whose number increases each time you hit the 'b' key. I've accomplished this, but I'm wondering why the origin point of the new worm isn't that of the last one in the series... this is what I was going for.
Code:
squirm mySq;
void setup(){
smooth();
size(800,800);
background(0);
stroke(255);
mySq = new squirm();
}
void draw(){
mySq.update();
mySq.fade(22);
}
void keyReleased(){
if(key == 'b'){
mySq.more();}
}
class squirm{
float[]x;
float[]y;
float[]xp;
float[]yp;
float[]n;
float id[];
float delta[];
int nSquirm = 1;
squirm(){
x = new float[nSquirm];
y = new float[nSquirm];
xp = new float[nSquirm];
yp = new float[nSquirm];
n = new float[nSquirm];
id = new float[nSquirm];
delta = new float[nSquirm];
for (int i = 0; i < x.length; i++){
id[i]=random(10,100);
delta[i]=random(100,1000)/100000;
x[i]=0;
y[i]=random(height);
n[i]=0;
}
}
void update(){
for (int i = 0; i < x.length; i++){
xp[i]=x[i];
yp[i]=y[i];
id[i]+=delta[i];
n[i]=noise(id[i])*height;
x[i]+=cos(radians(n[i]))*3;
y[i]+=sin(radians(n[i]))*3;
if(x[i]<-10) x[i]=xp[i]=width+10;//offscreen rewind
if(x[i]>width+10)x[i]=xp[i]=-10;
if(y[i]<-10) y[i]=yp[i]=height+10;
if(y[i]>height+10)y[i]=yp[i]=-10;
line(xp[i],yp[i],x[i],y[i]);
}
}
void more(){
float[] x_ = append(x,x[nSquirm-1]);
float[] y_ = append(y,x[nSquirm-1]);
float[] xp_ = append(xp,xp[nSquirm-1]);
float[] yp_ = append(yp,yp[nSquirm-1]);
float[] n_ = append(n,n[nSquirm-1]);
float[] delta_ = append(delta,random(100,1000)/100000);
float[] id_ = append(id,id[nSquirm-1]);
x = x_;
y = y_;
xp = xp_;
yp = yp_;
n = n_;
delta = delta_;
id = id_;
println(x.length);
}
void fade(float alpha){
fill(0,0,0,alpha);
rect(0,0,width,height);
}
}