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.
IndexDiscussionExhibition › Small Flocking system
Pages: 1 2 
Small Flocking system (Read 10752 times)
Re: Small Flocking system
Reply #15 - Jan 15th, 2010, 11:31am
 
This was bugging me so I tried to work on it myself and then looked around the internet for a better solution. I happened on an example in some open source code and wrote a Processing version.

Quote:
float angleOne, angleTwo;
boolean increase;
void setup() {
  size(400,400);
  noFill();
  float cx = width/2;
  float cy = height/2;
  angleOne = atan2(random(height)-cy, random(width)-cx);
  angleTwo = atan2(random(height)-cy, random(width)-cx);
  if(sin(angleTwo-angleOne)>0) {
    increase = true;
  }
  if(sin(angleTwo-angleOne)<0) {
    increase = false;
  }
}
void draw() {
  background(196);
  stroke(255,0,0);
  pushMatrix();
  translate(width/2,height/2);
  rotate(angleOne);
  line(0,0,50,0);
  popMatrix();
  stroke(0,255,0);
  pushMatrix();
  translate(width/2,height/2);
  rotate(angleTwo);
  line(0,0,50,0);
  popMatrix();
  
//  if(abs(angleTwo-angleOne)>.2) {
//    if(increase) {
//      angleOne += .01;
//    } else {
//      angleOne -= .01;
//    }
//  }
  stroke(0);
  if(increase) {
    arc(width/2,height/2,25,25,angleOne,angleTwo);
  } else {
    arc(width/2,height/2,25,25,angleTwo,angleOne);
  }
}
void mouseClicked() {
  setup();
}



The critical part is in setup. The sin(angleTwo-angleOne) determines direction. The arc is drawn on the shortest angle. You could also add to angleOne (red) if increase is true (subtract if its false of course) to turn the shortest way towards angleTwo (green). My commented code wouldn't work to stop the turn when they were close for some reason though. Click for a new set of angles.

I guess its not a whole lot less complex than knutEinar's last example, but I was glad I found it anyway.
Re: Small Flocking system
Reply #16 - Jan 16th, 2010, 11:56pm
 
The reason why it keeps moving then, is because you only check which angle is greater in setup. You need to check it for each frame. Smiley
Re: Small Flocking system
Reply #17 - Apr 25th, 2010, 11:47am
 
Re: Small Flocking system
Reply #18 - May 22nd, 2010, 12:20pm
 
Really cool.

I think you should make motion a function of time not of frames. That way it would look the same no matter if it is running at 60 or 20 fps. People begin to notice a low frame rate when it drops below 20.

A movie with lots of particles would be cool.
Re: Small Flocking system
Reply #19 - May 24th, 2010, 4:56am
 
this is very good ....thanks for sharing....really very cool
thanks
---------------
Re: Small Flocking system
Reply #20 - May 24th, 2010, 5:34am
 
really nice. as mentioned on your website, the idea to turn it into a library is a good one. I once mentioned in another post that this would be a really useful library as a lot of people ask for flockling code and examples.

Re: Small Flocking system
Reply #21 - May 26th, 2010, 4:23pm
 
Yes! i really wish i had the time to do it myself.
At least i hope to eventually clean the code enough to make it workable Tongue.
I will try the separation between render and logic soon. Also hope to do some optimization on it.
Re: Small Flocking system
Reply #22 - May 26th, 2010, 4:36pm
 
hey, i did something like this

 while((millis()-lastMillisCheck)>=10){

     myEngine.update();
   lastMillisCheck+=10;
 }

 myEngine.display();  

I guess the logic is quite harder for the processor than the drawing because once i have more than 300 particles it collapses Tongue
any ideas?
Pages: 1 2