//Fish and Flys // Based on Mutual Attraction by Daniel Shiffman // The Nature of Code, Spring 2007 // Demonstrates gravitational pull amongst a group of moving objects // G ---> universal gravitational constant // m1 --> mass of object #1 // m2 --> mass of object #2 // d ---> distance between objects // F = (G*m1*m2)/(d*d) //import noc.*; //Perlin noise variables float xoff = 0.0; float yoff = 0.0; float xincrement = 0.05; float yincrement = 0.01; float px; float py; PFont font; int maxThings = 20; int maxFood = 50; float foodInc = 1; float foodDec = -0.00009; int maxAttractors = 10; int trailAmount = 10; int aNum = 4; int pNum = 0; int aMouse; float nx; float ny; boolean showVectors = false; boolean started = false; boolean show = true; ParticleSystem ps; ParticleSystemFood psF; int numDead = 0; int foodTimer = 0; void setup() { font = loadFont("Tahoma-12.vlw"); textFont(font, 12); size(640,480); //size(screen.width,screen.height); smooth(); colorMode(RGB,255,255,255,100); ps = new ParticleSystem(); psF = new ParticleSystemFood(); } void draw() { fill(0,trailAmount); rect(0,0,width,height); ps.run(); psF.run(); if (foodTimer%100 == 0){ psF.addParticle(1); } foodTimer++; xoff += xincrement; yoff += yincrement; stroke(0); fill(255); text((ps.total() - numDead) + " Fish " + psF.total() + " Flies M + Fish | N - Fish | B + Flies | V - Flies | R RESET", 10,height-10); //line(0,height/2,width,height/2); } void mousePressed() { } void mouseReleased() { //reset(); } void keyPressed() { // showVectors = !showVectors; if (key == 'r' || key == 'R'){ started = false; aNum = 1; pNum = 0; reset(); } else if(key == 's' || key == 'S'){ showVectors = !showVectors; } else if (key == 'm'|| key == 'M'){ ps.addParticle(); } else if (key == 'b'|| key == 'B'){ psF.addParticle(1); } else if (key == 'n'|| key == 'N'){ ps.delParticle(0); } else if (key == 'v'|| key == 'V'){ psF.remParticle(0); } else if (pNum < aNum){ pNum = aNum; aNum++; if (aNum == maxAttractors){ aNum--; pNum = maxAttractors; } } else{ aNum--; if(aNum == 0 ){ pNum = 0; aNum++; } } } // Renders a vector object 'v' as an arrow and a location 'loc' void drawVector(Vector3D v, Vector3D loc, float scayl) { if (v.magnitude() > 0.0) { pushMatrix(); float arrowsize = 4; // Translate to location to render vector translate(loc.x,loc.y); stroke(255); // Call vector heading function to get direction (note that pointing up is a heading of 0) and rotate rotate(v.heading2D()); // Calculate length of vector & scale it to be bigger or smaller if necessary float len = v.magnitude()*scayl; // Draw three lines to make an arrow (draw pointing up since we've rotate to the proper direction) line(0,0,len,0); line(len,0,len-arrowsize,+arrowsize/2); line(len,0,len-arrowsize,-arrowsize/2); popMatrix(); } } void reset(){ for (int i = 0; i < ps.total(); i++){ ps.delAllParticles(i); } for (int i = 0; i < psF.total(); i++){ psF.delParticle(i); } ps = new ParticleSystem(); psF = new ParticleSystemFood(); }