Documentation000_upload
Transform your PDFs into Flipbooks and boost your revenue!
Leverage SEO-optimized Flipbooks, powerful backlinks, and multimedia content to professionally showcase your products and significantly increase your reach.
// Claus Rytter Bruun de Neergaard
// www.clausclaus.com, March, 2013
// Tested in Processing 2.0b8
// AGENT BEHAVIOR, THREE-DIMENSIONAL WORLD, AFFECTING / CREATING GEOMETRY
import 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 beginning
cam = new PeasyCam(this, 750);
cam.setRotations(0.75, 0.65, -0.2);
cam.lookAt(world/2, world/2, -world/2);
// create number of starting agents
for (int i = 0; i < n-1; i++) {
agents.add(new Agent());
}
}
void draw() {
background(120);
// update noise attractor
noiseAtt();
// call functions within each agent
for (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 pos
grid();
}
void noiseAtt() {
// calc noise movement
nX = 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-direction
if (loc.y > world) {
loc.y = 0;
}
else if (loc.y < 0) {
loc.y = world;
}
// z-direction
if (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 subgrid
for (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 agents
for (int i = 0; i < agents.size(); i++) {
Agent a = (Agent) agents.get(i);
// if agent is inside subgrid, draw box
if ((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 attractor
stroke(255, 0, 0);
strokeWeight(6.0);
point(nX, nY, nZ);
}
class Agent {
PVector loc;
PVector vel;
PVector acc;
// constructor
Agent() {
loc = new PVector(random(0, world),
random(0, world),
random(-world, 0));
vel = new PVector(0, 0, 0);
}
void movement() {
// algorithm for calculating acceleration
PVector 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 vel
vel.add(acc);
vel.limit(speed);
loc.add(vel);
}
void display() {
stroke(grayscale);
strokeWeight(strokethick);
point(loc.x, loc.y, loc.z);
}
void edges() {
// x-direction
if (loc.x > world) {
code studies --- 23 | 29