Thursday, March 19, 2009

Saturday, March 14, 2009

Wednesday, March 11, 2009

ants and muds

import kGeom.*;import kRender.*;
kSpace space;
kWorld world1;final float envSize = 300;final int N = 300; // mud number in every layerfinal int M = 300; //ants numberfinal float CS = 10; //check step of ants and mudsfinal float pos_futurePos = 80;// range of the mudfinal float connent_line = 20*sqrt(3); //R2 when distance 0 && dist < target =" new" checkstep =" CS;" futurepos =" kVec.clone(pos);" mvel =" kVec.clone(vel);" i =" 0;" m =" (kMud)" mtarget =" kVec.clone(m.pos);" mdist =" mTarget.distance(futurePos);"> mDist){ closestMDist = mDist; closestM = i; } }else{ closestMDist = mDist; closestM = 0; } } // if the future position is within the radius of the obstacle then apply lateral force kMud m = (kMud) muds.get(closestM); if(closestMDist < to =" length" mdist =" pos.distance(m.pos);" lnm1 =" new" mvel =" new" acc=" new" vec =" new" maxvel =" 0;" maxforce =" 0;" to =" length" mdist =" pos.distance(m.pos);" lnm2 =" new" mvel =" new" sum =" new" count =" 0;" i =" 0" other =" (kAnt)" dist =" pos.distance(other.pos);"> 0) && (dist < diff =" kVec.clone(pos);"> 0) { sum.scale(1/(float)count); } sumSep = kVec.clone(sum); return sum; } // alignment kVec align (ArrayList pop) { kVec sum = new kVec(0,0,0); int count = 0; for (int i = 0 ; i < other =" (kAnt)" dist =" pos.distance(other.pos);"> 0) && (dist <> 0) { sum.scale(1/(float)count); sum.limit(maxForce); } return sum; }
// cohesion kVec cohesion (ArrayList pop) { kVec sum = new kVec(0,0,0); int count = 0; for (int i = 0 ; i < other =" (kAnt)" dist =" pos.distance(other.pos);"> 0) && (dist <> 0) { sum.scale(1/(float)count); return steer(sum, ht); } return sum; }
void render() {
// draw vectors float vecDisScale = 2; kLine ln = new kLine(pos, vel, vel.length()*vecDisScale); ln.setColor(255,0,0); //space.append(ln);
kLine ln1 = new kLine(pos, sumSep, sumSep.length()*vecDisScale); ln1.setColor(0,0,255); //space.append(ln1); // draw Ant if(maxVel<1){ ln2=" new" ln3 =" new" ln2 =" new" x =" envSize;" y =" envSize;" z =" envSize;"> envSize) pos.x = -envSize; if (pos.y > envSize) pos.y = -envSize; if (pos.z > envSize) pos.z = -envSize; }
}



