We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Asexual Reproduction
Page Index Toggle Pages: 1
Asexual Reproduction (Read 357 times)
Asexual Reproduction
Feb 11th, 2009, 5:57pm
 
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);
}
}
Re: Asexual Reproduction
Reply #1 - Feb 11th, 2009, 6:11pm
 
Quote:
float[] y_ = append(y,x[nSquirm-1]);

looks suspicious...
Re: Asexual Reproduction
Reply #2 - Feb 11th, 2009, 6:19pm
 
Well gee, that was obvious, It almost bit me!  Guess I need another cup of coffee.
Page Index Toggle Pages: 1