Hello Guys, i am having a question again. Maybe its a syntax question.
I was looking arround at openprocessing and found this nice sketch of flocking behaviour. http://www.openprocessing.org/visuals/?visualID=5101 cause i wanted to learn that stuff i took a closer look.
My second thought was that it would be nice, if i could create different swarms of flocking circles. so i created a different class where i put the flocking Dots in. Now i thought if i create different instances of that class i get different swarms. But i am keep getting the problem "cant find anything named Dots" when trying to read the arrayList the objects are in themself. But it is working in his example this way, so probably i just have to write it in a different way.
Can anybody help me to understand where the problem is?
that wouldbe nice.
Here is what i tried to do :
Code:Swarm[] swarm = new Swarm[2];
void setup(){
size(500, 500);
background(50);
fill(200);
noStroke();
for (int i=0; i<swarm.length; i++){
swarm[i] = new Swarm();
}
}
void draw(){
background(50);
for (int i=0; i<swarm.length; i++) {
swarm[i].run();
}
}
class Swarm{
float x;
float y;
color c;
ArrayList Dots = new ArrayList(); // make an empty arraylist
int numDots = 10; // number of dots created per click
Swarm(){
x = mouseX;
y = mouseY;
c = color(random(100,255),random(100,255),random(100,255)); // random initial colour
for (int i=0; i<10; i++){ // make a bunch of new Dots
Dot D = new Dot(new PVector(width/2,height/2,0),random(10,20),c); // make new Dot at mouse pos, with diameter between 10 and 20
Dots.add(D); // add D to the arraylist Dots
}
}
void run(){
for (int i=0; i<Dots.size(); i++){ // step through the arraylist - size() gets the size of the arraylist
Dot tempD = (Dot) Dots.get(i); // get an object out of the list, put it in tempD
tempD.run(); // call the object's run function
}
}
}
class Dot{ // define a new class
float diameter;
color c;
PVector pos; // position
PVector vel; // velocity
PVector acc; // acceleration
// cohesion variables
float cohesionFactor = .05;
float cohesionRadius = 40;
// alignment variables
float alignFactor = 0.1;
float alignRadius = 40;
// separation variables
float separationFactor = 1.5;
float separationRadius = 25;
// speed limit
float maxVel = 4;
// friction
float frictionFactor = 0.95;
Dot(PVector _pos, float _diameter, color _c){ // constructor function has same name as class
// constructor gets called when we make a new Dot
pos = _pos;
c = _c;
diameter = _diameter;
vel = new PVector(random(-5,5),random(-5,5),0); // random initial velocity
acc = new PVector(0,0,0); // set acceleration to 0
}
void run(){ // the main function - calls all the other behaviours and updates position and acceleration
bounce();
cohesion(); // cohesion factor, radius
separation(); // separation factor, radius
alignment();
friction();
speedLimit();
render();
vel.add(acc); // update the velocity vector - add the current acceleration vector
pos.add(vel); // update the position - add the current velocity vector
acc.mult(0); // reset acceleration to 0
}
void render(){ // draw the dot
fill(c);
ellipse(pos.x,pos.y,diameter,diameter);
}
void bounce(){
if (pos.x > width || pos.x < 0){ // if x pos is greater than right hand edge OR less than left hand edge
vel.set(vel.x*-1,vel.y,0); // bounce off the edge
}
if (pos.y > height || pos.y < 0) { // if y pos is greater than bottom edge OR less than top edge
vel.set(vel.x,vel.y*-1,0);
}
}
void separation(){
for (int i=0; i<Dots.size(); i++){
Dot Neighbour = (Dot) Dots.get(i); // get each Dot
float distance = PVector.dist(pos,Neighbour.pos);
if (distance < separationRadius){
PVector toNeighbour = PVector.sub(pos,Neighbour.pos); // make a vector from the neighbour
toNeighbour.normalize(); // scale it to 1
toNeighbour.mult(((separationRadius-distance)/separationRadius)*separationFactor);
acc.add(toNeighbour);
}
}
}
void cohesion(){ // each agent steers towards the average position of its neighbours
PVector avePos = new PVector(); // PVector to store average position
int neighbourcount = 0; // int to count the number of neighbours
for (int i=0; i<Dots.size(); i++){
Dot Neighbour = (Dot) Dots.get(i);
float distance = PVector.dist(pos,Neighbour.pos);
if (distance < cohesionRadius){
avePos.add(Neighbour.pos);
neighbourcount++;
}
}
avePos.div(neighbourcount); // divide by num neighbours to get the average - the center position of our near neighbours
PVector toCenter = PVector.sub(avePos,pos); // make a vector to that point
toCenter.normalize(); // scale it to 1
toCenter.mult(cohesionFactor); // multiply by cohesion factor
acc.add(toCenter); // add the cohesion force to the acceleration
}
void alignment(){
PVector aveVel = new PVector();
for (int i=0; i<Dots.size(); i++){
Dot Neighbour = (Dot) Dots.get(i);
float distance = PVector.dist(pos,Neighbour.pos);
if (distance < alignRadius){
aveVel.add(Neighbour.vel);
}
}
aveVel.normalize();
aveVel.mult(alignFactor);
acc.add(aveVel);
}
void speedLimit(){
if (vel.mag() > maxVel){
vel.normalize(vel).mult(maxVel);
}
}
void friction(){
vel.mult(frictionFactor);
}
}