//created=02 Jan 07
//description=Perturbed gravitational motion to simulate swarming bees.
//title=Bees
//publish=true
 
import java.awt.geom.*;
 
Vector swarm = new Vector();
 
PImage guy;
PImage bee;
float px = 300;
float py = 200;
float lpx;
 
float time = 0;
 
int direction = 0;
 
boolean paused = false;
 
float harmonic = 0;
float harmonicStep = 0;
void setup(){
 
size(550,300,P3D);
for (int i =0; i < 10; i++){
swarm.add(new Bee());
}
guy = loadImage("screamPon.png");
bee = loadImage("sheepz.png");
textureMode(NORMALIZED);
hint(ENABLE_DEPTH_SORT);
}
 
void draw(){
time += .01;
background(200,200,255);
 
fill(255);
 
 
Bee buzz;
for (int i =0; i < swarm.size(); i++){
buzz = (Bee)swarm.elementAt(i);
buzz.draw();
}
noStroke();
pushMatrix();
translate(px,230 + harmonic);
if (lpx > px) direction = 1;
if (lpx < px) direction = 2;
if (direction == 1) rotate(PI/20);
if (direction == 2) rotate(-PI/20);
beginShape(QUADS);
texture(guy);
if (direction == 2){
vertex(-90,-120,0,0,0);
vertex(90,-120,0,1,0);
vertex(90,120,0,1,1);
vertex(-90,120,0,0,1);
}
else{
vertex(90,-120,0,0,0);
vertex(-90,-120,0,1,0);
vertex(-90,120,0,1,1);
vertex(90,120,0,0,1);
}
endShape();
popMatrix();
lpx = px;
beginShape(QUADS);
noStroke();
fill(200,200,255);
vertex(-150,200,-100);
vertex(751,200,-100);
fill(255);
vertex(751,401,-100);
vertex(-150,401,-100);
endShape();
}
 
void keyPressed(){
if (key == ' '){
swarm.add(new Bee());
}
 
}
 
class Bee{
 
float x,dx,y,dy,z,dz,lx;
float beePower;
Vector trail = new Vector();
public Bee(){
x = random(0,300);
dx = 0;
y = 200 + random(-40,40);
dy =random(-5,5);
z = random(-50,50);
dz = 0;
beePower = 500 + random(-300,200);
}
 
void draw(){
if (random(0,100) < 35){
dx += random(-1,1);
dy += random(-1,1);
}
//gravitate towards the person
dx += -(x - px)/beePower;
dy += -(y - py)/beePower;
z += -z/beePower;
dx *=.999;
dy *=.999;
dz *=.999;
if (dx > 5) dx = 5;
if (dx < -5 ) dx = -5;
 
if (dy > 2) dy = 2;
if (dy < -2 ) dy = -2;
x += dx;
y += dy;
z += dz;
trail.add(new Point2D.Float(x,y));
if (trail.size() > 50) trail.removeElementAt(0);
Point2D.Float pnt;
stroke(0,0,0,100);
beginShape(LINES);
for (int i =0; i < trail.size(); i+=3){
pnt = (Point2D.Float)trail.elementAt(i);
vertex(pnt.x,pnt.y);
}
endShape();
 
pushMatrix();
noStroke();
translate(x,y,z);
scale(6,6);
rotate(atan2(dy,dx));
//don't fly upside down when going right to left
if (lx > x) rotate(PI);
beginShape(QUADS);
texture(bee);
vertex(-2,-1.75,0,0);
vertex(2,-1.75,1,0);
vertex(2,1.75,1,1);
vertex(-2,1.75,0,1);
endShape();
 
popMatrix();
lx = x;
}
 
 
}
 
 
void mouseDragged(){
 
px += (mouseX-pmouseX);
harmonicStep += .18;
harmonic = 7*cos(harmonicStep);
 
}