A101
SWARM [Basic]
功夫 Kong Fu
You need to practice !
Separation: steer to avoid crowding local flockmates
Cohesion: steer to move toward the average position of local flockmates
http://www.red3d.com/cwr/boids/
Alignment: steer towards the average heading of local flockmates
http://vimeo.com/38600889
Plethora Swarm Agent Based Library
Get the library here: http://www.plethora-project.com/Plethora-0.3.0/index.html
Let’s put all the library we need to build up the SWARM
Let’s import these
Set up All for the 3D environment import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core.*; import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3;
void draw(){ background(235); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } fill(20); box(50); gui(); }
Test
void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; }
PeasyCam cam;
void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 0,0,0,600); initGUI(); controlP5.setAutoDraw(false); }
ControlP5 controlP5; void initGUI() { controlP5 = new ControlP5(this); }
For GUI Tag
import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core. import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3; PeasyCam cam;
void draw(){ background(235); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); }
gui(); }
int pop = 600; int DIMX = 600; int DIMY=600;int DIMZ=600; ArrayList <Ple_Agent>boids;
void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; }
void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 600); initGUI(); boids = new ArrayList<Ple_Agent>();
Set the
for(int i=0; i<pop; i++){ starting place Vec3D v = new Vec3D(0,0,0); Ple_Agent pa = new Ple_Agent(this, v); Vec3D initialVelocity = new Vec3D (random(-1, 1), random(-1, 1), 0 ); pa.setVelocity(initialVelocity); pa.initTail(5); boids.add(pa); Set the initial } controlP5.setAutoDraw(false); velocity }
Set up the Swarm
LET the SWARM ALIVE
import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core.*; import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3; PeasyCam cam; ControlP5 controlP5;
void draw(){ background(235); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } noFill(); stroke(0,90); strokeWeight(1); rect(-DIMX/2,-DIMY/2,DIMX,DIMY);
int pop = 600; int DIMX = 600; int DIMY=600; int DIMZ=600; ArrayList <Ple_Agent>boids;
for(Ple_Agent pa : boids){ //cohesion, alignment, separation pa.flock(boids, 80,40,30, 1,0.5,1.5); pa.setMaxspeed(3); pa.wrapSpace(DIMX/2, DIMY/2, DIMZ/2); pa.update();
void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 600); initGUI();
stroke(0); strokeWeight(5); pa.displayPoint(); } gui();
boids = new ArrayList<Ple_Agent>();
}
void gui() { Set the for(int i=0; i<pop; i++){ currCameraMatrix = new PMatrix3D(g3.camera); Vec3D v = new Vec3D(0,0,0); starting place camera(); Ple_Agent pa = new Ple_Agent(this, v); controlP5.draw(); Vec3D initialVelocity = new Vec3D (random(-1, 1), random(-1, 1), 0); g3.camera = currCameraMatrix; pa.setVelocity(initialVelocity); } pa.initTail(5); boids.add(pa); Set the initial } controlP5.setAutoDraw(false); velocity }
Press Run and see the Swarm Start
How about 3D?
import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core. import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3; PeasyCam cam; ControlP5 controlP5;
int pop = 600; int DIMX = 600; int DIMY=600;int DIMZ=600; ArrayList <Ple_Agent>boids; void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 600); initGUI(); boids = new ArrayList<Ple_Agent>();
void draw(){ background(235); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } noFill(); stroke(0,90); strokeWeight(1); rect(-DIMX/2,-DIMY/2,DIMX,DIMY); for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, 1,0.5,1.5); pa.setMaxspeed(3); pa.wrapSpace(DIMZ/2, DIMY/2, DIMZ/2); pa.update(); stroke(0); strokeWeight(5); pa.displayPoint(); } gui(); } void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; }
for(int i=0; i<pop; i++){ Vec3D v = new Vec3D(0,0,0); 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(5); boids.add(pa); Set the z random(-1,1) } controlP5.setAutoDraw(false); as well }
Draw the Boundary
import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core.*; import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3; PeasyCam cam; ControlP5 controlP5;
int pop = 600; int DIMX = 600; int DIMY=600;int DIMZ=600; ArrayList <Ple_Agent>boids; void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 600); initGUI();
boids = new ArrayList<Ple_Agent>(); for(int i=0; i<pop; i++){ Vec3D v = new Vec3D(0,0,0); 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(5); boids.add(pa); } controlP5.setAutoDraw(false); }
void draw(){ if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); }
background(235); noFill(); stroke(0,90); strokeWeight(1); box(DIMX, DIMY, DIMZ);
Draw the Boundary
for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, 1,0.5,1.5); pa.setMaxspeed(3); pa.wrapSpace(DIMX/2, DIMY/2, DIMZ/2); pa.update(); stroke(0); strokeWeight(5); pa.displayPoint(); } gui(); } void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; }
Bounce Back From the Boundary
import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core.*; import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3; PeasyCam cam; ControlP5 controlP5;
int pop = 600; int DIMX = 600; int DIMY=600;int DIMZ=600; ArrayList <Ple_Agent>boids; void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 600); initGUI(); boids = new ArrayList<Ple_Agent>(); for(int i=0; i<pop; i++){ Vec3D v = new Vec3D(0,0,0); 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(5); boids.add(pa); } controlP5.setAutoDraw(false); }
void draw(){ if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); }
background(235); noFill(); stroke(0,90); strokeWeight(1); box(DIMX, DIMY, DIMZ); for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, 1,0.5,1.5); pa.setMaxspeed(3); //pa.wrapSpace(DIMX/2, DIMY/2, DIMZ/2);
pa.bounceSpace(DIMX/2, DIMY/2, DIMZ/2); pa.update(); stroke(0); strokeWeight(5); pa.displayPoint(); } gui(); } void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; } void buildBox(float x, float y, float z){ noFill(); stroke(0,90); strokeWeight(1); pushMatrix(); scale(x,y,z); box(1); popMatrix(); }
Draw tails
import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core.*; import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3; PeasyCam cam; ControlP5 controlP5;
void draw(){ if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } background(235); noFill(); stroke(0,90); strokeWeight(1); box(DIMX, DIMY, DIMZ);
for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, 1,0.5,1.5); pa.setMaxspeed(3); //pa.wrapSpace(DIMX/2, DIMY/2, DIMZ/2); pa.bounceSpace(DIMX/2, DIMY/2, DIMZ/2);
int pop = 600; int DIMX = 600; int DIMY=600;int DIMZ=600; ArrayList <Ple_Agent>boids;
pa.update(); pa.updateTail(1); //display the tail interpolating 2 sets of values: //R,G,B,ALPHA,SIZE - R,G,B,ALPHA,SIZE pa.displayTailPoints(255, 0, 0, 0, 1, 0, 255, 0, 255, 1);
void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 600); initGUI();
stroke(0); strokeWeight(5); pa.displayPoint(); } gui();
boids = new ArrayList<Ple_Agent>(); for(int i=0; i<pop; i++){ Vec3D v = new Vec3D(0,0,0); 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(40); // the number are the length of the tail boids.add(pa); } controlP5.setAutoDraw(false); }
} void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; } void buildBox(float x, float y, float z){ noFill(); stroke(0,90); strokeWeight(1); pushMatrix(); scale(x,y,z); box(1); popMatrix(); }
Draw trail
import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core. import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3; PeasyCam cam; ControlP5 controlP5;
int pop = 600; int DIMX = 600; int DIMY=600;int DIMZ=600; ArrayList <Ple_Agent>boids; void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 600); initGUI(); boids = new ArrayList<Ple_Agent>(); for(int i=0; i<pop; i++){ Vec3D v = new Vec3D(0,0,0); 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(5); // the number are the length of the tail boids.add(pa); } controlP5.setAutoDraw(false); }
void draw(){ if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } background(235); noFill(); stroke(0,90); strokeWeight(1); box(DIMX, DIMY, DIMZ);
for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, 1,0.5,1.5); pa.setMaxspeed(3); pa.bounceSpace(DIMX/2, DIMY/2, DIMZ/2); pa.update(); pa.updateTail(1); pa.displayTailPoints(255, 0, 0, 0, 1, 0, 255, 0, 255, 1); pa.dropTrail(1,800); stroke(0,90); strokeWeight(1); pa.drawTrail(200); stroke(0); strokeWeight(5); pa.displayPoint(); } gui(); } void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; } void buildBox(float x, float y, float z){ noFill(); stroke(0,90); strokeWeight(1); pushMatrix(); scale(x,y,z); box(1); popMatrix(); }
Draw Lines Between
import toxi.math.conversion.*; import toxi.geom.*; import toxi.math.*; import toxi.geom.mesh2d.*; import toxi.util.datatypes.*; import toxi.util.events.*; import toxi.geom.mesh.subdiv.*; import toxi.geom.mesh.*; import toxi.math.waves.*; import toxi.util.*; import toxi.math.noise.*; import peasy.test.*; import peasy.org.apache.commons.math.*; import peasy.*; import peasy.org.apache.commons.math.geometry.*; import plethora.core. import processing.opengl.*; import controlP5.*; PMatrix3D currCameraMatrix; PGraphics3D g3; PeasyCam cam; ControlP5 controlP5;
int pop = 300; int DIMX = 600; int DIMY=600;int DIMZ=600; ArrayList <Ple_Agent>boids; void setup(){ size(800,800,OPENGL); g3 = (PGraphics3D)g; cam = new PeasyCam(this, 600); initGUI(); boids = new ArrayList<Ple_Agent>(); for(int i=0; i<pop; i++){ Vec3D v = new Vec3D(0,0,0); 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(5); // the number are the length of the tail boids.add(pa); } controlP5.setAutoDraw(false); }
void draw(){ if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } background(235); noFill(); stroke(0,90); strokeWeight(1); box(DIMX, DIMY, DIMZ);
for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, 1,0.5,1.5); pa.setMaxspeed(3); pa.bounceSpace(DIMX/2, DIMY/2, DIMZ/2); pa.update(); pa.updateTail(1); pa.displayTailPoints(255, 0, 0, 0, 1, 0, 255, 0, 255, 1); stroke(pa.loc.z,pa.loc.y,pa.loc.z,90); strokeWeight(1); pa.drawLinesInRange(boids,20,100); stroke(0); strokeWeight(5); pa.displayPoint(); } gui(); } void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; } void buildBox(float x, float y, float z){ noFill(); stroke(0,90); strokeWeight(1); pushMatrix(); scale(x,y,z); box(1); popMatrix(); }
Try to use the GUI to set up the options
……………. ……………………… ………………. ………….. boolean showSwarm = true; boolean showTails; boolean showTrails; boolean showLinesBetween; boolean run = true; …………….
……………….. if(run){ pa.update(); } if(showTails){ pa.updateTail(1); //display the tail interpolating 2 sets of values: //R,G,B,ALPHA,SIZE - R,G,B,ALPHA,SIZE pa.displayTailPoints(255, 0, 0, 0, 0.5, 0, 0, 255, 255, 0.5); } if(showTrails){ pa.dropTrail(5,800); stroke(255,90); strokeWeight(1); pa.drawTrail(200); }
void setup(){ … .. ….
if(showLinesBetween){ stroke(pa.loc.z,pa.loc.y,pa.loc.z,90); strokeWeight(1); pa.drawLinesInRange(boids,20,100); }
…. …. } ………….. …….. void draw(){ background(235); biuldBox(DIMX, DIMY, DIMZ); if (controlP5.window(this).isMouseOver()) { cam.setActive(false); } else { cam.setActive(true); } for(Ple_Agent pa : boids){ pa.flock(boids, 80,40,30, 1,0.5,1.5); pa.setMaxspeed(3); pa.bounceSpace(DIMX/2, DIMY/2, DIMZ/2);
if(showSwarm){ strokeWeight(5); stroke(pa.loc.z,pa.loc.y,pa.loc.z); pa.displayPoint(); } gui(); } void gui() { currCameraMatrix = new PMatrix3D(g3.camera); camera(); controlP5.draw(); g3.camera = currCameraMatrix; } …. …. ………….
stroke(pa.loc.x,pa.loc.y,pa.loc.z,90); strokeWeight(1); pa.drawLinesInRange(boids,20,100);