Skip to content

Instantly share code, notes, and snippets.

@osresearch
Created December 20, 2018 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save osresearch/31992328c7589c81705e59489bb07cfd to your computer and use it in GitHub Desktop.
Save osresearch/31992328c7589c81705e59489bb07cfd to your computer and use it in GitHub Desktop.
Creative Coding Utrecht live coding swarm demo
class Bee
{
Bee() {}
float x = random(1000);
float y = random(1000);
float vx = random(10);
float vy = random(10);
float ox = x, oy = y;
float r = random(255);
float g = random(255);
float b = random(255);
float dt = 1;
void move(float tx, float ty)
{
ox = x;
oy = y;
float dx = tx - x;
float dy = ty - y;
float dist = sqrt(dx*dx+dy*dy);
if (dist == 0) dist = 1;
float a = 4;
vx += dx * a / dist + random(-4,4);
vy += dy * a / dist + random(-4,4);
float max_v = 10;
if (vx > max_v) vx = max_v;
if (vy > max_v) vy = max_v;
if (vx < -max_v) vx = -max_v;
if (vy < -max_v) vy = -max_v;
x += vx * dt;
y += vy * dt;
}
void draw()
{
stroke(r,g,b);
line(ox, oy, x, y);
}
};
Bee [] bees;
void setup()
{
fullScreen();
frameRate(30);
bees = new Bee[50];
for(int i = 0 ; i < 50 ; i++)
bees[i] = new Bee();
}
void draw()
{
background(0);
stroke(255);
for(int i = 0 ; i < 50 ; i++)
{
bees[i].move(mouseX, mouseY);
bees[i].draw();
}
stroke(255,0,0);
line(mouseX, mouseY, mouseX+1, mouseY+1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment