import simpleML.*; // Modeling exchange rates // Zoe Fraade-Blanar (http://www.binaryspark.com) // Demonstrates attractive force one body exerts on a group of bodies stored in an array // G ---> universal gravitational constant // m1 --> mass of object #1 // m2 --> mass of object #2 // d ---> distance between objects // F = (G*m1*m2)/(d*d) //to do. Read in XML. make objects equal to how many //make size of object equal to inverse strength of each //make objects rotate //make dollar rotate //color objects? //make objects equal to symbols? //Make each object actually be an array of objects int MAX = 64; //that's how many currencies are on the list. Would be good if we had a way to make it dynamic Currency[] t = new Currency[MAX]; //create the array of 5 things (which are not yet substantiated) Dollar a; boolean showVectors = true; XMLRequest xmlRequest; //getting the information to initialize particles...with boolean bodiesReady = false; //we don't want to do any operations on bodies before they're ready! PFont font; PFont cFont; boolean showName; boolean showCrosshairs; boolean showMoney; boolean showSparkles; void setup() { size(800,500); background(255); smooth(); showName = false; showCrosshairs = false; showMoney = true; showSparkles = false; // Currency /*-- for (int i = 0; i < t.length; i++) { PVector ac = new PVector(0.0,0.0); //to start, each has o acc on thier x and y PVector ve = new PVector(random(-1,1),random(-1,1)); //but they start at a random constant velocity PVector lo = new PVector(random(width),random(height)); //at a random location t[i] = new Currency(ac,ve,lo,random(8,16)); //with a rando mass! } */ // Dollar a = new Dollar(new PVector(width/2,height/2),20,4); //a new attractor object with a pvector location, 20 mass, and .4 gravity. xmlRequest = new XMLRequest(this, "http://www.binaryspark.com/Academia/Nature/Midterm/financialGrab.php" ); //make the XML request xmlRequest.makeRequest(); font = loadFont("FrenchScriptMT-48.vlw"); cFont = loadFont("BitstreamVeraSans-Roman-10.vlw"); } void draw() { fill (255, 1); rect (0,0, width, height); if (bodiesReady == true){ //We only want to do operations on bodies if they exist! a.rollover(mouseX,mouseY); a.go(); for (int i = 0; i < t.length; i++) { // Calculate a force exerted by "Dollar" on "Currency" PVector f = a.calcGravForce(t[i]); // Apply that force to the thing t[i].applyForce(f); // Update and render t[i].go(); }//end for }//end if else { fill(95, 224, 57); textFont(font, 48); text("Loading Financial Data", 15, 50); } }//end draw void mousePressed() { a.clicked(mouseX,mouseY); } void mouseReleased() { a.stopDragging(); } void keyPressed() { if (bodiesReady == true) { //Only show em if you've got em! if (key == 'm') { showMoney = true; } if (key == 'v') { showVectors = !showVectors; }//end if v if (key == 'n') {//only if we have bodies, toggle the ability to see names showName = !showName; }//end if n if (key == 'c') { showCrosshairs = !showCrosshairs; showSparkles = false; //so we never have sparkles and crosshairs at the same time showMoney = false; //to make sure we never have the two showing at the same time }//end if c if (key == 's') { showSparkles = !showSparkles; showCrosshairs = false; showMoney = false; } //finally, make sure that something's always showing if((showSparkles == false) && (showCrosshairs == false)){ showMoney = true; } if (showMoney == true) { showSparkles = false; //to make sure we never have the two showing at the same time showCrosshairs = false; } }//end if bodies ready } // Renders a vector object 'v' as an arrow and a location 'loc' void drawVector(PVector v, PVector loc, float scayl) { if (v.mag() > 0.0) { pushMatrix(); float arrowsize = 4; // Translate to location to render vector translate(loc.x,loc.y); stroke(0); // 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.mag()*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 netEvent(XMLRequest ml) { // Retrieving an array of all XML elements inside"  title*  "tags String[] codes = ml.getElementArray( "code" ); String[] rates = ml.getElementArray( "rate" ); //for (int i = 0; i < codes.length; i++ ) { //for testing purposes only //println(codes[i]); //println(rates[i]); //} for (int i = 0; i < t.length; i++) { //Now, let's make some bodies! PVector ac = new PVector(0.0,0.0); //to start, each has o acc on thier x and y PVector ve = new PVector(random(-1,1),random(-1,1)); //but they start at a random constant velocity PVector lo = new PVector(random(width/2+width/4),random(height/2+height/4)); //at a random location //PVector lo = new PVector(0,0); //at a random location float currentRate = float(rates[i]); //get the rate for this article of currency float mass = constrain(40/currentRate, 1, 50); // invert so the less something is worth, the smaller it is, make sure it's never oto small or too big mass=map (mass, 1, 50, 40, 100); //so we get an even range String name = codes[i]; t[i] = new Currency(ac,ve,lo,mass, name); } fill (255); //wipe the screen blank to start why isn't this working? //rect (0,0, width, height); // background(255); bodiesReady = true; //we've got bodies, so we're ready to do something with them }