zeebra
YaBB Newbies
Offline
Posts: 6
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; } }