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); }
void checkClasses(Boid b) { if (addSpeciesNameInUIClass) { String uiClass = b.getAttribute("ui.class"); if (uiClass == null) uiClass = name; else uiClass = uiClass + " " + name; b.setAttribute("ui.class", uiClass); } }
/** * Unregister this species from the boids graph. * * <p>This removes all boids pertaining to this species, and release the link with the graph. */ public void release() { pop.release(); Iterator<Node> i = ctx.getNodeIterator(); while (i.hasNext()) { Boid b = (Boid) i.next(); if (b.getSpecies() == this) { i.remove(); } } }
public void draw() { background(0); // Draw an ellipse at the mouse location int mx = mouseX; int my = mouseY; fill(100); noStroke(); ellipse(mx, my, 30, 30); // call some behaviors on our agent // b.wander(); // b.seek(new Vector3D(mx,my)); b.arrive(new Vector3D(mx, my)); b.run(); }
/** Ensures all boids are inside the walls. */ public void ensureContainment() { ArrayList<Line2D.Float> walls = p.getWalls(); for (Boid boid : boids) { int intersections = 0; PVector position = boid.getPosition(); for (Line2D.Float wall : walls) { if (wall.intersectsLine(position.x, position.y, Float.MAX_VALUE, position.y)) { ++intersections; } } if (intersections % 2 == 0) { boid.setPosition(new PVector(p.width / 2.0f, p.height / 2.0f)); csp.updateEntity(boid, position); } } }
/** * Creates an empty flock. * * @param parent the PApplet that hosts this class */ public Flock(Flocking applet) { p = applet; boids = new ArrayList<Boid>(); // Size the cells so that we never have to search more // than four cells. float cellSize = 2.0f * Boid.getNeighborhoodSize(); int cellsX = (int) (p.width / cellSize); int cellsY = (int) (p.height / cellSize); csp = new CellSpacePartition<Boid>(p.width, p.height, cellsX, cellsY); }
@Override public Vector2 getUpdate(Boid boid, Vector<Boid> neighbours) { Vector2 result = new Vector2(0, 0); for (int i = 0; i < neighbours.size(); ++i) { Vector2 direction = boid.getPosition().minus(neighbours.get(i).getPosition()); double distance = direction.normL2(); // Normalize if (distance > 0) direction = direction.divide(distance); double rejection = strength / (1e-7 * strength + falloff * distance); result = result.add(direction.multiply(rejection)); } return result; }
void unregister(Boid b) { boids.remove(b.getId()); /* * if (addSpeciesNameInUIClass) { String uiClass = * b.getAttribute("ui.class"); * * if (uiClass != null && uiClass.indexOf(name) != -1) { uiClass = * uiClass.replaceAll("(^|\\s)" + name + "($|\\s)", " "); uiClass = * uiClass.trim(); * * if(uiClass.length() == 0 ) b.removeAttribute("ui.class"); else * b.setAttribute("ui.class", uiClass); } } */ }
void register(Boid b) { boids.put(b.getId(), b); }
/** Updates all boids in the flock. */ public void update() { for (Boid boid : boids) { boid.update(csp); } }