Intro Code
Playing around with processing.js and old bits of code from Daniel Shiffman’s flocking, I decided to create some intro script for my blog. The processing.js version was working fine but was a bit slow… also had some problems when loading an image or displaying different fonts. So I went back to the java version that can handle higher populations // better speed.
Here is the code:
//code version written by Jose Sanchez
PFont headline;
PFont local;
PFont small;
ArrayList agents;
float desiredseparation = 30.0;
float neighbordistC = 100;
float neighbordistA = 40;
PImage b;
void setup() {
size(1440,300);
background(0);
smooth();
agents = new ArrayList();
headline = createFont("FFScala-32.vlw", 20);
local = createFont("FFScala-32.vlw", 10);
small = createFont("FFScala-32.vlw", 7);
for(int i = 0; i < 500 ;i++) {
agents.add(new Agent(new PVector(random(width),0)));
}
frameRate(30);
}
void draw() {
//background(0,1);
noStroke();
fill(0,9);
rect(0,0,width,height);
for(int i = 0; i < agents.size(); i++) {
Agent a = (Agent) agents.get(i);
a.run();
}
if(mouseX > 0 && mouseX < 300) {
if(mouseY > 200 && mouseY < height) {
fill(255);
if(mousePressed) {
link("http://genware.org/blog/");
}
}
else {
fill(255,10);
}
}
else {
fill(255,10);
}
noStroke();
//text label
textFont(headline);
text("[GENWARE.ORG]", 30, 270);
textFont(local);
text("// OPEN KNOWLEDGE REPOSITORY", 190, 270);
textFont(small);
text("++ CLICK ON SCREEN TO ADD MORE AGENTS", 30, 20);
text("++ CLICK ON TITLE TO CONTINUE", 30, 30);
if(mousePressed) {
agents.add(new Agent(new PVector(mouseX,mouseY)));
}
}
//code version written by Jose Sanchez
class Agent {
PVector loc = new PVector(width/2,height/2);
PVector vel = new PVector(random(-1,1),random(-1,1));
PVector acc= new PVector(0,0);
float r = 12;
float maxspeed =9;
float maxforce = 0.4;
float maxVel = 9;
float vx, vy;
float stiffness = 0.006;
float damping = -0.7;
Agent(PVector _loc) {
loc = _loc;
}
void run() {
display();
flock();
borders();
}
void flock() {
PVector sep = new PVector(0,0);
PVector ali = new PVector(0,0);
PVector coh = new PVector(0,0);
PVector sum = new PVector(0,0);
int count1 = 0;
int count2 = 0;
int count3 = 0;
if(frameCount % 1 == 0) {
for (int i = 0 ; i < agents.size(); i++) {
Agent other = (Agent) agents.get(i);
float d = PVector.dist(loc,other.loc);
//-----------------------------------------------------------------
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
sep.add(diff);
count1++; // Keep track of how many
}
//-----------------------------------------------------------------
if ((d > 0) && (d < neighbordistA)) {
ali.add(other.vel);
count2++;
}
//-----------------------------------------------------------------
if ((d > 0) && (d < neighbordistC)) {
sum.add(other.loc); // Add location
count3++;
}
}
}
//-----------------------------------------------------------------
//-----------------------------------------------------------------
if (count1 > 0) {
sep.div((float)count1);
}
// As long as the vector is greater than 0
if (sep.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
sep.normalize();
sep.mult(maxspeed);
sep.sub(vel);
sep.limit(maxforce);
}
//-----------------------------------------------------------------
if (count2 > 0) {
ali.div((float)count2);
ali.limit(maxforce);
}
// As long as the vector is greater than 0
if (ali.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
ali.normalize();
ali.mult(maxspeed);
ali.sub(vel);
ali.limit(maxforce);
}
//-----------------------------------------------------------------
if (count3 > 0) {
sum.div((float)count3);
sum.sub(loc);
sum.normalize();
sum.mult(maxVel);
sum.sub(vel);
sum.limit(maxforce);
coh.add(sum); // steer(coh,false); // Steer towards the location
}
sep.mult(3);
ali.mult(0.1);
coh.mult(0.2);
acc.add(sep);
acc.add(ali);
acc.add(coh);
vel.add(acc);
vel.limit(maxVel);
loc.add(vel);
acc = new PVector(0,0);
}
// Wraparound
void borders() {
if (loc.x < 0) loc.x = width;
if (loc.y < 0) loc.y = height;
if (loc.x > width) loc.x = 0;
if (loc.y > height) loc.y = 0;
}
void display() {
fill(3,180,255);
float v = vel.mag();
float c = map(v,0,maxVel,255,100);
float c2 = map(v,0,maxVel,100,255);
fill(0,c2,255);
//stroke(255);
noStroke();
strokeWeight(1);
ellipse(loc.x,loc.y,r,r);
PVector ep = new PVector(vel.x,vel.y);
ep.normalize();
ep.mult(8);
ep.add(loc);
stroke(255);
//line (loc.x,loc.y,ep.x,ep.y);
}
}
Responses (3) to “Intro Code”

Valuable information and excellent design you got here! I must are grateful for sharing your mind and time in to the stuff you post!! Thumbs up
Heyyy I only wanted to stop by and say i love reading your blog.
I like how the song was written and the tone, too; it s so like Eminem. I m just not that iesrepsmd with the video, it s too simple for me. They could have added some effects to make it look livelier.