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 10751 times)
Small Flocking system
Jan 1st, 2010, 11:06am
 
Just in case it might be of use to anybody.
http://draconiansolo.wordpress.com/2010/01/01/flocking-system/
it's messy but i think it can be useful.
Re: Small Flocking system
Reply #1 - Jan 1st, 2010, 11:15am
 
really cool. And thanks for sharing. Btw. you wrote "It'd be so nice if there were a flocking system library for processing."

thats exactly what i thought and asked for here: http://processing.org/discourse/yabb2/num_1255052351.html

so know as you have some knowledge Smiley wouldnt that be something for you hehe
Re: Small Flocking system
Reply #2 - Jan 2nd, 2010, 10:27am
 
Wink
Re: Small Flocking system
Reply #3 - Jan 2nd, 2010, 10:43am
 
so this means yes ? hehe ...
Re: Small Flocking system
Reply #4 - Jan 2nd, 2010, 7:47pm
 
Recently, I finally got my head around comparing angles and did a little prototype that produces flocking behavior through simple Braitneberg type interactions. Its not nearly as sophisticated as fdevant's but it might have potential for anyone looking to explore.

As of this post its my most recent sketch at R6753.com. The top left picture or direct link to the applet.
Re: Small Flocking system
Reply #5 - Jan 3rd, 2010, 12:11am
 
ooo I've completely forgoten about that model, it's pretty
Re: Small Flocking system
Reply #6 - Jan 4th, 2010, 4:45am
 
Hi! This reminds me about something I was trying to do, but couldn't get to work. In my case I had a flock chasing a single point (defined by a motion detection system in a video stream). My problem occurred when the angle came close to 0, where I would have either a value of close to zero, or a value close to 2PI. This made my flock behave erratic, or just turn away from the point, instead of chasing it(!). Was this a problem that happened to you as well, or did you not get that problem? I see you use ATAN2, which is what I was using as well ... Am I making sense? Smiley

Re: Small Flocking system
Reply #7 - Jan 4th, 2010, 6:41am
 
hey this is fantastic, just what I was hoping to use for something, thanks so much for sharing!
Re: Small Flocking system
Reply #8 - Jan 4th, 2010, 7:20am
 
I'm glad you people find this useful.
I promise i will make a cleaner version for you to use, and who knows, maybe eventually a processing library.
Meanwhile i'm having a little trouble [laziness] with space partitioning... gonna code quad/oct trees eventually.

Mmmh, the way i dealt with 0 distance was to make any steering vectors equal to 0 when distance was less than 1. that way, they just kept their inertia while they went back to reasonable distances.
Re: Small Flocking system
Reply #9 - Jan 4th, 2010, 8:10am
 
Negative angles and those close to zero need to be seen as similar or close to ones that are nearly a full circle like 357 if you're using degrees. The same thing happens for angles that are greater than a full turn like 370 in degrees.

To work with them you can add a full circle if the angle is less than (or equal to?) zero and subtract a full circle if the angle is greater than (or equal to?) a full turn (360). Angles like -5 become 355 and 370 would become 10. See the remap() function at the end of my code. I'm using radians there.

I had run into similar problems to those knutEinar was having and it took me a while to figure the whole thing out.
Re: Small Flocking system
Reply #10 - Jan 6th, 2010, 3:17am
 
Rick wrote on Jan 4th, 2010, 8:10am:
Negative angles and those close to zero need to be seen as similar or close to ones that are nearly a full circle like 357 if you're using degrees. The same thing happens for angles that are greater than a full turn like 370 in degrees.

To work with them you can add a full circle if the angle is less than (or equal to) zero and subtract a full circle if the angle is greater than (or equal to) a full turn (360). Angles like -5 become 355 and 370 would become 10. See the remap() function at the end of my code. I'm using radians there.

I had run into similar problems to those knutEinar was having and it took me a while to figure the whole thing out.


But using your method, is exactly where and when I get my problem, because then the two angles, say 10 and 355, which is only 15 degrees apart, will look like they are 245 degrees apart. So how do I fix this A bunch of if/elses
Re: Small Flocking system
Reply #11 - Jan 6th, 2010, 8:22am
 
I don't fully understand how it works myself, but you also have to subtract the angles you're comparing. That part makes some sense because comparison often means subtraction. If you subtract 355 from 10 you get -345 ( 10 - 355 = -345 ). The result, -345, is less than 0 so add a full turn ( -345 + 360 = 15 ) and you get the difference that you were expecting.
Re: Small Flocking system
Reply #12 - Jan 6th, 2010, 12:40pm
 
If you use radians instead of degrees and use atan2() to solve them, you'll probably not get those problems anymore.
Re: Small Flocking system
Reply #13 - Jan 10th, 2010, 10:52am
 
I know we're a little off topic, but how would you use atan2() (correctly and without remapping) in the following example to avoid the jump at 9 o' clock?

Quote:
float[][]rand;

void setup() {
  size(400,400);
  rand = new float[5000][2];
  for(int a=0; a<rand.length; a++) {
    rand[a][0] = random(width);
    rand[a][1] = random(height);
  }
}
void draw() {
  background(0);
  float mAngle = atan2(mouseY-height/2, mouseX-width/2);
  float w = 1;
  for(int a=0; a<rand.length; a++) {
    float angle = atan2(rand[a][1]-height/2, rand[a][0]-width/2);
    angle -= mAngle;
    if(angle < -w || angle > w) {
      stroke(0);
    } else {      
      stroke(255,0,0);
    }
    point(rand[a][0], rand[a][1]);
  }
}

Re: Small Flocking system
Reply #14 - Jan 14th, 2010, 6:02am
 
fdevant wrote on Jan 6th, 2010, 12:40pm:
If you use radians instead of degrees and use atan2() to solve them, you'll probably not get those problems anymore.


I did use radians and atan2, but still could not figure it out. I have it working now, but I had to use a bunch of if/elses as I suspected. Not sure if I can do it any other way.

Here is the code I used. I know it can be simplifid, I just wanted to type everything out, to make it clearer to myself Smiley

void checkAngles(){
 v2ang += TWO_PI;
 v2ang %= TWO_PI;
 //
 fill(0, 102, 153);
 textFont(font,20);
 text("v2ang = "+v2ang, 15, 30);
   text("v1ang = "+v1ang, 15, 50);

 if (v2ang > v1ang){
   // ok, this means that v1ang is closer to 0 degrees, than v2ang, but it does not say anything about which quadrant they are ...
   // then ...
   if( v2ang - v1ang > PI){
     // if the difference is more than PI, i.e. a half circle, then the angle is obtuse,
     // and the fastest way to get closer to the other angle is to increase it ...
     v2ang += TWO_PI/200;
   }
   else {
     v2ang -= TWO_PI/200;
   }
 }
 else if (v2ang < v1ang){
   // this means that v1ang is closer to TWO_PI than v2ang is ...
   // subtract the smaller from the bigger ...
   if( v1ang - v2ang > PI){
     // again: if the difference is more than PI, i.e. a half circle, then the angle is obtuse,
     // and the fastest way to get closer to the other angle is to increase it ...
     v2ang -= TWO_PI/200;
   }
   else {
     v2ang += TWO_PI/200;
   }
 }
}
Pages: 1 2