smrchrdsn:

Gravitational Attractors (Version 3)

Attraction is now based on the inverse square of the particle’s distance to each attractor and works across three dimensions (x, y, z).

// Gravitational attractors in three dimensions

// X and Y correspond to X and Y axes on screen

// Z (depth) is shown through size and opacity of particle

Pea[] peas = new Pea[750];

ArrayList nodes;

boolean isFading = false;

public int bgColor = 255;

void setup() {

  size(640, 480);

  smooth();

  background(255);

  for (int i = 0; i < peas.length; i++) {

    peas[i] = new Pea();

  }

  nodes = new ArrayList();

}

void draw() {

  if (isFading) fade();

  else background(bgColor);

  for (int p = 0; p < peas.length; p++) {

    for (int n = 0; n < nodes.size(); n++) {

      Node node = (Node) nodes.get(n);

      peas[p].seek(node.pos());

    }

    if (nodes.size() > 0) {

      peas[p].move(nodes.size());

    }

    peas[p].display();

  }

  for (int i = 0; i < nodes.size(); i++) {

    Node node = (Node) nodes.get(i);

    node.display();

  }

  //fill(0);

  //rect(peas[0].loc().x, peas[0].loc().y, 10, 10);

  //saveFrame(“frames/####.png”);

}

// Key events

void keyPressed() {

  if (key == ’ ‘) isFading = !isFading;

  else if (key > 48 && key < 58) newAcc();

  else if (key == ‘b’) newBackground();

  //else saveFrame(“frame_####.png”);

}

void fade() {

  noStroke();

  fill(bgColor, 20);

  rect(0, 0, width, height);

}

void newAcc() {

  for (int p = 0; p < peas.length; p++) {

    peas[p].accMultEquals((key – 47) * 0.5);

  }

}

void newBackground() {

  if (bgColor == 255) bgColor = 0;

  else bgColor = 255;

}

// Mouse events

void mouseClicked() {

  for (int i = 0; i < nodes.size(); i++) {

    Node node = (Node) nodes.get(i);

    if (node.isUnderMouse(mouseX, mouseY)) {

      nodes.remove(i);

      return;

    }

  }

  nodes.add(new Node(mouseX, mouseY, random(-20, 20))); 

}

void mouseWheel(MouseEvent event) {

  for (int i = 0; i < nodes.size(); i++) {

    Node node = (Node) nodes.get(i);

    if (node.isUnderMouse(mouseX, mouseY)) {

      node.newZ(event.getAmount());

      return;

    }

  }

}

class Node {

  PVector loc;

  float zLimit = 50;

  float diameter;

  float r, g, b, a;

  Node(float _x, float _y, float _z) {

    loc = new PVector(_x, _y, _z);

    diameter = map(loc.z, -zLimit, zLimit, 60, 8);

    r = random(50, 150);

    g = random(0, 100);

    b = random(100, 250);

    a = 10;

  }

  PVector pos() {

    return loc;

  }

  boolean isUnderMouse(float _x, float _y) {

    if (dist(_x, _y, loc.x, loc.y) <= diameter * 0.5) return true;

    else return false;

  }

  void newZ(float _w) {

    loc.z += _w;

    loc.z = constrain(loc.z, -zLimit, zLimit);

    diameter = map(loc.z, -zLimit, zLimit, 50, 15);

  }

  void display() {

    fill(r, g, b, a);

    if (bgColor == 255) stroke(50, 100);

    else stroke(205, 100);

    strokeWeight(map(loc.z, -zLimit, zLimit, 5, 0.5));

    ellipse(loc.x, loc.y, diameter, diameter);

  }

}

class Pea {

  PVector loc, ploc, pploc, vel, acc;

  float accMult = 0.1;

  float velLimit = 10;

  float zLimit = 200;

  float r, g, b, a;

  Pea() {

    loc = new PVector(random(width), random(height), random(-50, 50));

    ploc = new PVector(loc.x, loc.y, loc.x);

    pploc = new PVector(loc.x, loc.y, loc.x);

    vel = new PVector(0, 0, 0);

    acc = new PVector(0, 0, 0);

    r = random(100, 250);

    g = random(50, 150);

    b = random(0, 100);

    a = map(loc.z, 0, 200, 220, 0);

  }

  void seek(PVector _n) {

    PVector n = PVector.sub(_n, loc);

    n.div(sq(0.01 * _n.dist(loc)));

    acc = PVector.add(n, acc);

  }

  void move(float _i) {

    acc.div(_i);

    acc.normalize();

    acc.mult(accMult);

    vel.add(acc);

    vel.limit(velLimit);

    loc.add(vel);

    loc.z = constrain(loc.z, -zLimit, zLimit);

  }

  void accMultEquals(float _m) {

    accMult = _m * 0.1;

  }

  void display() {

    a = map(loc.z, -zLimit, zLimit, 255, 1);

    stroke(r, g, b, a);

    float weight = map(loc.z, -zLimit, zLimit, 6, 0.1);

    strokeWeight(weight);

    line(loc.x, loc.y, pploc.x, pploc.y);

    pploc.set(ploc);

    ploc.set(loc);

  }

  PVector loc() {

    return loc;

  }

}