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 & HelpPrograms › reading arraylist where the obj is in in a class
Page Index Toggle Pages: 1
reading arraylist where the obj is in in a class (Read 1208 times)
reading arraylist where the obj is in in a class
Nov 10th, 2009, 3:48am
 
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);
 }
}

Re: reading arraylist where the obj is in in a class
Reply #1 - Nov 10th, 2009, 4:38am
 
if i move  ArrayList Dots = new ArrayList();  to the very beginning of the sketch. It is running. but then i guess. every dot is added to the same arrayList. But i want a new arrayList for every Swarm, so they only follow the boids from their own swarm.
Re: reading arraylist where the obj is in in a class
Reply #2 - Nov 10th, 2009, 4:47am
 
Indicating what line you get an error helps in making diagnostics.

Anyway, I see that in separation() method of Dot, you try to access Dots which is a field defined in Swarm, not in Dot.
If you need to access the swarm a dot belongs to, you can add a field of type Swarm in Dot and init in the constructor, for example (like Dot D = new Dot(this, new PVector(width/2, height/2,0), random(10,20), c);).
Re: reading arraylist where the obj is in in a class
Reply #3 - Nov 10th, 2009, 5:28am
 
Thank your for your help and yes, you are right. Thats where the problem is. I need to access the dots arraylist that is in the swarm that the dot belongs to.

i added "this" to the cronstructor. but what do i do with it when i passed it to the class? i have seen the usage of "this" serveral times. but i never understand how it should be used. Is there an example or tutorial page i am missing? if not, how would have hade to change the rest of the code to make it work?

Thanks again!
Re: reading arraylist where the obj is in in a class
Reply #4 - Nov 10th, 2009, 5:57am
 
Code:
class Dot{
 Swarm posse;
 // [...] (same fields)

 Dot(Swarm _posse, PVector _pos, float _diameter, color _c){ // constructor function has same name as class
   // constructor gets called when we make a new Dot
   posse = _posse;
   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 separation(){
   for (int i=0; i<posse.Dots.size(); i++){
Dot Neighbour = (Dot) posse.Dots.get(i); // get each Dot
 // [...] etc.
Re: reading arraylist where the obj is in in a class
Reply #5 - Nov 10th, 2009, 8:58am
 
Thank you so much! this is what i needed. This stuff is not documented somewhere. Or is it?
How do people know about and learn things like that?
Re: reading arraylist where the obj is in in a class
Reply #6 - Nov 10th, 2009, 9:02am
 
That's Java knowledge more than Processing knowledge.
And some experience...
Page Index Toggle Pages: 1