Skip to content
Simulate oscillator synchronization with Processing
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
licenses
resources
src/sync
web
.classpath
.gitignore
.project
LICENSE
README.md

README.md

processing-synchronization

Simulate oscillator synchronization with Processing

  • Implements the Kuramoto model for synchronization.
  • Supports oscillator networks with arbitrary sizes, levels of coupling, and time steps.
  • Includes Perlin noise.
  • Still early days. Things will break.

Example

The following example simulates the swarming behavior and bioluminescence of fireflies. Pairs well with Owl City

Visualization gratefully borrowed from Daniel Shiffman's Flocking example.

import sync.*;

PNetwork net;
Swarm swarm;

void setup() {
  size(640, 360);
  int networkSize = 150;
  float coupling = 2.5;
  float stepSize = 0.05;
  float noiseLevel = 0.5;
  net = new PNetwork(this, networkSize, coupling, stepSize, noiseLevel);
  swarm = new Swarm(net);
  for (int i = 0; i < networkSize; i++) {
    swarm.addFly(new Firefly(width/2, height/2));
  }
}

void draw() {
  background(0, 5, 20);
  swarm.run();
}


// The Swarm (a list of Firefly objects)

class Swarm {
  ArrayList<Firefly> flies; // An ArrayList for all the fireflies
  PNetwork net; // A PNetwork of coupled oscillators for managing brightness

  Swarm(PNetwork _net) {
    flies = new ArrayList<Firefly>(); // Initialize the ArrayList
    net = _net;
  }

  void run() {
    for (int i = 0; i < flies.size(); i++) {
      flies.get(i).run(flies, net.phase[i]);  // Passing the entire list of fireflies to each firefly individually
    }
    net.step();
  }

  void addFly(Firefly f) {
    flies.add(f);
  }
}


// The Firefly class

class Firefly {

  PVector position;
  PVector velocity;
  PVector acceleration;
  float r;
  float maxforce;    // Maximum steering force
  float maxspeed;    // Maximum speed

    Firefly(float x, float y) {
    acceleration = new PVector(0, 0);
    velocity = PVector.random2D();
    position = new PVector(x, y);
    r = 2.0;
    maxspeed = 2;
    maxforce = 0.03;
  }

  void run(ArrayList<Firefly> flies, float phase) {
    swarm(flies);
    update();
    borders();
    render(phase);
  }

  void applyForce(PVector force) {
    // We could add mass here if we want A = F / M
    acceleration.add(force);
  }

  // We accumulate a new acceleration each time based on three rules
  void swarm(ArrayList<Firefly> flies) {
    PVector sep = separate(flies);   // Separation
    PVector ali = align(flies);      // Alignment
    PVector coh = cohesion(flies);   // Cohesion
    // Arbitrarily weight these forces
    sep.mult(1.5);
    ali.mult(1.0);
    coh.mult(1.0);
    // Add the force vectors to acceleration
    applyForce(sep);
    applyForce(ali);
    applyForce(coh);
  }

  // Method to update position
  void update() {
    // Update velocity
    velocity.add(acceleration);
    // Limit speed
    velocity.limit(maxspeed);
    position.add(velocity);
    // Reset accelertion to 0 each cycle
    acceleration.mult(0);
  }

  // A method that calculates and applies a steering force towards a target
  // STEER = DESIRED MINUS VELOCITY
  PVector seek(PVector target) {
    PVector desired = PVector.sub(target, position);  // A vector pointing from the position to the target
    // Steering = Desired minus Velocity
    desired.setMag(maxspeed);
    PVector steer = PVector.sub(desired, velocity);
    steer.limit(maxforce);  // Limit to maximum steering force
    return steer;
  }

  void render(float phase) {
    // Draw a triangle rotated in the direction of velocity
    float theta = velocity.heading() + radians(90);
    float alpha = 175*round(map(phase, 0, TWO_PI, 0, 1));
    fill(225, 240, 45, alpha);
    stroke(225, 240, 45, alpha);
    pushMatrix();
    translate(position.x, position.y);
    rotate(theta);
    beginShape(TRIANGLES);
    vertex(0, -r*2);
    vertex(-r, r*2);
    vertex(r, r*2);
    endShape();
    popMatrix();
  }

  // Wraparound
  void borders() {
    if (position.x < -r) position.x = width+r;
    if (position.y < -r) position.y = height+r;
    if (position.x > width+r) position.x = -r;
    if (position.y > height+r) position.y = -r;
  }

  // Separation
  // Method checks for nearby fireflies and steers away
  PVector separate (ArrayList<Firefly> flies) {
    float desiredseparation = 25.0f;
    PVector steer = new PVector(0, 0, 0);
    int count = 0;
    // For every firefly in the system, check if it's too close
    for (Firefly other : flies) {
      float d = PVector.dist(position, other.position);
      // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
      if ((d > 0) && (d < desiredseparation)) {
        // Calculate vector pointing away from neighbor
        PVector diff = PVector.sub(position, other.position);
        diff.normalize();
        diff.div(d);        // Weight by distance
        steer.add(diff);
        count++;            // Keep track of how many
      }
    }
    // Average -- divide by how many
    if (count > 0) {
      steer.div((float)count);
    }

    // As long as the vector is greater than 0
    if (steer.mag() > 0) {
      // Implement Reynolds: Steering = Desired - Velocity
      steer.setMag(maxspeed);
      steer.sub(velocity);
      steer.limit(maxforce);
    }
    return steer;
  }

  // Alignment
  // For every nearby firefly in the system, calculate the average velocity
  PVector align (ArrayList<Firefly> flies) {
    float neighbordist = 50;
    PVector sum = new PVector(0, 0);
    int count = 0;
    for (Firefly other : flies) {
      float d = PVector.dist(position, other.position);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.velocity);
        count++;
      }
    }
    if (count > 0) {
      sum.div((float)count);
      // Implement Reynolds: Steering = Desired - Velocity
      sum.setMag(maxspeed);
      PVector steer = PVector.sub(sum, velocity);
      steer.limit(maxforce);
      return steer;
    }
    else {
      return new PVector(0, 0);
    }
  }

  // Cohesion
  // For the average position (i.e. center) of all nearby fireflies, calculate steering vector towards that position
  PVector cohesion (ArrayList<Firefly> flies) {
    float neighbordist = 50;
    PVector sum = new PVector(0, 0);   // Start with empty vector to accumulate all positions
    int count = 0;
    for (Firefly other : flies) {
      float d = PVector.dist(position, other.position);
      if ((d > 0) && (d < neighbordist)) {
        sum.add(other.position); // Add position
        count++;
      }
    }
    if (count > 0) {
      sum.div(count);
      return seek(sum);  // Steer towards the position
    }
    else {
      return new PVector(0, 0);
    }
  }
}

Creative Commons License
This example is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

You can’t perform that action at this time.