code studies --- 22 | 29
// Claus Rytter Bruun de Neergaard// www.clausclaus.com, March, 2013// Tested in Processing 2.0b8// AGENT BEHAVIOR, THREE-DIMENSIONAL WORLD, AFFECTING / CREATING GEOMETRYimport peasy.*;PeasyCam cam;int n = 100;int grayscale = 255;int subgrid = 50;float strokethick = 3.5;float speed = 10.0;float acceleration = 0.35;int world = 500;float noiseX = 100, noiseY = 110, noiseZ = 120;float nX = (world/2), nY = (world/2), nZ = -(world/2);float inc = 0.015;ArrayList agents = new ArrayList(n);void setup() {size(670, 450, P3D);background(120);// set camera from the beginningcam = new PeasyCam(this, 750);cam.setRotations(0.75, 0.65, -0.2);cam.lookAt(world/2, world/2, -world/2);// create number of starting agentsfor (int i = 0; i < n-1; i++) {agents.add(new Agent());}}void draw() {background(120);// update noise attractornoiseAtt();// call functions within each agentfor (int i = 0; i < agents.size(); i++) {Agent a = (Agent) agents.get(i);a.movement();a.display();a.edges();}// evaluate grid background, based on agent posgrid();}void noiseAtt() {// calc noise movementnX = noise(noiseX) * world;nY = noise(noiseY) * world;nZ = noise(noiseZ) * (-world);noiseX = noiseX + inc;noiseY = noiseY + inc;noiseZ = noiseZ + inc;loc.x = 0;}else if (loc.x < 0) {loc.x = world;}// y-directionif (loc.y > world) {loc.y = 0;}else if (loc.y < 0) {loc.y = world;}// z-directionif (loc.z > 0) {loc.z = -world;}else if (loc.z < -world) {loc.z = 0;}}}void grid() {noFill();stroke(255);strokeWeight(1);pushMatrix();translate(world/2, world/2, -(world/2));box(world);popMatrix();// check if agents inside subgridfor (int x = 0; x < world; x += subgrid) {for (int y = 0; y < world; y += subgrid) {for (int z = -world; z < 0; z += subgrid) {// for each subgrid field - loop through all agentsfor (int i = 0; i < agents.size(); i++) {Agent a = (Agent) agents.get(i);// if agent is inside subgrid, draw boxif ((a.loc.x > x) && (a.loc.x < x+subgrid) &&(a.loc.y > y) && (a.loc.y < y+subgrid) &&(a.loc.z < z) && (a.loc.z > z-subgrid)) {stroke(255, 35);noFill();// (a bit weird translate, since box is drawn at center)pushMatrix();translate(x+(subgrid/2), y+(subgrid/2), z-(subgrid/2));box(subgrid);popMatrix();}}}}}}// draw attractorstroke(255, 0, 0);strokeWeight(6.0);point(nX, nY, nZ);}class Agent {PVector loc;PVector vel;PVector acc;// constructorAgent() {loc = new PVector(random(0, world),random(0, world),random(-world, 0));vel = new PVector(0, 0, 0);}void movement() {// algorithm for calculating accelerationPVector att = new PVector(nX, nY, nZ);PVector dir = PVector.sub(att, loc);dir.normalize();dir.mult(acceleration);acc = dir;// find vector pointing// towards mouse// normalize// scale// set to acceleration// vel changes by acc; loc changes by velvel.add(acc);vel.limit(speed);loc.add(vel);}void display() {stroke(grayscale);strokeWeight(strokethick);point(loc.x, loc.y, loc.z);}void edges() {// x-directionif (loc.x > world) {code studies --- 23 | 29