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);
  }
Esempio n. 2
0
  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);
    }
  }
Esempio n. 3
0
  /**
   * 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();
  }
Esempio n. 5
0
 /** 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);
     }
   }
 }
Esempio n. 6
0
  /**
   * 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);
  }
Esempio n. 7
0
 @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;
 }
Esempio n. 8
0
  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); } }
     */
  }
Esempio n. 9
0
 void register(Boid b) {
   boids.put(b.getId(), b);
 }
Esempio n. 10
0
 /** Updates all boids in the flock. */
 public void update() {
   for (Boid boid : boids) {
     boid.update(csp);
   }
 }