Hi I am wondering why this script stops ( I know it is meant to) but is it time/number of boids near eahc other...etcetc that makes it stop.
If someone know and could describe how they know would be great
Thanks!
import peasy.test.*;
import peasy.org.apache.commons.math.*;
import peasy.*;
import peasy.org.apache.commons.math.geometry.*;
import processing.opengl.*;
PeasyCam cam;
int worldsize;
int TAIL_LENGTH;
H_Flock andrew;
void setup()
{
size(800, 600, OPENGL);
cam = new PeasyCam(this, 0,0,0, 500);
cam.setMinimumDistance(10);
cam.setMaximumDistance(1550);
worldsize = 800;
TAIL_LENGTH = 20;
andrew = new H_Flock(#4BFFB5);
andrew.setParameters(2, 1, 1, 3, 0.05);
for(int i = 0; i < 200; i++)
{
H_Boid b;
b = new H_Boid(new PVector(random(-worldsize/2, worldsize/2), random(-worldsize/2, worldsize/2), random(-worldsize/2, worldsize/2)), andrew);
andrew.addBoid(b);
}
H_Boid death = new H_Boid(new PVector(0,0,0), andrew); death.dead = true;
andrew.addBoid(death);
}
void draw()
{
background(50);
lights();
displayWorldSize();
andrew.run();
}
void displayWorldSize()
{
pushMatrix();
translate(0,0,0);
noFill(); stroke(#2FA3C1);
box(worldsize);
popMatrix();
}
class H_Boid
{
PVector loc;
PVector vel;
PVector acc;
H_Flock parent;
H_Trace tail;
ArrayList deadguys;
boolean dead;
H_Boid(PVector _loc, H_Flock _parent)
{
loc = _loc;
parent = _parent;
vel = new PVector(random(-1, 1), random(-1, 1));
acc = new PVector(0, 0);
tail = new H_Trace();
dead = false;
deadguys = new ArrayList();
}
void run()
{
if(!dead) flock();
if(!dead) update();
if(!dead) borders();
render();
renderDeadGuys();
connectivity();
if(!dead) tail.addPoint(new PVector(loc.x, loc.y, loc.z));
if(!dead) tail.display();
}
void flock()
{
PVector sep = separate();
sep.mult(parent.FSep);
acc.add(sep);
PVector coh = cohesion();
coh.mult(parent.FCoh);
acc.add(coh);
PVector ali = align();
ali.mult(parent.FAli);
acc.add(ali);
cluster();
}
void update()
{
vel.add(acc);
vel.limit(parent.maxspeed);
loc.add(vel);
acc = new PVector(0, 0);
}
void borders()
{
//just like in snake! only in 3d as well...
//teleporting walls
if (loc.x > worldsize/2) loc.x = -worldsize/2;
if (loc.y > worldsize/2) loc.y = -worldsize/2;
if (loc.z > worldsize/2) loc.z = -worldsize/2;
if (loc.x < -worldsize/2) loc.x = worldsize/2;
if (loc.y < -worldsize/2) loc.y = worldsize/2;
if (loc.z < -worldsize/2) loc.z = worldsize/2;
}
void render()
{
pushMatrix();
translate(loc.x, loc.y, loc.z);
noStroke();
fill(parent.c);
ellipse(0, 0, 10, 10);
//sphereDetail(8);
//sphere(10);
popMatrix();
}
void renderDeadGuys()
{
for(int i = 0; i < deadguys.size(); i++)
{
H_Boid temp = (H_Boid) deadguys.get(i);
line(loc.x, loc.y, loc.z, temp.loc.x, temp.loc.y, temp.loc.z);
}// end for loop
}// end of the render dead guys function
PVector separate()
{
float desiredseparation = 50;
PVector steer = new PVector(0, 0, 0);
int count = 0;
for (int i = 0; i < parent.boids.size(); i++)
{
H_Boid temp = (H_Boid) parent.boids.get(i);
float distance = PVector.dist(loc, temp.loc);
if ( (distance > 0) && (distance < desiredseparation) )
{
PVector diff = PVector.sub(loc, temp.loc);
diff.normalize();
diff.div(distance);
steer.add(diff);
count++;
} // end distance check
} // end for loop
if (count > 0) steer.div(count);
if (steer.mag() > 0)
{
steer.normalize();
steer.mult(parent.maxspeed);
steer.sub(vel);
steer.limit(parent.maximumturnspeed);
}
return steer;
}// end of the function separate()
PVector cohesion()
{
float vision = 80;
PVector sum = new PVector(0, 0, 0);
int count = 0;
for (int i = 0; i < parent.boids.size(); i++)
{
H_Boid temp = (H_Boid) parent.boids.get(i);
float distance = PVector.dist(loc, temp.loc);
if ( (distance > 0) && (distance < vision) )
{
sum.add(temp.loc);
count++;
}
}// end for
if (count > 0)
{
sum.div(count);
return steer(sum, false);
}
return sum;
}// end of the cohesion function
PVector align()
{
float vision = 80;
PVector steer = new PVector(0, 0, 0);
int count = 0;
for (int i = 0; i < parent.boids.size(); i++)
{
H_Boid temp = (H_Boid) parent.boids.get(i);
float distance = PVector.dist(loc, temp.loc);
if ( (distance > 0) && (distance < vision) )
{
steer.add(temp.vel);
count++;
}// end if
}// end for loop
if (count > 0) steer.div(count);
if (steer.mag() > 0)
{
steer.normalize();
steer.mult(parent.maxspeed);
steer.sub(vel);
steer.limit(parent.maximumturnspeed);
}
return steer;
}// end of the alignment function
PVector steer(PVector target, boolean slowdown)
{
PVector steer;
PVector desired;
desired = PVector.sub(target, loc);
float d = desired.mag();
if (d > 0)
{
desired.normalize();
if ((slowdown) && (d < 150))
{
desired.mult(parent.maxspeed * (d / 150));
}
else
{
desired.mult(parent.maxspeed);
} // end of the slowdown if
steer = PVector.sub(desired, vel);
steer.limit(parent.maximumturnspeed);
}// end of the distance check if
else
{
// if distance is zero
steer = new PVector(0, 0, 0);
}
return steer;
} // end of the steering function
void connectivity()
{
for(int i = 0; i < parent.boids.size(); i++)
{
H_Boid andrew = (H_Boid) parent.boids.get(i);
float distance = PVector.dist(loc, andrew.loc);
if( (distance > 0) && (distance < 150) )
{
stroke(parent.c, map(distance, 0, 150, 255, 0));
line(loc.x, loc.y, loc.z, andrew.loc.x, andrew.loc.y, andrew.loc.z);
}
} // end for
}// end of the connectivity function
void cluster()
{
for(int i = 0; i < parent.boids.size(); i++)
{
H_Boid george = (H_Boid) parent.boids.get(i);
if(george.dead)
{
float distance = PVector.dist(loc, george.loc);
if( (distance > 0) && (distance < parent.clusterdistance) )
{
dead = true;
deadguys.add(george);
}
}
}
}
}
class H_Flock
{
float FSep, FCoh, FAli;
float maxspeed;
float maximumturnspeed;
color c;
float clusterdistance;
ArrayList boids;
H_Flock(color _c)
{
boids = new ArrayList();
c = _c;
clusterdistance = 100;
}
void run()
{
for(int i = 0; i < boids.size(); i++)
{
H_Boid temp = (H_Boid) boids.get(i);
temp.run();
}
}
void addBoid(H_Boid george)
{
boids.add(george);
}
void setParameters(float _FSep, float _FCoh, float _FAli, float _maxspeed, float _maxturnspeed)
{
FSep = _FSep; FCoh = _FCoh; FAli = _FAli;
maxspeed = _maxspeed; maximumturnspeed = _maxturnspeed;
}
}
class H_Flock
{
float FSep, FCoh, FAli;
float maxspeed;
float maximumturnspeed;
color c;
float clusterdistance;
ArrayList boids;
H_Flock(color _c)
{
boids = new ArrayList();
c = _c;
clusterdistance = 100;
}
void run()
{
for(int i = 0; i < boids.size(); i++)
{
H_Boid temp = (H_Boid) boids.get(i);
temp.run();
}
}
void addBoid(H_Boid george)
{
boids.add(george);
}
void setParameters(float _FSep, float _FCoh, float _FAli, float _maxspeed, float _maxturnspeed)
{
FSep = _FSep; FCoh = _FCoh; FAli = _FAli;
maxspeed = _maxspeed; maximumturnspeed = _maxturnspeed;
}
}
class H_Trace
{
ArrayList l; // our collection of points
H_Trace()
{
l = new ArrayList();
} // end of the constructor function
void addPoint(PVector p)
{
l.add(p);
if(l.size() > 50)
{
l.remove(0);
}
} // end of the addpoint function
void display()
{
for(int i = 1; i < l.size(); i++)
{
PVector a, b;
a = (PVector) l.get(i);
b = (PVector) l.get(i-1);
if (!(PVector.dist(a, b) > worldsize-10)) {
stroke(255, map(i, 0, l.size(), 0, 255));
line(a.x, a.y, a.z, b.x, b.y, b.z);
}
} // end of the for loop
} // end of the display function
} // end of the class.
1