A103
SWARM [Attractor to Line]
Use the code we did already
Spline3D sp2; void setup(){ ……………
Set up the points
sp2 = new Spline3D(); for(int i=0; i<20; i++){ Vec3D v = new Vec3D(-300+(600/20*i), random(-50,50), random(-50,50)+100); sp2.add(v); } .............................. } void draw(){ background(205); buildBox(DIMX, DIMY,DIMZ); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); }
for(int i= 1; i<sp2.pointList.size(); i++){ Vec3D a = sp2.pointList.get(i); Vec3D b = sp2.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, coh,ali,sep); pa.setMaxspeed(maxSpeed); pa.wrapSpace(DIMZ/2, DIMY/2, DIMZ/2); …………………… ………………………………………………………… gui(); }
Draw the line
A PLAN A SWARM A LINE
Follow the step to let the swarm follow the line
void draw(){ background(205); buildBox(DIMX, DIMY,DIMZ); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } for(int i= 1; i<sp2.pointList.size(); i++){ Vec3D a = sp2.pointList.get(i); Vec3D b = sp2.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, coh,ali,sep); pa.setMaxspeed(maxSpeed); pa.wrapSpace(DIMZ/2, DIMY/2, DIMZ/2); …………………….…… …………….. …………
float m = map(mouseX,0,width,-5,5); Vec3D fLoc = pa.futureLoc(5); Vec3D cns = pa.closestNormalandDirectionToSpline(sp2,fLoc,m); pa.seek(cns,5); } gui(); }
Put it Back into 2D
Spline3D sp2; void setup(){ ……………
Make this to “0”
sp2 = new Spline3D(); for(int i=0; i<20; i++){ Vec3D v = new Vec3D(-300+(600/20*i), random(-50,50), 0 ); sp2.add(v); } .............................. } void draw(){ background(205); buildBox(DIMX, DIMY,DIMZ); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } for(int i= 1; i<sp2.pointList.size(); i++){ Vec3D a = sp2.pointList.get(i); Vec3D b = sp2.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, coh,ali,sep); pa.setMaxspeed(maxSpeed); pa.wrapSpace(DIMZ/2, DIMY/2, DIMZ/2); …………………… ………………………………………………………… gui(); }
Put the starting point randomly
Spline3D sp2;
Just that simple
void setup(){ …………… for(int i=0; i<pop; i++){ Vec3D v = new Vec3D(random(-300,300),random(-300,300),random(-300,300)); Ple_Agent pa = new Ple_Agent(this, v); Vec3D initialVelocity = new Vec3D (random(-1, 1), random(-1, 1), random(-1,1)); pa.setVelocity(initialVelocity); pa.initTail(50); boids.add(pa); } .............................. }
Now I want to put the starting points to the 2 sides of the boundary
Spline3D sp2; void setup(){ …………… vall = new ArrayList(); for(int i=0; i<=pop; i++){ Vec3D v1 = new Vec3D(-300+2*(600/pop)*i,300,0); vall.add(v1); } Separate else{ Vec3D v2 = new Vec3D(-900+2*(600/pop)*i,-300,0); vall.add(v2); } Vec3D v = new Vec3D(random(-300,300),random(-300,300),random(-300,300)); Vec3D v0 = (Vec3D)vall.get(i); Ple_Agent pa = new Ple_Agent(this, v0); Vec3D initialVelocity = new Vec3D (random(-1, 1), random(-1, 1), random(-1,1)); pa.setVelocity(initialVelocity); pa.initTail(50); boids.add(pa);
} void draw(){ background(205); buildBox(DIMX, DIMY,DIMZ); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } ………………….. …………….. ………………………………………………………… gui(); }
the starting points
Turn the trail on
Move the line back to 3D
Make 2 lines for Attractors
Just did it Twice!
Spline3D sp2; Spline3D sp3; void setup(){ …………… sp2 = new Spline3D(); sp3 = new Spline3D(); for(int i=0; i<20; i++){ Vec3D v = new Vec3D(-300+(600/20*i), random(-50,50), random(50,50)+100); sp2.add(v); } for(int i=0; i<20; i++){ Vec3D v = new Vec3D(-900+(600/20*i), 200+random(-50,50), random(-50,50)+100); sp3.add(v); } .............................. }
void draw(){ background(205); ……. for(int i= 1; i<sp2.pointList.size(); i++){ Vec3D a = sp2.pointList.get(i); Vec3D b = sp2.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } for(int i= 1; i<sp3.pointList.size(); i++){ Vec3D a = sp3.pointList.get(i); Vec3D b = sp3.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } ….. .. float m = map(mouseX,0,width,-5,5); Vec3D fLoc = pa.futureLoc(5); Vec3D cns01 = pa.closestNormalandDirectionToSpline(sp2,fLoc,-5); Vec3D cns02 = pa.closestNormalandDirectionToSpline(sp3,fLoc,-5); if(Attract){ pa.seek(cns01,factor); pa.seek(cns02,factor); } ….. ………………………………………………………… gui(); }
Make 2 lines for Attractors
Make 2 Circles for Attractors
Just do it Twice!
Spline3D sp2; Spline3D sp3; void setup(){ …………… sp2 = new Spline3D(); sp3 = new Spline3D(); for(int i=0; i<20; i++){ //Vec3D v = new Vec3D(-300+(600/20*i), random(-50,50), random(50,50)+100); Vec3D v = new Vec3D (100*sin(2*PI*i/18),100*cos(2*PI*i/18),100); sp2.add(v); } for(int i=0; i<20; i++){ //Vec3D v = new Vec3D(-300+(600/20*i), 200+random(-50,50), random(50,50)+100); Vec3D v = new Vec3D (250+20*sin(2*PI*i/18),80+20*cos(2*PI*i/18),-100); sp3.add(v); } .............................. }
void draw(){ background(205); ……. for(int i= 1; i<sp2.pointList.size(); i++){ Vec3D a = sp2.pointList.get(i); Vec3D b = sp2.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } for(int i= 1; i<sp3.pointList.size(); i++){ Vec3D a = sp3.pointList.get(i); Vec3D b = sp3.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } ….. .. Vec3D fLoc = pa.futureLoc(5); Vec3D cns01 = pa.closestNormalToSpline(sp2,fLoc); Vec3D cns02 = pa.closestNormalToSpline(sp3,fLoc); if(Attract){ pa.seek(cns01,factor); pa.seek(cns02,factor);
}
}
gui();
Just do it 4 times(both Lines and agents) For the “SETUP” Spline3D sp2; Spline3D sp3; Spline3D sp4; Spline3D sp5;
void setup(){ …………… sp2 = new Spline3D(); sp3 = new Spline3D(); sp4 = new Spline3D(); sp5 = new Spline3D(); for(int i=0; i<20; i++){ Vec3D v = new Vec3D (150+20*sin(2*PI*i/18),20*cos(2*PI*i/18),0); sp2.add(v); } for(int i=0; i<20; i++){ Vec3D v = new Vec3D (150+100*sin(2*PI*i/18),150+100*cos(2*PI*i/18),125); sp3.add(v); } for(int i=0; i<20; i++){ Vec3D v = new Vec3D (-150+100*sin(2*PI*i/18),-150+100*cos(2*PI*i/18),-125); sp4.add(v); } for(int i=0; i<20; i++){ Vec3D v = new Vec3D (-150+20*sin(2*PI*i/18),20*cos(2*PI*i/18),0); sp5.add(v); }
……………………. …………………………………………. vall = new ArrayList(); for(int i=0; i<=pop; i++){ if(i<pop/4){ Vec3D v1 = new Vec3D(-300+(4*600/pop)*i,300,0); vall.add(v1); } else if(i>=pop/4 && i<pop/2){ Vec3D v2 = new Vec3D(-900+(4*600/pop)*i,-300,0); vall.add(v2); } else if(i>=pop/2 && i<3*pop/4){ Vec3D v3 = new Vec3D(300,-1500+(4*600/pop)*i,0); vall.add(v3); } else{ Vec3D v4 = new Vec3D(-300,-2100+(4*600/pop)*i,0); vall.add(v4); } //Vec3D v = new Vec3D(random(-300,300),random(-300,300),random(300,300)); Vec3D v0 = (Vec3D)vall.get(i); ……………….. ………. ……………………. . ………………………….. ………………………………….
}
Just do it 4 times(both Lines and agents) For the “DRAW” void draw(){ …………… if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } for(int i=1; i<sp2.pointList.size(); i++){ Vec3D a = sp2.pointList.get(i); Vec3D b = sp2.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } for(int i=1; i<sp3.pointList.size(); i++){ Vec3D a = sp3.pointList.get(i); Vec3D b = sp3.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } for(int i=1; i<sp4.pointList.size(); i++){ Vec3D a = sp4.pointList.get(i); Vec3D b = sp4.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } for(int i=1; i<sp5.pointList.size(); i++){ Vec3D a = sp5.pointList.get(i); Vec3D b = sp5.pointList.get(i-1); stroke(0,90); line(a.x,a.y,a.z,b.x,b.y,b.z); } …. ……
……………………. …………………………………………. if(showTrails){ pa.dropTrail(5,800); //stroke(0,90); stroke(pa.loc.x,pa.loc.y,pa.loc.z); strokeWeight(1); pa.drawTrail(200); } ………………………………………. ……………………………… ……………………………….. float m = map(mouseX,0,width,-5,5); Vec3D fLoc = pa.futureLoc(5); Vec3D cns01 = pa.closestNormalToSpline(sp2,fLoc); Vec3D cns02 = pa.closestNormalToSpline(sp3,fLoc); Vec3D cns03 = pa.closestNormalToSpline(sp4,fLoc); Vec3D cns04 = pa.closestNormalToSpline(sp5,fLoc); if(Attract){ pa.seek(cns01,0); pa.seek(cns02,10); pa.seek(cns03,10); pa.seek(cns04,0); } } ……… ……… …. }