• fullscreen
• Boid.pde
• Flock.pde
• ```// The Boid class

class Boid {

color boidColor;
PVector loc;
PVector vel;
PVector acc;
float r;
float maxforce;    // Maximum steering force
float maxspeed;    // Maximum speed

Boid(PVector l, float ms, float mf) {
acc = new PVector(0,0);
vel = new PVector(random(-1,1),random(-1,1));
loc = l.get();
r = 2.0;
maxspeed = ms;
maxforce = mf;
boidColor = color(random(50,100), 255, random(50,100));
}

void run(ArrayList boids) {
flock(boids);
update();
borders();
render();
}

// We accumulate a new acceleration each time based on three rules
void flock(ArrayList boids) {
PVector sep = separate(boids);   // Separation
PVector ali = align(boids);      // Alignment
PVector coh = cohesion(boids);   // Cohesion
// Arbitrarily weight these forces
sep.mult(1.5);
ali.mult(1.0);
coh.mult(1.0);
// Add the force vectors to acceleration
}

// Method to update location
void update() {
// Update velocity
// Limit speed
vel.limit(maxspeed);
// Reset accelertion to 0 each cycle
acc.mult(0);
}

void seek(PVector target) {
}

void arrive(PVector target) {
}

// A method that calculates a steering vector towards a target
// Takes a second argument, if true, it slows down as it approaches the target
PVector steer(PVector target, boolean slowdown) {
PVector steer;  // The steering vector
PVector desired = target.sub(target,loc);  // A vector pointing from the location to the target
float d = desired.mag(); // Distance from the target is the magnitude of the vector
// If the distance is greater than 0, calc steering (otherwise return zero vector)
if (d > 0) {
// Normalize desired
desired.normalize();
// Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed)
if ((slowdown) && (d < 100.0)) desired.mult(maxspeed*(d/100.0)); // This damping is somewhat arbitrary
else desired.mult(maxspeed);
// Steering = Desired minus Velocity
steer = target.sub(desired,vel);
steer.limit(maxforce);  // Limit to maximum steering force
}
else {
steer = new PVector(0,0);
}
return steer;
}

void render() {
// Draw a triangle rotated in the direction of velocity
float theta = vel.heading2D() + PI/2;
fill(200,100);
stroke(boidColor);
pushMatrix();
translate(loc.x,loc.y);
point(0,0);
//rotate(theta);
//beginShape(TRIANGLES);
//vertex(0, -r*2);
//vertex(-r, r*2);
//vertex(r, r*2);
//endShape();
popMatrix();
}

// Wraparound
void borders() {
if (loc.x < -r) loc.x = width+r;
if (loc.y < -r) loc.y = height+r;
if (loc.x > width+r) loc.x = -r;
if (loc.y > height+r) loc.y = -r;
}

// Separation
// Method checks for nearby boids and steers away
PVector separate (ArrayList boids) {
float desiredseparation = 20.0;
PVector steer = new PVector(0,0,0);
int count = 0;
// For every boid in the system, check if it's too close
for (int i = 0 ; i < boids.size(); i++) {
Boid other = (Boid) boids.get(i);
float d = PVector.dist(loc,other.loc);
// If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
if ((d > 0) && (d < desiredseparation)) {
// Calculate vector pointing away from neighbor
PVector diff = PVector.sub(loc,other.loc);
diff.normalize();
diff.div(d);        // Weight by distance
count++;            // Keep track of how many
}
}
// Average -- divide by how many
if (count > 0) {
steer.div((float)count);
}

// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(vel);
steer.limit(maxforce);
}
return steer;
}

// Alignment
// For every nearby boid in the system, calculate the average velocity
PVector align (ArrayList boids) {
float neighbordist = 25.0;
PVector steer = new PVector(0,0,0);
int count = 0;
for (int i = 0 ; i < boids.size(); i++) {
Boid other = (Boid) boids.get(i);
float d = PVector.dist(loc,other.loc);
if ((d > 0) && (d < neighbordist)) {
count++;
}
}
if (count > 0) {
steer.div((float)count);
}

// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.sub(vel);
steer.limit(maxforce);
}
return steer;
}

// Cohesion
// For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
PVector cohesion (ArrayList boids) {
float neighbordist = 25.0;
PVector sum = new PVector(0,0);   // Start with empty vector to accumulate all locations
int count = 0;
for (int i = 0 ; i < boids.size(); i++) {
Boid other = (Boid) boids.get(i);
float d = loc.dist(other.loc);
if ((d > 0) && (d < neighbordist)) {
count++;
}
}
if (count > 0) {
sum.div((float)count);
return steer(sum,false);  // Steer towards the location
}
return sum;
}
}

```
```// The Flock (a list of Boid objects)

class Flock {
ArrayList boids; // An arraylist for all the boids

Flock() {
boids = new ArrayList(); // Initialize the arraylist
}

void run() {
for (int i = 0; i < boids.size(); i++) {
Boid b = (Boid) boids.get(i);
b.run(boids);  // Passing the entire list of boids to each boid individually
}
}

void addBoid(Boid b) {
}

void directBoids(PVector target) {
for (int i = 0; i < boids.size(); i++) {
Boid b = (Boid) boids.get(i);
b.seek(target);
}
}

}

```
```/**
* A quick experiment mod of Daniel Shiffman's Flocking example, creating a target item out of the mouse
*
*
* Flocking
* by Daniel Shiffman.
*
* An implementation of Craig Reynold's Boids program to simulate
* the flocking behavior of birds. Each boid steers itself based on
* rules of avoidance, alignment, and coherence.
*
*
*/

// this stores how many tweets we've gotten
int tweets = 0;
int numInit = 0;
// and this stores the text of the last tweet
String tweetText = "Please wait for the first Geo tagged tweet";

PVector locations[] = new PVector[50];
PShape worldmap;

String username = "###############";
String password = "########";

Flock flock;
PVector target;

void setup() {
size(1000, 600);

flock = new Flock();
// Add an initial set of boids into the system
for (int i = 0; i < numInit; i++) {
flock.addBoid(new Boid(new PVector(width/2,height/2), 5.0, 0.05));
}
smooth();
strokeWeight(2);
target = new PVector(width/2, height/2);

PFont font = createFont("FoundryGridnik-Light", 14);
textFont(font, 14);
// set up twitter stream object
TweetStream s = new TweetStream(this, "stream.twitter.com", 80, "1/statuses/sample.json", username, password);
s.go();

background(0);
}

void draw() {
//background(50);
noStroke();
fill(0,5);
rect(0,0,width,height);
strokeWeight(2);
flock.directBoids(target);
flock.run();

noStroke();
fill(0);
rect(0,0,width,60);
// and draw the text of the last tweet
fill(255);
text(tweetText, 50 , 50);
shape(worldmap, 100, 100, 800, 400);
drawLocations();
}

// Add a new boid into the System
void mousePressed() {
PVector mouseV = new PVector(mouseX, mouseY);
target = mouseV;
stroke(0,255,255);
fill(0);
ellipse(target.x, target.y, 15, 15);

}

// called by twitter stream whenever a new tweet comes in
void tweet(Status tweet) {

// testing a filter for word usage, could assign multiple actions to tweets with words containe
//if(tweet.text().contains("the")){
if(tweet.geo()!=null){
// print a message to the console just for giggles if you like
println("Geo Tweet from: " + tweet.text());

// store the latest tweet text
tweetText = tweet.text();
// place a circle on the map where this is located and relocate target
mapPoint(tweet.geo());
// bump our tweet count by one
tweets += 1;
flock.addBoid(new Boid(new PVector(0,0), 5.0, 0.05));
}

//println(tweet.geo());
}

void mapPoint(Geo tweetLoc) {
int lat = round((float)tweetLoc.latitude());
int lon = round((float)tweetLoc.longitude());

target = new PVector(map(lon,-180,180,100,900), map(lat,-90,90,550,150));

}

// push all items back in the array
for(int i = locations.length-1; i>0; i--){
locations[i] = locations[i-1];
}

// add the new location to the array
locations[0] = l;
}

void drawLocations(){
noStroke();
fill(255,0,0);
for(int i = 0; i<locations.length-2; i++){
if(locations[i]!=null)
ellipse(locations[i].x,locations[i].y,5,5);
}
}
```

### tweaks (0)

This sketch is running as Java applet, exported from Processing.

Report Sketch

Report for inappropriate content

Please provide details if possible:

Your have successfully reported the sketch. Thank you very much for helping to keep OpenProcessing clean and tidy :)

Make a Copyright Infringement claim

## Twitter Geo Loc Test

2
You must login/register to add this sketch to your favorites.

A quick sketch utilizing Dan Shiffman's flocking class, modified slightly, and Mark McBride's twitter stream library for interfacing with twitter, as a test of streaming data visualization

You need to login/register to comment.