class kMud{ kVec pos; float x; float y; float z; kRange mudRng; float rad = Rad; float R = pos_futurePos;//range of the mud float R2 = connent_line; //when distance

// a class which controls the agent population arraylist
class kWorld { ArrayList population; ArrayList muds; kWorld() { population = new ArrayList(); muds = new ArrayList(); }
// cycles through each agent passing the population to it void run(){ for (int i = 0; i < population.size(); i++) { kAnt a = (kAnt) population.get(i); a.step(this); } }
// adds an agent to the population void addAnt(kAnt a) { population.add(a); } // adds an obstacle to the obstacles array list void addMud(kMud m) { muds.add(m); } // calls render methods for all objects in the world void render(){ // agents for (int i = 0; i < population.size(); i++) { kAnt a = (kAnt) population.get(i); a.render(); } // obstacles for (int i = 0; i < muds.size(); i++) { kMud m = (kMud) muds.get(i); m.render(); } } }

Friday, March 6, 2009

aggregation code_mar06_v1_rv1

sketch code:

import kGeom.*;import kRender.*;
kWorld world1; //float intRad = 20; //float outRad = 30; void setup(){ size(1000,700); frameRate(50); smooth();
world1 = new kWorld();
// loop to create some agents for(int i = 0; i < 75; i++){ world1.addAgent( new kAgent( new kVec(random(1000),random(700)), new kVec(random(-1,1),random(-1,1)), random(1, 4), random(0.1,0.2),random(30,35), random (100,200))); }for(int i = 0; i < 400; i++){ world1.addAgent( new kAgent2( new kVec(random(1000),random(700)), new kVec(random(-2,2),random(-2,2)), random(2, 6), random(0.3,0.4))); }}
void draw(){ background(255); world1.run();
}void mousePressed(){ noLoop();
}
void mouseReleased(){ loop();
}

____________________________________________________________

kAgent:
class kAgent{
// variable or states kVec acc; // temporary vector calculated each step kVec vel; // speed and direction it is currently heading kVec pos; // where the agent is
float maxForce; // how fast the agent can turn float maxVel; // how fast the agent can move float intRad; float outRad; float rangeOfVision = 50; //int fillColor; //color fillColor;
// constructor kAgent(kVec _pos, kVec _vel, float _maxForce, float _maxVel, float _intRad, float _outRad){ pos = _pos; vel = _vel; maxForce = _maxForce; maxVel = _maxVel; acc = new kVec(0,0); intRad = _intRad; outRad = _outRad; //fillColor = int(random(255)); //println(fillColor); //fillColor = color(int(random(255)), int(random(255)), int(random(255))); }
// behaviors
// update - adds together all the vectors and moves the agent void update(ArrayList pop){ //changeColor(pop); // world1.addTrail(new kTrail( kVec.clone(pos) )); // call the collective behavior functions kVec sep = seperate(pop); kVec ali = align(pop); kVec coh = cohesion(pop); // weight - scale sep.scale(200.0);//the more sep, the more distance ali.scale(4.0); coh.scale(0.1);// the more coh, the more attraction // add to acc acc.plus(sep); acc.plus(ali); acc.plus(coh); // add acc to vel vel.plus(acc); // limit to maxVel vel.limit(maxVel); // add vel to pos pos.plus(vel); // reset the acc to 0 acc = new kVec(0,0); // call the borders function borders(); }
// function needs to input the population
kVec seperate(ArrayList pop){ kVec sum = new kVec(0,0,0); // makes a blank vector int count = 0; // loop through the population of agents for(int i = 0; i < other =" (kAgent)" dist =" pos.distance(other.pos);"> 0) && (dist < diff =" kVec.clone(pos);" count =" count"> 0){ sum.scale(1/(float)count); } // return the sum vector return sum; }
// align kVec align(ArrayList pop){ kVec sum = new kVec(0,0,0); int count = 0; for(int i = 0; i < other =" (kAgent)" dist =" pos.distance(other.pos);">0) && (dist <> 0){ sum.scale(1/(float)count); sum.limit(maxForce); } return sum; }
kVec cohesion(ArrayList pop){ kVec sum = new kVec(0,0,0); int count = 0; for(int i = 0; i < other =" (kAgent)" dist =" pos.distance(other.pos);">0) && (dist <> 0){ sum.scale(1/(float)count); // found the midpoint of all the surrounding agents return steer(sum); // steer towards that point } return sum; }
// steer - returns a vector to steer towards something kVec steer(kVec target){ // get a vector between the agents position and the target kVec steer; target.minus(pos); // vector between agent and target // get the distance between agent and target float distance = target.length();
// if distance is greater than 0 - calculate steer vector if(distance > 0){ target.normalize(); // makes the vector a length of 1 target.scale(maxVel); target.minus(vel); target.limit(maxForce); steer = kVec.clone(target); }else{ steer = new kVec(0,0); } return steer; }
// seek void seek(kVec target){ // calls steer function and adds the resultant vector to acc acc.plus(steer(target)); }
// render
void render() { fill(200); // stroke(255); stroke(0); rectMode(CENTER); rect(pos.x,pos.y,random(18,22),random(18,22)); fill(255); rect(pos.x,pos.y,random(20,24),random(20,24)); }
void borders(){ if(pos.x < x =" width;" y =" height;"> width) pos.x = 0; if(pos.y > height) pos.y = 0; }
}
___________________________________________________

kAgent2:

class kAgent2{
// variable or states kVec acc; // temporary vector calculated each step kVec vel; // speed and direction it is currently heading kVec pos; // where the agent is
float maxForce; // how fast the agent can turn float maxVel; // how fast the agent can move //float intRad; //float outRad; float rangeOfVision = 50; int fillColor; //color fillColor;
// constructor kAgent2(kVec _pos, kVec _vel, float _maxForce, float _maxVel){ pos = _pos; vel = _vel; maxForce = _maxForce; maxVel = _maxVel; //intRad = _intRad; //outRad = _outRad; acc = new kVec(0,0);
//println(fillColor); //fillColor = color(int(random(255)), int(random(255)), int(random(255))); }
// behaviors
// update - adds together all the vectors and moves the agent void update(ArrayList pop2){
// world1.addTrail(new kTrail( kVec.clone(pos) )); kVec avoidAg = avoidAgent(world1.population); kVec attAg = attractAgent(world1.population); // call the collective behavior functions kVec sep = seperate(world1.population2); kVec ali = align(world1.population2); kVec coh = cohesion(world1.population2); // weight - scale sep.scale(80.0);//the more sep, the more distance ali.scale(4.0); coh.scale(0.5);// the more coh, the more attraction avoidAg.scale(50.0); attAg.scale(1.0); // add to acc acc.plus(sep); acc.plus(ali); acc.plus(coh); acc.plus(avoidAg); acc.plus(attAg); // add acc to vel vel.plus(acc); // limit to maxVel vel.limit(maxVel); // add vel to pos pos.plus(vel); // reset the acc to 0 acc = new kVec(0,0); // call the borders function borders(); }
// function needs to input the population kVec avoidAgent (ArrayList pop){ // calculate the future pos float checkStep = 10; float closestAgDist = 9999999; int closestAg = 0; kVec futurePos = kVec.clone(pos); kVec AgVel = kVec.clone(vel); AgVel.scale(checkStep); futurePos.plus(AgVel); for(int i = 0; i < a =" (kAgent)pop.get(i);" agtarget =" kVec.clone(a.pos);" agdist =" AgTarget.distance(futurePos);" closestagdist =" AgDist;" closestag =" i;" a =" (kAgent)" agdist =" pos.distance(a.pos);" agvel =" new" dir =" new" closeagdist =" 99999999;" closestag =" 0;" closestrange =" 100;" i =" 0;" a =" (kAgent)pop.get(i);" agpos =" kVec.clone(a.pos);" agdist =" AgPos.distance(pos);" closeagdist =" AgDist;" closestag =" i;" closestrange =" a.outRad;" a =" (kAgent)pop.get(closestAg);" agtarget =" kVec.clone(a.pos);" dir =" kVec.clone(AgTarget);" sum =" new" count =" 0;" i =" 0;" other =" (kAgent2)" dist =" pos.distance(other.pos);"> 0) && (dist < diff =" kVec.clone(pos);" count =" count"> 0){ sum.scale(1/(float)count); } // return the sum vector return sum; }
// align kVec align(ArrayList pop2){ kVec sum = new kVec(0,0,0); int count = 0; for(int i = 0; i < other =" (kAgent2)" dist =" pos.distance(other.pos);">0) && (dist <> 0){ sum.scale(1/(float)count); sum.limit(maxForce); } return sum; }
kVec cohesion(ArrayList pop2){ kVec sum = new kVec(0,0,0); int count = 0; for(int i = 0; i < other =" (kAgent2)" dist =" pos.distance(other.pos);">0) && (dist <> 0){ sum.scale(1/(float)count); // found the midpoint of all the surrounding agents return steer(sum); // steer towards that point } return sum; }
// steer - returns a vector to steer towards something kVec steer(kVec target){ // get a vector between the agents position and the target kVec steer; target.minus(pos); // vector between agent and target // get the distance between agent and target float distance = target.length();
// if distance is greater than 0 - calculate steer vector if(distance > 0){ target.normalize(); // makes the vector a length of 1 target.scale(maxVel); target.minus(vel); target.limit(maxForce); steer = kVec.clone(target); }else{ steer = new kVec(0,0); } return steer; }
// seek void seek(kVec target){ // calls steer function and adds the resultant vector to acc acc.plus(steer(target)); }
// render
void render() { fill(200); // stroke(255); noStroke(); rectMode(CENTER); rect(pos.x,pos.y,random(16,20),random(16,20)); fill(50); rect(pos.x,pos.y,random(6,10),random(6,10)); }
void borders(){ if(pos.x < x =" width;" y =" height;"> width) pos.x = 0; if(pos.y > height) pos.y = 0; }
}
_______________________________________________________

kWorld:

class kWorld{ // arraylists of things ArrayList population; ArrayList population2; ArrayList trails; // constructor kWorld(){ population = new ArrayList(); population2 = new ArrayList(); trails = new ArrayList(); } // run void run(){ // loop through all the agents in the population for(int i = 0; i < population.size(); i++){ // get an agent out of the population arraylist kAgent a = (kAgent) population.get(i);
a.update(population); a.render(); } for(int i = 0; i < population2.size(); i++){ // get an agent out of the population arraylist kAgent2 a = (kAgent2) population2.get(i);
a.update(population2); a.render(); } } // add agents to the population void addAgent(kAgent a){ population.add(a); } void addAgent(kAgent2 a){ population2.add(a); }
}

___________________________________________________________________