// Flocking Particle // - hacky implementation so far, but that's mainly to get it to work with my Physics class // by dickering with the general rule speeds you'll get the sort of behaviour that will look cool class Boid extends Particle{ float centering = 0.01; float avoidance = 0.05; float goalSpeed = 0.08; int id; BoidFlock p; // Constructor Boid(float x, float y, BoidFlock p){ super(x, y, p); this.p = p; } // Keep the boid moving towards the center of the group void group(){ // center measurements float vx = p.center.x-x; float vy = p.center.y-y; float invLen = Line.invSqrt(vx * vx + vy * vy); float dx = vx * invLen; float dy = vy * invLen; addVelocity(dx*centering, dy*centering); } // Avoid other boids void avoid(){ Point move = new Point(0, 0); for(int i = 0; i < p.p.size(); i++){ if(i == id) continue; Boid temp = (Boid)p.p.get(i); float vx = x - temp.x; float vy = y - temp.y; float invLen = Line.invSqrt(vx * vx + vy * vy); if(invLen > avoidance){ float dx = vx * invLen; float dy = vy * invLen; addVelocity(dx*avoidance, dy*avoidance); } } } // Fly toward a given goal void goal(float tx, float ty){ // goal measurements float vx = tx-x; float vy = ty-y; float invLen = Line.invSqrt(vx * vx + vy * vy); float dx = vx * invLen; float dy = vy * invLen; addVelocity(dx*goalSpeed, dy*goalSpeed); } }