// Flocking // Daniel Shiffman // Male class // Methods for Separation, Cohesion, Alignment added // Click mouse to add Males into the system // Created 2 May 2005 class Female extends Boid { float mass; int timer = 0; int maxTime = 300; Female(Vector3D l, float ms, float mf, float mr) { super(l, ms, mf); mass = mr; } void run(ArrayList boids) { super.run(boids); // Cycle through MALE particle systems float closestDist = 1000000; int male = 0; for (int i = 0; i < boids.size(); i++) { Boid b = (Boid)boids.get(i); String s = b.getSex(); if (s == "Male"){ float d = Vector3D.distance(b.loc,loc); if (d < closestDist) { closestDist = d; male = i; } } } Male m = (Male)boids.get(male); if (m.getBusy() == false){ m.setBusy(); Vector3D attractForce = m.calcGravForce(this, m); attractForce.mult(1); acc.add(attractForce); timer++; } } String getSex(){ super.getSex(); String s = "Female"; return s; } void render() { super.render(); // Draw a triangle rotated in the direction of velocity float theta = vel.heading2D() + radians(135); stroke(200,50,50); fill(255,100,100); pushMatrix(); translate(loc.x,loc.y); rotate(theta); beginShape(); vertex(mass/4, 0); vertex(mass/2, mass/4); vertex(mass-mass/4, 0); vertex(mass, mass/4); vertex(mass-mass/4, mass/2); vertex(mass, mass-mass/4); vertex(mass-mass/4, mass); vertex(mass/2, mass-mass/4); vertex(mass/4, mass); vertex(0, mass-mass/4); vertex(mass/4, mass/2); vertex(0, mass/4); vertex(mass/4, 0); endShape(); ellipse(mass*1.3,mass*1.3,mass*1.3,mass*1.3); popMatrix(); } // We accumulate a new acceleration each time based on three rules void flock(ArrayList boids) { swt = 2.0; //sep.mult(25.0f); awt = 0.1f; //ali.mult(4.0f); cwt = 0.5f; //coh.mult(5.0f); super.flock(boids); Vector3D sep = separate(boids); // Separation Vector3D ali = align(boids); // Alignment Vector3D coh = cohesion(boids); // Cohesion // Arbitrarily weight these forces sep.mult(2.0f); ali.mult(1.0f); coh.mult(1.0f); // Add the force vectors to acceleration acc.add(sep); acc.add(ali); acc.add(coh); } }