// Class that represents a bug. Has a position, velocity, and random color. class Cucaracha { // Costructor. Assigns position, velocity is 0, color is random Cucaracha(float x, float y) { _x = x; _y = y; _vx = 0; _vy = 0; _fx = 0; _fy = 0; _h = random(0,255); _s = 90; _b = 192; } // Draw the bug. void draw(PGraphics2D pg) { pg.colorMode(HSB); pg.fill(_h,_s,_b); float hw = 10; float dx = _vx; float dy = _vy; float l = sqrt(dx*dx+dy*dy); l = constrain(l, 1.e-8, l); dx = dx/l; dy = dy/l; pg.beginShape(TRIANGLES); pg.vertex(_x+hw*dx, _y+hw*dy); pg.vertex(_x-hw*dy/2,_y+hw*dx/2); pg.vertex(_x+hw*dy/2,_y-hw*dx/2); pg.endShape(); } // Initialize the force to 0. Call once at the start of each timestep. void start_step() { _fx = 0; _fy = 0; } // Add a gravitational force vector. Call once for each planet after // calling start_step. void add_force(float x, float y) { float dx = (_x - x) / 80; float dy = (_y - y) / 80; float del = dx*dx+dy*dy; float l = sqrt(del); // normalize dx = dx / l; dy = dy / l; float g = .1; float f = g * ( -(1.-del)*pow(2.71828,-.5*del) ); //float f = g / (l*l); f = -constrain(f, -1, .5 ); _fx = dx * f; _fy = dy * f; } // Add the force to the velocity. Then add the velocity to the position. // Call once after add all of the forces. void move() { _vx += _fx; _vy += _fy; _x += _vx; _y += _vy; float damp = .996; _vx *= damp; _vy *= damp; } float _x; float _y; float _vx; float _vy; float _fx; float _fy; float _h; float _s; float _b; }; Cucaracha[] bugs = new Cucaracha[75]; int num_bugs = bugs.length; boolean first_frame = true; // true means we have to clear boolean always_clear = false; // true means we should clear at the start of every frame PGraphics2D pg = null; // Create a random set of bugs void setup() { size(400, 400); pg = (PGraphics2D)createGraphics(400,400,P2D); for (int i=0; i