import java.util.ArrayList; import processing.core.PApplet; import processing.opengl.*; public class Test3D extends PApplet { // Declare a "Bling" object ArrayList blings; float rotation; boolean showVectors = false; int numBlings = 70; float g = 4f; float defense = 18f; float attack = 2.5f; public void setup() { size(800,600, P3D);//download & change P3D to OPENGL for better performance framerate(30); //smooth(); lights(); // Create the thing objects blings = new ArrayList(); for(int i = 0; i < numBlings; i++) { blings.add(new Bling(new Vector3D(random(-200,200),random(-200,200),random(-200,200)),new Vector3D(1.5f,2f,1f), new Vector3D(0f,0f,0f),i%2,this)); } } public void draw() { lights(); background(100); pushMatrix(); translate(width/2,height/2,200); //rotateX(rotation*2.5f); rotateY(rotation); //rotateZ(rotation*0.5f); stroke(70); noFill(); fill(200,200,200,50); //box(316); sphereDetail(30); sphere(316); sphereDetail(6); updateObjects(); popMatrix(); rotation +=.02; } private void updateObjects() { runPhysics(); for (int i = 0; i < blings.size(); i++) { Bling z = (Bling) blings.get(i); z.go(); if(z.mass < 0.1f) { blings.remove(i); i--; } } } private void runPhysics() { for(int i = 0; i < blings.size(); i++) { Bling bi = (Bling)blings.get(i); bi.acc = new Vector3D(0,0,0); for(int j = 0; j < blings.size(); j++) { if(i != j) { Bling bj = (Bling)blings.get(j); float dist = Vector3D.distance(bi.loc,bj.loc); // carnivore if(dist < (pow(bi.mass,.33333f) + pow(bj.mass,.333333f)) / 1 && bi.type != bj.type) { float totalMass = 0.0f; for(int count = 0; count < blings.size(); count++) { Bling bc = (Bling)blings.get(count); totalMass += bc.mass; } println("numBlings = " + blings.size() + " Avg. mass = " + totalMass / (float)blings.size()); boolean jWins = false; float jThresh = bi.mass / (bi.mass+bj.mass); if(random(1) > jThresh) { jWins = true; } if(jWins) { //bj.mass = pow(pow(bi.mass,3) + pow(bj.mass,3), .333333f); bj.mass += bi.mass; bj.shrinkCounter = 0; blings.remove(i); i--; break; } else { //bi.mass = pow(pow(bi.mass,3) + pow(bj.mass,3), .333333f); bi.mass += bj.mass; bi.shrinkCounter = 0; blings.remove(j); if(j >= blings.size()) { break; } bj = (Bling)blings.get(j); dist = Vector3D.distance(bi.loc,bj.loc); } }// end carnivore dist *= dist; if(bi.type != bj.type) { if(bi.mass <= bj.mass) { bi.acc.sub(Vector3D.div(Vector3D.mult(Vector3D.sub(bj.loc,bi.loc),abs((/*bi.mass-*/bj.mass))*g*(bi.mass*defense/(bi.shrinkInterval-bi.shrinkCounter+1))),dist)); } else { bi.acc.add(Vector3D.div(Vector3D.mult(Vector3D.sub(bj.loc,bi.loc),abs((/*bi.mass-*/bj.mass))*g*(bi.mass *attack / (bi.shrinkInterval/(bi.shrinkCounter+1)))),dist)); } } else { if(bi.mass <= bj.mass) { bi.acc.add(Vector3D.div(Vector3D.mult(Vector3D.sub(bj.loc,bi.loc),abs((/*bi.mass-*/bj.mass))*g*(bi.mass *defense/(bi.shrinkInterval-bi.shrinkCounter+1))),dist)); } else { //bi.acc.sub(Vector3D.div(Vector3D.mult(Vector3D.sub(bj.loc,bi.loc),/*bi.mass**/bj.mass*g),dist)); } } bi.acc.limit(10f); } } } } // Renders a vector object 'v' as an arrow and a location 'loc' public void drawVector(Vector3D v, Vector3D loc, float scayl) { pushMatrix(); float arrowsize = 4; // Translate to location to render vector translate(loc.x,loc.y,loc.z); 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(); } public void keyPressed() { if (key == 'r' || key == 'R') { blings.add(new Bling(new Vector3D(random(400)-200, random(400)-200, random(400)-200), new Vector3D( 15f, 2f, 1f), new Vector3D(0f, 0f, 0f), 0, this)); } else if (key == 'b' || key == 'B') { blings.add(new Bling(new Vector3D(random(400)-200, random(400)-200, random(400)-200), new Vector3D( -15f, 2f, 1f), new Vector3D(0f, 0f, 0f), 1, this)); } } public void mousePressed() { setup(); } }