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.
Page Index Toggle Pages: 1
Swarm of flies (Read 816 times)
Swarm of flies
Feb 14th, 2009, 9:19pm
 
hi everybody!
im a really nupe in processing, but i found a great project by Elie Zananiri - Flies. What I want do do is paint some flies (maybe 10), that swarm around from the beginning on (no addFly, no mousePressed etc) just simple have some flies that swarm around the mouse.

what do i have to remove/add to fullfill this?

thanks a lot!
carina



http://www.openprocessing.org/visuals/?visualID=493

 
int SWARM_OFFSET = 50;
int MAX_FLIES = 500;
int numFlies = 0;
Fly[] flies = new Fly[MAX_FLIES];

void setup() {
 //size(screen.width, screen.height, OPENGL);
 size(800, 600);
 smooth();
 strokeWeight(2);
 noCursor();
 //for (int i=0; i < MAX_FLIES; i++)
   addFly();
}

void draw() {
 fill(0, 150);
 rect(0, 0, width, height);
 
 for (int i=0; i < numFlies; i++) {
   if (!flies[i].isAtTarget()) {
     flies[i].move();
   }
     
   flies[i].swarm();
   flies[i].paint();
 }  
}

void mousePressed() {
 addFly();  
}

void mouseMoved() {
 for (int i=0; i < numFlies; i++) {
   flies[i].newTarget(mouseX, mouseY);
 }  
}

void addFly() {
 if (numFlies < MAX_FLIES) {
   flies[numFlies] = new Fly(random(width), random(height));
   numFlies++;
 }  
}

class Fly {
 // swarm animation
 float homeX, homeY;
 float rangeX, rangeY;
 float angle;
 float step;
 
 // current position
 float currX, currY;
 
 // move animation
 float targetX, targetY;
 boolean atTarget;
 
 // paing variables
 int bodySize;
 int wingSpan;
 float flap;
 float flapStep;
 
 Fly(float x, float y) {
   // init the move variables
   homeX = random(x-SWARM_OFFSET, x+SWARM_OFFSET);
   homeY = random(y-SWARM_OFFSET, y+SWARM_OFFSET);
   newTarget(homeX, homeY);
   
   // init the swarm variables
   rangeX = random(20, 50);
   rangeY = random(20, 50);
   angle = 0;
   step = random(-0.1, 0.1);
   
   // init the paint variables
   bodySize = (int)random(5, 10);
   wingSpan = (int)random(5, 10);
   flap = 0;
   flapStep = (bodySize*1.0/wingSpan)/5;
 }
 
 void paint() {
   pushMatrix();
     translate(currX, currY);
     rotate(angle);
     
     // draw the wings
     stroke(#02B409);
     line(0, 0, -wingSpan, -sin(flap)*wingSpan);
     line(0, 0, wingSpan, -sin(flap)*wingSpan);
     flap += flapStep;
     
     // draw the body
     fill(#B47C02);
     noStroke();
     ellipse(0, 0, bodySize, bodySize);
   popMatrix();
 }  
 
 void move() {
   // calculate the distance to the target
   float dX = targetX-homeX;
   float dY = targetY-homeY;
   
   // calculate the current step towards the target
   float stepX = dX/10;
   float stepY = dY/10;
   
   homeX += stepX;
   homeY += stepY;
   
   // if we're close enough to the target...
   if (abs(dX) < 1 && abs(dY) < 1) {
     // ...assume we've reached it
     atTarget = true;
   }
 }
 
 void swarm() {
   // hover around the home position
   currX = int(rangeX*sin(angle)+homeX);
   currY = int(rangeY*cos(angle)+homeY);
   angle += step;
 }
 
 void newTarget(float newX, float newY) {
   // set a new target position for the home
   targetX = random(newX-50, newX+50);
   targetY = random(newY-50, newY+50);
   atTarget = false;
 }
 
 boolean isAtTarget() {
   return atTarget;
 }
}


Re: Swarm of flies
Reply #1 - Feb 15th, 2009, 6:37am
 
i didnt check the code and what to exclude but maybe this simple wanderer Class is what you need.
You can change the behavior with offset and radius and maybe add some collision if you want to...
i guess you dont need flocking as there is no such thing as flocking and flies (fly swarms, do they exist?!?)

Wanderer[] wanderer = new Wanderer[21];

void setup()
{
 size(500, 500);
 background(50);
 fill(200);
 noStroke();
 
 for (int i=0; i<wanderer.length; i++)
 {
   wanderer[i] = new Wanderer(random(width), random(height));
 }
}

void draw()
{
 background(50);
 
 for (int i=0; i<wanderer.length; i++)
 {
   wanderer[i].stayInsideCanvas();
   wanderer[i].move();
   ellipse(wanderer[i].getX(), wanderer[i].getY(), 2, 2);
 }
}
class Wanderer
{
 float x;
 float y;
 float wander_theta;
 float wander_radius;
 

 float max_wander_offset = 0.3;
 float max_wander_radius = 3.5;
 
 Wanderer(float _x, float _y)
 {
   x = _x;
   y = _y;
   
   wander_theta = random(TWO_PI);
   wander_radius = random(max_wander_radius);
 }
 
 void stayInsideCanvas()
 {
   x %= width;
   y %= height;
 }
 
 void move()
 {
   float wander_offset = random(-max_wander_offset, max_wander_offset);
   wander_theta += wander_offset;
   
   x += cos(wander_theta);
   y += sin(wander_theta);
 }
 
 float getX()
 {
   return x;
 }
 
 float getY()
 {
   return y;
 }
}
Re: Swarm of flies
Reply #2 - Feb 15th, 2009, 10:18am
 
Hi!
thanks a lot for your reply!
yes, the wanderer class is nearly what I want, but all the "flies" shall follow the mouse position and swarm around it (don't know if they actually do, but if not, they will from now on Wink )

so how can this be done?


Re: Swarm of flies
Reply #3 - Feb 15th, 2009, 10:52am
 
Im sorry, I just didnt see "following the mouse" ...
got a good flies example for that purpose. Found it a while ago. You have to install traer physics to use it, but its easy to handle...

http://www.vohm.com/processing/flies/

Page Index Toggle Pages: 1