import controlP5.*; /*Firefly behavior simulated as Bouncing Balls based on 'Firefly-Inspired Sensor Network Synchronicity with Realistic Radio Effects ' Werner-Allen, et. al. and 'Synchronization of pulse-coupledbiological oscillators' R. Mirollo and S. Strogatz. Basically this models the synchrony of fireflies and visualizes the synchrony by modeling each firefly as a bouncing ball, with the firefly flashing represented by the ball hitting the ground. Simply put, all of the fireflies have the same period but are at different phases. If a firefly's neighbor flashes, it increments the time of the firefly to bring it closer to flashing. The increment function is created such that if a neighbor of i flashes closer to the end of i's period, the increment will be larger for firefly i. Likewise if the neighbor flashes close to the beginning of i's period the increment will be less. This behavior can be represented as a logarithmic function. We can approximate the increment function as delta_t = epsilon * t , where t is the time in a period. The reason for this is given in the first paper above on page 5. Initially the balls will make sharp jumps, that is because one of its neighbors hit the ground (thus flashing) and incrementing the original ball along its period. by Kawandeep Virdee */ float detection_radiusI=30; int number_firefliesI= 100; float periodI=200; int height = 500; //height of the bouncing int width = 500; float diameter = 10; //diameter of the circles representing the fireflies ArrayList fireflies; float period, detection_radius; float epsilon = .1; //to calculate the increment, an approximation float accConst; //the acceleration float initVelocity; //initial velocity int number_fireflies; boolean firstRun = true; color backgroundColor = color(0,0,0,10); void setup(){ if(firstRun){ setupGUI(); firstRun = false; } detection_radius=int(detection_radiusI); //how far does a firefly look to either side to see a neighbor flash number_fireflies=int(number_firefliesI); period = int(periodI); //how long the time inbetween flashes is, in this case the number of points to model the bouncing size(width,height); accConst = 2*(height-20)/(sq(period/2)); //equation to calculate the acceleration to go a certain height //from displacement = (1/2) a t^2 initVelocity = (accConst/2)*period; //at time = period the ball has fallen to its initial location, we can calculate the //initial velocity fireflies = new ArrayList(); int j=width/4; for(int i=0; i= period){ f.time=0; f.flash = true; f.position.y = heightCalc(f.time); f.drawFlash(); } else{ f.time +=1; f.flash = false; //check for neighbor flashing f.neighborsFlash(detection_radius); //increment if neighbor flashes f.time +=f.time_memory; f.time_memory=0; f.position.y = heightCalc(f.time); f.drawNormal(); } } } /// class Firefly { PVector position; float time; //current time level of the firefly float time_memory=0; //this will hold the delta t value float delta_t; //the change in t when a neighbor flashes boolean flash = false; //constructor Firefly(PVector _position, float _time){ position = _position; time = _time; } //check the neighbors ArrayList neighbors(float detection_radius){ ArrayList neighbors = new ArrayList(); for(int i = 0; i