public void drawOrbits(ArrayList alObjectsArchive) { // println("SIZE:" + alObjectsArchive.size()); // ArrayList alObjectsArchive = timeline.getObjectStateArchive(); ArrayList alPrevPos = new ArrayList(); ArrayList alColors = new ArrayList(); alColors.add(color(255, 0, 0)); alColors.add(color(255, 255, 0)); alColors.add(color(255, 0, 255)); // for (int i = timeline.getTimeIdx(); i >= 0 && i > (timeline.getTimeIdx() - 1 - 100); i--) for (int i = 0; i < alObjectsArchive.size(); i++) { ArrayList objects = (ArrayList) alObjectsArchive.get(i); for (int j = 0; j < objects.size(); j++) { CelestialObject obj = (CelestialObject) objects.get(j); // CelestialObject obj = (CelestialObject)objects.get(1); PVector pos = obj.getPosition(); // stroke(0, 0, 255); stroke((Integer) alColors.get(j)); if (alPrevPos.size() == objects.size()) { PVector prevPos = (PVector) alPrevPos.get(j); line(prevPos.x, prevPos.y, pos.x, pos.y); alPrevPos.set(j, pos); } else alPrevPos.add(pos); } } }
public void draw() { background(255); // Turn off highlighting for all obstalces for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.highlight(false); } // Act on all boids for (int i = 0; i < boids.size(); i++) { Boid b = (Boid) boids.get(i); b.avoid(obstacles); b.run(); } // Display the obstacles for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); o.display(); } // Instructions textFont(f); fill(0); text( "Hit space bar to toggle debugging lines.\nClick the mouse to generate a new boids.", 10, height - 30); }
// Does a deepcopy of an array list public static ArrayList cloneArrayList(ArrayList al) { ArrayList alNew = new ArrayList(al.size()); for (int i = 0; i < al.size(); i++) { alNew.add(((CelestialObject) al.get(i)).clone()); } return alNew; }
// Does a deepcopy of an array list public ArrayList cloneArrayList(ArrayList al) { ArrayList alNew = new ArrayList(al.size()); for (int i = 0; i < al.size(); i++) { PVector pv = (PVector) al.get(i); alNew.add(new PVector(pv.x, pv.y)); } return alNew; }
/** Update all of the Ball's and draw them */ public void update() { if (balls.size() != 0) { for (int i = 0; i < balls.size(); i++) { Ball b = (Ball) balls.get(i); b.update(); b.attract = kelly; b.drawBall(); } } }
public void calculateForces(ArrayList objects) { for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); ArrayList forces = obj.getForces(); float totalForceX = 0; float totalForceY = 0; for (int j = 0; j < forces.size(); j++) { totalForceX += ((PVector) forces.get(j)).x; totalForceY += ((PVector) forces.get(j)).y; } PVector newAccel = new PVector(totalForceX / obj.getMass(), totalForceY / obj.getMass()); obj.setAcceleration(newAccel); } for (int i = 0; i < objects.size(); i++) { CelestialObject obj1 = (CelestialObject) objects.get(i); float forceX = 0; float forceY = 0; obj1.clearForces(); if (obj1.getClass() == Star.class) { println(obj1.getVelocity()); continue; } for (int j = 0; j < objects.size(); j++) { CelestialObject obj2 = (CelestialObject) objects.get(j); if (i == j) continue; PVector pvDistance = PVector.sub(obj2.getPosition(), obj1.getPosition()); // println("distance: x:" + pvDistance.x + " y:" + pvDistance.y); float distance = sqrt(sq(pvDistance.y) + sq(pvDistance.x)); float angle = degrees(atan2(pvDistance.y, pvDistance.x)); float force = (G * obj1.getMass() * obj2.getMass()) / sq(distance); forceX = force * cos(radians(angle)); forceY = force * sin(radians(angle)); // println("FORCES on " + obj1.getName() + ":" + forceX + "," + forceY); obj1.addForce(new PVector(forceX, forceY)); println(); } } }
public void trace(float x, float y) { println(x); if (frameCounter > 2 && mousePressed) { coords.add(new PVector(x, y)); frameCounter = 0; if (coordCounter > 0) { PVector tempCoord = (PVector) coords.get(coordCounter - 1); line(x, y, tempCoord.x, tempCoord.y); } coordCounter++; } // println(coordCounter); for (int i = 0; i < coords.size(); i++) { PVector tempCoordnear = (PVector) coords.get(i); float coordDist = dist(x, y, tempCoordnear.x, tempCoordnear.y); if (coordDist < distance) { stroke(255, 0, 0); line(x, y, tempCoordnear.x, tempCoordnear.y); } } frameCounter++; }
public void setup() { size(600, 400); smooth(); f = createFont("Georgia", 12, true); obstacles = new ArrayList(); boids = new ArrayList(); // A little algorithm to pick a bunch of random obstacles that don't overlap for (int i = 0; i < 100; i++) { float x = random(width); float y = random(height); float r = random(50 - i / 2, 50); boolean ok = true; for (int j = 0; j < obstacles.size(); j++) { Obstacle o = (Obstacle) obstacles.get(j); if (dist(x, y, o.loc.x, o.loc.y) < o.radius + r + 20) { ok = false; } } if (ok) obstacles.add(new Obstacle(x, y, r)); } // Starting with three boids boids.add(new Boid(new PVector(random(width), random(height)), 3f, 0.2f)); boids.add(new Boid(new PVector(random(width), random(height)), 3f, 0.1f)); boids.add(new Boid(new PVector(random(width), random(height)), 2f, 0.05f)); }
public void avoid(ArrayList obstacles) { // Make a vector that will be the position of the object // relative to the Boid rotated in the direction of boid's velocity PVector closestRotated = new PVector(sight + 1, sight + 1); float closestDistance = 99999; Obstacle avoid = null; // Let's look at each obstacle for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); float d = PVector.dist(loc, o.loc); PVector dir = vel.get(); dir.normalize(); PVector diff = PVector.sub(o.loc, loc); // Now we use the dot product to rotate the vector that points from boid to obstacle // Velocity is the new x-axis PVector rotated = new PVector(diff.dot(dir), diff.dot(getNormal(dir))); // Is the obstacle in our path? if (PApplet.abs(rotated.y) < (o.radius + r)) { // Is it the closest obstacle? if ((rotated.x > 0) && (rotated.x < closestRotated.x)) { closestRotated = rotated; avoid = o; } } } // Can we actually see the closest one? if (PApplet.abs(closestRotated.x) < sight) { // The desired vector should point away from the obstacle // The closer to the obstacle, the more it should steer PVector desired = new PVector(closestRotated.x, -closestRotated.y * sight / closestRotated.x); desired.normalize(); desired.mult(closestDistance); desired.limit(maxspeed); // Rotate back to the regular coordinate system rotateVector(desired, vel.heading2D()); // Draw some debugging stuff if (debug) { stroke(0); line(loc.x, loc.y, loc.x + desired.x * 10, loc.y + desired.y * 10); avoid.highlight(true); } // Apply Reynolds steering rules desired.sub(vel); desired.limit(maxforce); acc.add(desired); } }
public void mouseClicked() { ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); if (obj.isMouseOver()) { println(obj.getName() + " clicked!"); break; } } }
public void render(PGraphics g) { if (parent != null) rot = PApplet.lerp(rot, parent.rot, 0.25f); g.rotate(rot); g.textSize(txtSize); g.text(txt, 0, 0); g.translate(g.textWidth(txt) * kerning, 0); for (int i = 0; i < children.size(); i++) children.get(i).render(g); }
public void draw() { background(255); for (int i = 0; i < balls.size(); i++) { Ball ball = (Ball) balls.get(i); ball.calc(); ball.display(); } }
public void draw() { background(0); fill(255); if (!paused) timeline.moveForward(); if (dragging) drawOrbits(timeline.getFutureObjectStates()); else drawOrbits(timeline.getPastObjectStates()); ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); obj.display(); } }
public void draw() { // background(255); fill(255); rect(-4, -4, width + 4, height + 4); for (int i = skaters.size() - 1; i >= 0; i--) { Skater skater = (Skater) skaters.get(i); // if (mousePressed) { skater.trace(mouseX, mouseY); // blob[i].x etc. println(i); } }
/** If a key is pressed perform the respective actions */ public void keyPressed() { // Add 'stems' to the balls if (keyCode == SHIFT) { stems = !stems; for (int i = 0; i < balls.size(); i++) { Ball b = (Ball) balls.get(i); b.STEM = stems; } } // toggle repaint background else if (key == 'b') REPAINT = !REPAINT; // Empty the ArrayList of Balls else if (key == 'x') balls.clear(); // Add a ball else if (key == 'f') addBall(); }
public int moveForward() { // println("forward!"); intTimeIdx++; // if the future values have already been calculated, just fetch them instead of calculating // them again if (alObjectStateArchive.size() > intTimeIdx) setCurrentState(cloneArrayList((ArrayList) alObjectStateArchive.get(intTimeIdx))); else { // println("calculating..."); sim.calculateForces(alStatefulObjects); alObjectStateArchive.add(cloneArrayList(alStatefulObjects)); } sliderTimeline.setValue(intTimeIdx); return intTimeIdx; }
public void mouseDragged() { if (paused) { ArrayList objects = timeline.getStatefulObjects(); for (int i = 0; i < objects.size(); i++) { CelestialObject obj = (CelestialObject) objects.get(i); if (obj.isMouseOver()) { dragging = true; PVector pos = obj.getPosition(); pos.x = mouseX; pos.y = mouseY; timeline.reset(); timeline.setCurrentState(objects); sliderTimeline.setValue(0); break; } } } }
public void addPoint(long handId, PVector handPoint) { ArrayList curList = getPointList(handId); curList.add(0, handPoint); if (curList.size() > _maxPoints) curList.remove(curList.size() - 1); }
protected String[] getMachineParams() { ArrayList<String> params = new ArrayList<String>(); // params.add("-Xint"); // interpreted mode // params.add("-Xprof"); // profiler // params.add("-Xaprof"); // allocation profiler // params.add("-Xrunhprof:cpu=samples"); // old-style profiler // TODO change this to use run.args = true, run.args.0, run.args.1, etc. // so that spaces can be included in the arg names String options = Preferences.get("run.options"); if (options.length() > 0) { String pieces[] = PApplet.split(options, ' '); for (int i = 0; i < pieces.length; i++) { String p = pieces[i].trim(); if (p.length() > 0) { params.add(p); } } } // params.add("-Djava.ext.dirs=nuffing"); if (Preferences.getBoolean("run.options.memory")) { params.add("-Xms" + Preferences.get("run.options.memory.initial") + "m"); params.add("-Xmx" + Preferences.get("run.options.memory.maximum") + "m"); } if (Base.isMacOS()) { params.add("-Xdock:name=" + build.getSketchClassName()); // params.add("-Dcom.apple.mrj.application.apple.menu.about.name=" + // sketch.getMainClassName()); } // sketch.libraryPath might be "" // librariesClassPath will always have sep char prepended params.add( "-Djava.library.path=" + build.getJavaLibraryPath() + File.pathSeparator + System.getProperty("java.library.path")); params.add("-cp"); params.add(build.getClassPath()); // params.add(sketch.getClassPath() + // File.pathSeparator + // Base.librariesClassPath); // enable assertions // http://dev.processing.org/bugs/show_bug.cgi?id=1188 params.add("-ea"); // PApplet.println(PApplet.split(sketch.classPath, ':')); String outgoing[] = new String[params.size()]; params.toArray(outgoing); // PApplet.println(outgoing); // PApplet.println(PApplet.split(outgoing[0], ":")); // PApplet.println(); // PApplet.println("class path"); // PApplet.println(PApplet.split(outgoing[2], ":")); return outgoing; // return (String[]) params.toArray(); // System.out.println("sketch class path"); // PApplet.println(PApplet.split(sketch.classPath, ';')); // System.out.println(); // System.out.println("libraries class path"); // PApplet.println(PApplet.split(Base.librariesClassPath, ';')); // System.out.println(); }
public void setCurrentState(ArrayList alState) { for (int i = 0; i < alStatefulObjects.size(); i++) { alStatefulObjects.set(i, alState.get(i)); } }