@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; }
/** 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); } } }