/////////Trash_code
////////by Lluis Alexandre Casanovas/Bryan Shi/Miranda Romer/Ezio Blassetti
///////(n)certainties 5.2. Columbia Fall 2o11
import peasy.*;
import toxi.geom.*;
import processing.opengl.*;
import processing.opengl.*;
PeasyCam cam;
///////////ARRAYLIST POPULATIONS/////////
ArrayList particle;
ArrayList repellant;
ArrayList attractor;
ArrayList attPop;
ArrayList repPop;
ArrayList trailPop;
PrintWriter output;
float accel = +20.3; // Attractors
float accel2 = -20; //Repellants
///////////GLOBAL SETTINGS/////////////
void setup() {
size(600, 500,OPENGL);
smooth();
cam = new PeasyCam(this,250,250,0,400);
//population list
particle = new ArrayList();
repellant = new ArrayList();
attractor = new ArrayList();
attPop = new ArrayList();
repPop = new ArrayList();
trailPop = new ArrayList();
/////////////////////////////
String [] points= loadStrings (“A1_3.txt”); //LOADING TXT FILE, HAS TO BE A CLEAN FILES WITH NUMBERS WITHOUT ” “for (int i = 0; i < points.length; i++){
// THIS TAKES ALL THE NUMBERS OF THE FILE AND SEPERATES THE NUMBERS EVERY TIME IT READS A COMMA, ( OPEN TESTER.TXT TO CHECK)
float [] onePointData = float(split(points[i], “,”));// I WILL THEN LOAD THIS FIRST NUMBER (0) AS MY X POS, SECOND (1) AS Y, THIRD (3) AS Z… APPLY IT TO THE POSITION OF MY REPELLER.
Vec3D pos = new Vec3D (onePointData[0], onePointData[1], onePointData[2]);
Vec3D vel= new Vec3D(0,0,0);// THIS IS THERE VELOCITY, THIS VALUES GIVE THEIR VECTORS DIRECTION.
float range = 20; // THIS IS THE RANGE OF THE REPELLER, THIS CHANGES THE ‘RADIUS OF VISION’
float magnitude = 5;
repellant.add(new Repellant(pos)); //
}
/*
for (int i = 0; i < 40; i++) {
Vec3D pos = new Vec3D (random(500),random(height),0)
repellant.add(new Repellant(pos));
}*/
for (int i = 0; i < repellant.size(); i++) {
Repellant c = (Repellant) repellant.get(i);
Vec3D pos = new Vec3D (c.pos.x, c.pos.y+10,0);
Vec3D vel = new Vec3D (0,random(0,.1),0);
attractor.add (new Attractor(pos,vel));
}
for (int i = 0; i < 7; i++) {
Repellant c = (Repellant) repellant.get(i);
Vec3D p = new Vec3D(c.pos.x+random (10), 0,0); //(random (600),0,random(50)); //random position
Vec3D v = new Vec3D(0,0.01,0); //random position
particle.add (new Particle(p,v,.02,.1));
}
////////////Attractors que fan mes maco el cabell/////////////
for (int i = 0; i< 80; i++) {
attPop.add(new attractor2(new Vec3D(random(width), random(3000), 0),20, random(-1,1)));
}
for (int i = 0; i< 20; i++) {
attPop.add(new attractor2(new Vec3D(random(width), random(100), 0),20, random(-1,1)));
}
////////////Repellers que fan mes maco el cabell/////////////
for (int i = 0; i< 100; i++) {
repPop.add(new repellant2(new Vec3D(random(width), random(height), 0),20, random(-1,1)));
}
for (int i = 0; i< 20; i++) {
repPop.add(new repellant2(new Vec3D(random(width), random(300), 0),20, random(-1,1)));
}
}
void draw() {
background(255);
for (int i = 0; i < particle.size(); i++) {
Particle a = (Particle) particle.get(i);
a.update();
//a.renderLines();
}
for (int i = 0; i < 1 ; i++) {
// Repellant c = (Repellant) repellant.get(i);
Vec3D p = new Vec3D(random (100,500),0,0); //random position
Vec3D v = new Vec3D(0,0.03,0); //random position
particle.add (new Particle(p,v,.2,.1));
}
for (int i = 0; i < repellant.size(); i++) {
Attractor b = (Attractor) attractor.get(i);
b.move();
b.render();
}
for (int i = 0; i < repellant.size(); i++) {
Repellant c = (Repellant) repellant.get(i);
c.render();
}
for (int i = 0; i < attPop.size(); i++) {
attractor2 t = (attractor2) attPop.get(i);
//t.render();
}
for (int i = 0; i < attPop.size(); i++) {
repellant2 t = (repellant2) repPop.get(i);
//t.render();
}
// TO DRAW THE TRAILS, I HAVE TO MAKE 2 LOOPS. LOOP THROUGH THE AGENT CLASS
for(int i = 0; i < particle.size(); i++) {
Particle a = (Particle) particle.get(i);
// THEN LOOP THROUGH EACH AGENTS TRAIL
for (int j = 0; j < a.trailPop.size(); j++) {
trail t = (trail) a.trailPop.get(j);
t.evaporate();
t.remove();
t.render();
}
}
}
void exportAgents() {
output = createWriter(“A1_3_p.txt”);
for(int i = 0; i < particle.size(); i++) {
Particle a = (Particle) particle.get(i);
// THEN LOOP THROUGH EACH AGENTS TRAIL
for (int j = 0; j < a.trailPop.size(); j++) {
trail t = (trail) a.trailPop.get(j);
output.print(t.pos.x + “,” + t.pos.y + “,” + t.pos.z + ” ” ); //x,y,z add a space to differentiate each agents set of points
print (“hi”);
}
output.println(“”);
}
// save the file
output.flush();
output.close();
}
void keyPressed()
{
if(key == ‘g’) {
print (“ho”);
exportAgents();
}
}
//////////////////////////////
class Particle {
//P_States
Vec3D pos;
Vec3D vel;
float vx;
float vy;
float vz;
float maxForce;
float maxVel;
ArrayList trailPop;// I NEED TO MAKE AN ARRAYLIST FOR EACH AGENT TO MAKE LINES
//P_Constructor
Particle(Vec3D _pos, Vec3D _vel, float mV, float mF) {
pos=_pos;
vel=_vel;
maxVel = mV;
maxForce = mF;
trailPop = new ArrayList();// EACH AGENT WILL HAVE AN ARRAYLIST OF ITS HISTORY FOR LINES
}
//P_Behaviors
void update() {
for (int i = 0; i < repellant.size(); i++) {
//behaviour when the particle is close to the attractor
Attractor b = (Attractor) attractor.get(i);
float d2 = sq(b.pos.x-pos.x) + sq(b.pos.y-pos.y); //+sq(b.pos.z-pos.z);
if (d2 > .01) {
vx += accel * (b.pos.x-pos.x) / d2;
vy += accel * (b.pos.y-pos.y) / d2;
pos.addSelf(vel);
}
}
//behavior when the code is close to the repellant
for (int i = 0; i < repellant.size(); i++) {
Repellant c = (Repellant) repellant.get(i);
float d2 = sq(c.pos.x-pos.x) + sq(c.pos.y-pos.y); //+sq(c.pos.z-pos.z);
if (d2 > 0.1) {
vx += accel2 * (c.pos.x-pos.x) / d2;
vy += accel2 * (c.pos.y-pos.y) / d2;
pos.addSelf(vel);
}
}
pos.x += vx;
pos.y += vy;
pos.z += vz;
vx = 0;
vy = 1;
vy = 01;
// TRAILS ARE MADE USING THE HISTORY OF THE AGENTS POSITION, THIS IS WHY THE ARRAYLIST IS ADDED TO HERE
//drawParticle();
//stroke(0,255,0,20);
//fill(0,255,0);
//point(pos.x, pos.y,pos.z);
//}
// TRAILS ARE MADE USING THE HISTORY OF THE AGENTS POSITION, THIS IS WHY THE ARRAYLIST IS ADDED TO HERE
if (frameCount %.25 == 0) {
Vec3D pos = this.pos.copy();
float range = 5; // HERE I MADE THE RANGE SLIGHTLY LOWER SO THEY DONT LOOK AT TOO MANY TRAILS
float magnitude = 255;
trailPop.add(new trail(pos,range, magnitude));
}
Vec3D acc = new Vec3D();
// call the vector functions
Vec3D ste = steer(new Vec3D(mouseX,mouseY,1));
Vec3D ali = alignment(particle, 10);
Vec3D coh= cohesion (particle, random(50));
Vec3D sep= separation(particle, random(10,30));
Vec3D att = seekTrail(trailPop);
Vec3D attAt = seekAttractor(attPop,60);
Vec3D rep = seekAttractor(attPop,60);
// weight or scale vectors
ste.scaleSelf(1);
ali.scaleSelf(random(3,5));
coh.scaleSelf(random(3,5));
sep.scaleSelf(random(3,6));
att.scaleSelf(3);//10
attAt.scaleSelf(3);//10
rep.scaleSelf(3);//10
sep.scaleSelf(random(3,6));
// add vectors to acc
acc.addSelf(ste);
acc.addSelf(ali);
acc.addSelf(coh);
acc.addSelf(sep);
acc.addSelf(att);
acc.addSelf(attAt);
acc.addSelf(rep);
// vel = vel + acc
//vel.addSelf(acc);
vel.limit(maxVel);
// pos = pos + vel
pos.addSelf(vel);
pos.addSelf(acc);
}
////////////// S T E E R ///////////
Vec3D steer(Vec3D target){
// make a vector from the pos to the target: vec = target – pos
Vec3D vec = target.sub(pos);
float d = vec.magnitude();
if(d > 0){
// normalize the vector – magnitude of 1
vec.normalize();
// scale to length of maxVel: vec = vec x maxVel
vec.scaleSelf(maxVel);
// subtract vel
vec.subSelf(vel);
// limit by maxForce
vec.limit(maxForce);
}else{
vec = new Vec3D(); // 0,0,0
}
// return the vector
return vec;
}
//////////////////////
Vec3D seekTrail(ArrayList trailPop) {
Vec3D sum = new Vec3D();
int count = 0;
for (int i = 0; i < trailPop.size(); i++) {
trail a = (trail) trailPop.get(i);
float dist = a.pos.distanceTo(this.pos);
//if distance < range of attractor
if (dist < a.range) {
Vec3D vec = a.pos.sub(this.pos);
vec.scaleSelf(a.mag);
sum.addSelf(vec);
count++;
}
}
//sum
if (count > 0) {
sum.scaleSelf(1/(float)count);
sum.limit(maxForce);
}
return sum;
}
////////////A L I G N M E N T /////////
Vec3D alignment(ArrayList pop, float range){
Vec3D sum = new Vec3D(); // 0,0,0
int count = 0;
// loop through each agent in pop
for(int i = 0; i < pop.size(); i++){
Particle a = (Particle) particle.get(i);
// find the distance between the current agent and the other agent
float d = pos.distanceTo(a.pos);
// if distance < range
if( (d < range) && (d > 0) ){ //(d>0)
// sum = sum + other agents vel
sum.addSelf(a.vel);
//count = count + 1
count++;
}
}
// find the average: sum/count: sum = sum x 1/count
if(count > 0){
sum.scaleSelf(1/(float)count);
// limit the average vector to maxForce
sum.limit(maxForce);
}
// return the limited vector
return sum;
}
///////////// C O H E S I O N //////////////////
Vec3D cohesion(ArrayList pop, float range){
// make 0 vector sum, 0 int count
Vec3D sum = new Vec3D();
int count = 0;
// loop through all agents
for(int i = 0; i < pop.size(); i++){
// get one agent out of the arraylist
Particle a = (Particle) particle.get(i);
// calculate distance from pos to the other agent pos
float d = pos.distanceTo(a.pos);
// if distance < range && other agent is not this agent
if( (d < range) && (d>0)){ // (a != this) ){
// add other agent pos to sum, add 1 to count: sum = sum + a.pos
sum.addSelf(a.pos);
count++;
}
}
// find the average pos: sum/count: sum = sum x 1/count
if(count > 0){
sum.scaleSelf(1/(float)count); // sum is now the average pos
// steer toward the average pos
sum = steer(sum);
}
// return that vector
return sum;
}
///////////// S E P A R A T I O N //////////////////
Vec3D separation(ArrayList pop, float range){
// make 0 vector
Vec3D sum = new Vec3D(0,0,0);
float count=0;
// loop through each agent
for(int i = 0; i < pop.size(); i++){
// get an agent out of the arrayList
Particle a = (Particle) particle.get(i);
// calculate distance to the agent
float d = pos.distanceTo(a.pos);
// if distance is less than range
if( (d < range) && (d>0)){ //( a != this) ){
// make a vector between the other agent and this agent
Vec3D vec = pos.sub(a.pos);
//vec.addSelf(pos);
vec.normalize(); // BIG CHANGE WHEN ITS NORMALIZED AND WHEN IT IS NOT
// scale vector by 1/distance
vec.scaleSelf(1/d);
// add the vector to sum
sum.addSelf(vec);
count++;
}
}
// limit to maxForce
if (count>0){
sum.scaleSelf(1/(float)count);
sum.limit(maxForce);
}
// return vector
return sum;
}
Vec3D seekAttractor (ArrayList attAtPop, float range) {
Vec3D sum = new Vec3D();
int count = 0;
for (int i = 0; i < attPop.size(); i++) {
attractor2 t = (attractor2) attPop.get(i);
float dist = t.pos.distanceTo(this.pos);
//if distance < range of attractor
if (dist < range ) {
Vec3D vec = t.pos.sub(this.pos);
//pos = t.pos.copy();
vec.normalize();
vec.scaleSelf(maxVel);//mag
sum.addSelf(vec);
count++;
}
}
//sum
if (count > 0) {
sum.scaleSelf(1/(float)count);
sum.limit(.1);
}
return sum;
}
Vec3D seekRepellant(ArrayList attVirusPop) {
Vec3D sum = new Vec3D();
int count = 0;
//loop thru all the attractors
for(int i = 0; i < repPop.size(); i++) {
repellant2 c = (repellant2) repPop.get(i);
//get the distance to the attractor
float dist = c.pos.distanceTo(this.pos);
//if distance < range of attractor
if(dist < c.range && inView(c.pos, 1) == true) {
Vec3D vec = c.pos.sub(this.pos);
//multiply vector by mag, i.e. push or pull force of the attractor
vec.scaleSelf(c.mag);
sum.addSelf(vec);
count++;
}
}
return sum;
}
Boolean inView(Vec3D target, float angle) { //check if attractor is in the “view” of current agent
Vec3D vec = target.sub(this.pos);
float angle1 = vec.angleBetween(this.vel, true); //angle between this.vel and vector from this to attractor
if (angle1 > angle || angle1 < -angle) {
return false;
}
else {
return true;
}
}
//void drawParticle(){
//strokeWeight(1);
//stroke(0,0,0,200);
//fill(0);
//point(pos.x,pos.y);
//output.println(pathX + “,” + AGENT.loc.y + “,” + AGENT.loc.z);
//}
void renderLines(){
// HERE I AM LOOPING THROUGH THE TRAILS, THEN MAKING A CURVE THROUGH AGENTS TRAILPOP
beginShape();
for (int j = 0; j < trailPop.size(); j++){
trail p = (trail) trailPop.get(j);
stroke(0,0,0,200);
strokeWeight(.5);
vertex(p.pos.x,p.pos.y,p.pos.
bezierVertex (p.pos.x,p.pos.y,p.pos.z,p.
}
endShape();
}
}
//////////////////////////////
class Attractor{
//R_states
Vec3D pos;
Vec3D vel;
boolean locked = false;
//R_constructor
//It’s static so we just need the position
Attractor(Vec3D _pos, Vec3D _vel) {
pos = _pos;
vel= _vel;
}
//R_Behaviors
void move(){
pos.addSelf(vel);
}
void render() {
stroke(0,0,255);
//Drawing a cross
line(pos.x-2, pos.y, pos.z, pos.x+2, pos.y,pos.z);
line(pos.x, pos.y-2, pos.z, pos.x, pos.y+2, pos.z);
}
}
////////////////////////////// R E P E L L A N T /////////////////////////////
class Repellant{
//R_states
Vec3D pos;
boolean locked = false;
//R_constructor
//It’s static so we just need the position
Repellant (Vec3D _pos) {
pos = _pos;
}
//R_Behaviors
void render() {
strokeWeight(1);
stroke(0,0,0);
//Drawing a cross
line(pos.x-2, pos.y, pos.z, pos.x+2, pos.y,pos.z);
line(pos.x, pos.y-2, pos.z, pos.x, pos.y+2, pos.z);
}
}
////////////SECONDARY REPELLANTS
class repellant2 {
Vec3D pos;
float range;
float mag; //magnitude of push or pull force
repellant2(Vec3D _pos, float _range, float _mag) {
pos = _pos;
range = _range;
mag = _mag;
}
void render(){
fill(255,0,0); //from 255 to disappear
ellipse(pos.x,pos.y,3,3);
}
}
////////TRAIL//////////
class trail{
//T_states
Vec3D pos;
float range;
float mag; //magnitude of the push or pull force
//T_constructor
trail(Vec3D _pos, float _range, float _mag) {
pos = _pos;
range = _range;
mag = _mag;
}
//T_behaviors
void evaporate(){
mag=mag-0.1; //-.009 Lenght control
}
void remove(){
if (mag < 0){
//trailPop.remove(this);
}
}
void render() {
//want to render lines
strokeWeight(1);
stroke(0,255,76,100);
point(pos.x,pos.y,pos.z); // Form/size selected for the trai;
}
}
///////////////ATTRACTOR 2////////////////
class attractor2 {
Vec3D pos;
float range;
float mag; //magnitude of push or pull force
attractor2(Vec3D _pos, float _range, float _mag) {
pos = _pos;
range = _range;
mag = _mag;
}
void render(){
fill(255,0,0); //from 255 to disappear
ellipse(pos.x,pos.y,1,1);
}
}