//ran tao //nature of code //week 3 //10.2.06 //adapted from dan shiffman's code // Choose one or more of the followingÉor create your own. // 1) Extend one of the examples into 3 dimensions. // 2) Examine the first example which implements a ÒdragÓ force. // Can you make a liquid class (moving the code out of the main program)? // Can you create a system of multiple objects moving through multiple ÒliquidsÓ? // 3) Can the concept of ÒforceÓ be used as a metaphor for some other situation? // What other forces are there in the digital worlds you create? // 4) Research and implement another type of force not covered above. // 5) Can you use the notion of gravity to create a ÒrepulsiveÓ force, rather than ÒattractiveÓ? // Taking the last example above, can you develop some tricks to keep the system stable enough stay within the realm of the window? import noc.*; int MAX = 200; Cells[] t = new Cells[MAX]; sickCell a; boolean showVectors = false; boolean sick = true; void setup() { size(800,100); framerate(30); background(100,0,0); colorMode(RGB,255,255,255,100); // Some random bodies for (int i = 0; i < t.length; i++) { Vector3D ac = new Vector3D(0.0,0.0); Vector3D ve = new Vector3D(random(-1,1),random(-1,1)); Vector3D lo = new Vector3D(random(width),random(height)); t[i] = new Cells(ac,ve,lo,random(4,8)); } // Create an attractive body a = new sickCell(new Vector3D(width/2,height/2),10,0.4); } void draw() { background(100,0,0); smooth(); a.rollover(mouseX,mouseY); for (int i = 0; i < t.length; i++) { // Calculate a force exerted by "sickCell" on "Cells" Vector3D f = a.calcGravForce(t[i]); // Apply that force to the Cells t[i].add_force(f); // check to see if it touches sick cell, if so turn color if ((t[i].getLoc().x < a.getLoc().x + a.getMass()*1.5) && (t[i].getLoc().x > a.getLoc().x - a.getMass()*1.5) && (t[i].getLoc().y < a.getLoc().y + a.getMass()) && (t[i].getLoc().y > a.getLoc().y - a.getMass())) { t[i].sc = true; } // Update and render t[i].go(); } a.go(); } void mousePressed() { a.clicked(mouseX,mouseY); } void mouseReleased() { a.stopDragging(); } void keyPressed() { if (keyCode == TAB) { showVectors = !showVectors; } if (keyCode == ENTER || keyCode == RETURN) { sick = !sick; } if (keyCode == BACKSPACE) { setup(); } } // 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(); } }