‹ eco

Trails

// redFrik

//click to explode

int maxNumAgents= 50;
int numAgents= 50;
Agent[] agents= new Agent[maxNumAgents];
float rotX= 0, rotY= 0, rotZ= 0, rx, ry, rz;
int tx, ty, tz;
float sx, sy, sz, sxDrift, syDrift, szDrift;

void setup() {
  size(640, 480, P3D);
  frameRate(30);
  background(150, 150, 120);
  initAgents();
  newTrans();
  newScale();
  newRotation();
}
void draw() {
  translate(width/2+tx, height/2+ty, tz);
  sx= sx+sxDrift;
  sy= sy+syDrift;
  sz= sz+szDrift;
  scale(sx, sy, sz);
  rotX= rotX+radians(rx); 
  rotateX(rotX);
  rotY= rotY+radians(ry); 
  rotateY(rotY);
  rotZ= rotZ+radians(rz); 
  rotateZ(rotZ);
  for (int i= 0; i<numAgents; i++) {
    agents[i].move();
    agents[i].draw();
  }
}
void mousePressed() {
  background(150, 150, 120);
  initAgents();
  newTrans();
  newScale();
  newRotation();
}
void initAgents() {
  AWorld world= new AWorld(3, 400, 400, 400, 0.95);
  for (int i= 0; i<numAgents; i++) {
    ALocation loc= new ALocation(world, random(-0.1, 0.1), random(-0.1, 0.1), random(-0.1, 0.1));
    ADirection dir= new ADirection(random(-0.05, 0.05), random(-0.05, 0.05), random(-0.05, 0.05));
    agents[i]= new Agent3D(loc, dir, random(1.0), color(255, 0, 0), color(0), 5);
  }
}
void newTrans() {
  tx= int(random(-width/3, width/3));
  ty= int(random(-height/3, height/3));
  tz= 0;
}
void newScale() {
  if (random(0, 10)>7) {
    sx= random(0.6, 1.2);
    sy= random(0.6, 1.2);
    sz= random(0.1, 2);
    if (random(0, 10)>8) {
      sxDrift= random(-0.1, 0.1);
    } else {
      sxDrift= 0;
    }
    if (random(0, 10)>8) {
      syDrift= random(-0.1, 0.1);
    } else {
      syDrift= 0;
    }
    if (random(0, 10)>8) {
      szDrift= random(-0.1, 0.1);
    } else {
      szDrift= 0;
    }
  } else {
    sxDrift= 0;
    syDrift= 0;
    szDrift= 0;
    if (random(0, 10)>5) {
      sx= random(0.3, 0.7);
      sy= random(0.2, 0.6);
      sz= random(0.01, 0.25);
    } else {
      sx= 1;
      sy= 1;
      sz= 1;
    }
  }
}
void newRotation() {
  rx= ry= rz= 0;
  rotX= rotY= rotZ= 0;
  if (random(0, 10)>7) {
    ry= random(-4, 4);
  }
  if (random(0, 10)>7) {
    rz= random(-4, 4);
  }
  if (random(0, 10)>4) {
    rx= random(-4, 4);
  }
  if (rx+ry+rz==0) {        //make sure always some rotation
    rx= random(0.5, 3);
  }
}

// redFri - version 060225

class ALocation {
  AWorld world;
  float x, y, z;
  public ALocation(AWorld _world, float _x, float _y, float _z) {
    world= _world;
    x= _x;
    y= _y;
    z= _z;
  }
  float distance(ALocation other) {
    return abs(sqrt(pow(other.x-x, 2)+pow(other.y-y, 2)+pow(other.z-z, 2)));
  }
  void add(ADirection dir) {
    x= x+dir.vx;
    y= y+dir.vy;
    z= z+dir.vz;
  }
}
class ADirection {
  float vx, vy, vz;
  public ADirection(float _vx, float _vy, float _vz) {
    vx= _vx;
    vy= _vy;
    vz= _vz;
  }
  void mul(float _const) {
    vx= vx*_const;
    vy= vy*_const;
    vz= vz*_const;
  }
}
class AWorld {
  Agent[] agents;
  int dim, w, h, d;
  float damp;
  public AWorld(int _dim, int _w, int _h, int _d, float _damp) {
    dim= _dim;
    w= _w;
    h= _h;
    d= _d;
    damp= _damp;
  }
}

//--
abstract class Agent {
  ALocation loc;
  ADirection dir;
  float energy;
  color col, borderCol;
  int agentSize;
  public Agent(ALocation _loc, ADirection _dir, float _energy, color _col, color _borderCol, int _agentSize) {
    loc= _loc;
    dir= _dir;
    energy= _energy;
    col= _col;
    borderCol= _borderCol;
    agentSize= _agentSize;
  }
  void draw() {
    stroke(borderCol);
    fill(col);
    drawShape();
  }
  void drawShape() {
  }  //to override
  void move() {
    loc.add(dir);
    dir.mul(loc.world.damp);
  }
}

//--
class Agent1D extends Agent {
  public Agent1D(ALocation _loc, ADirection _dir, float _energy, color _col, color _borderCol, int _agentSize) {
    super(_loc, _dir, _energy, _col, _borderCol, _agentSize);
  }
  void drawShape() {
    point(loc.x*width, height/2);  //agentSize???
  }
}
class Agent2D extends Agent {
  public Agent2D(ALocation _loc, ADirection _dir, float _energy, color _col, color _borderCol, int _agentSize) {
    super(_loc, _dir, _energy, _col, _borderCol, _agentSize);
  }
  void drawShape() {
    rect(loc.x*width, loc.y*height, agentSize, agentSize);
  }
}
class Agent3D extends Agent {
  public Agent3D(ALocation _loc, ADirection _dir, float _energy, color _col, color _borderCol, int _agentSize) {
    super(_loc, _dir, _energy, _col, _borderCol, _agentSize);
  }
  void drawShape() {
    pushMatrix();
    translate(loc.x*width, loc.y*height, loc.z*100);
    box(agentSize, agentSize, agentSize);
    popMatrix();
  }
}