• Show Sketch
/** @peep sketchcode **/
int NUM_CIRCLES = 15;
float MIN_RADIUS = random(10,50);
float MAX_RADIUS = random(60,100);
 
float DELTA_ANGLE = TWO_PI/36;
 
Circle[] circles;
 
void setup() {
  size(300, 300);
  frameRate(10);
  smooth();
  circles = new Circle[NUM_CIRCLES];
  for (int i = 0; i < circles.length; i++) {
    circles[i] = new Circle(random(width), random(height), random(MIN_RADIUS, MAX_RADIUS));
  }
}
 
void update() {
  for (int i = 0; i < circles.length; i++) {
    circles[i].update();
  }
}
 
void draw() {
  update();
  background(255);
  for (int i = 0; i < circles.length; i++) {
    circles[i].draw();
  }
}
 
class Circle {
  float x;
  float y;
  float radius;
 
  float heading;
  float speed;
 
  Circle parent;
 
  Circle(Circle _parent, float _radius) {
    this(_parent.x, _parent.y, _radius);
    parent = _parent;
  }
 
  Circle(float _x, float _y, float _radius) {
    x = _x;
    y = _y;
    radius = _radius;
    heading = random(TWO_PI);
    speed = 1;
    parent = null;
  }
 
  void respawn() {
    if (parent != null) {
      x = parent.x;
      y = parent.y;
    }
  }
 
  void draw() {
    pushStyle();
    noFill();
    stroke(0);
    strokeWeight(1);
    ellipseMode(RADIUS);
 
    pushMatrix();
    translate(x, y);
    rotate(heading);
    strokeWeight(0.01);
    ellipse(0, 0, radius, radius);
 
    popMatrix();
 
    stroke(192, 0, 0, 64);
    for (int i = 0; i < circles.length; i++) {
      if (circles[i] != this) {
        Circle other = circles[i];
        if (touching(other)) {
          line(x, y, other.x, other.y);
        }
      }
    }
    popStyle();
  }
 
  void update() {
    behaviour1();
    behaviour5();
  }
 
  void behaviour1() {
    // Constant linear motion
    float dx = speed * cos(heading);
    float dy = speed * sin(heading);
    x += dx;
    y += dy;
  }
 
  void behaviour2() {
    // Constrain to surface
    if (x < radius) x = radius;
    if (y < radius) y = radius;
    if (x > width - radius) x = width - radius;
    if (y > height - radius) y = height - radius;
  }
 
  void behaviour3() {
    // While touching another, change direction
    for (int i = 0; i < circles.length; i++) {
      if (circles[i] != this) {
        if (touching(circles[i])) {
          heading += random(-DELTA_ANGLE, DELTA_ANGLE);
        }
      }
    }
  }
 
  void behaviour4() {
    // While touching another, move away from its centre
    for (int i = 0; i < circles; i++) {
      if (circles[i] != this) {
        if (touching(circles[i])) {
          Circle other = circles[i];
          float d = distance(other);
          float dx = (other.x - x)/d;
          float dy = (other.y - y)/d;
          x -= speed * dx;
          y -= speed * dy;
        }
      }
    }
  }
 
  void behaviour5() {
    x = (x + width) % width;
    y = (y + height) % height;
  }
 
  void touching(Circle other) {
    return (distance(other) < radius + other.radius);
  }
 
  float distance(Circle other) {
    return dist(x, y, other.x, other.y);
  }
}

Comments

Nobody has said anything yet.