nosis
YaBB Newbies
Offline
Posts: 11
Re: newb question about array.remove
Reply #4 - Dec 23rd , 2008, 6:02am
Ok.. so basically i've hacked together a couple of examples here to make a flock that is generated by beats in the music. I want to be able to remove the boids after they leave the screen area though to keep it running smooth and fast... Cheers! // ----------------- import ddf.minim.*; import ddf.minim.analysis.*; Minim minim; AudioPlayer song; BeatDetect beat; BeatListener bl; float kickSize, snareSize, hatSize; float colorIt; Flock flock; void setup() { size(1440,500, P2D); flock = new Flock(); smooth(); // minim = new Minim(this); song = minim.loadFile("test.mp3", 2048); song.play(); beat = new BeatDetect(song.bufferSize(), song.sampleRate()); beat.setSensitivity(100); kickSize = snareSize = hatSize = 46; bl = new BeatListener(beat, song); } void draw() { // saveFrame("swarmfloat2_####.png"); background(0); flock.run(); // if ( beat.isKick() ) flock.addBoid(new Boid(new PVector(200,random(height/2, height/2)),1.0,.005)); // colorIt = 50; //if ( beat.isSnare() ) flock.addBoid(new Boid(new PVector(10,random(0, height)),5.0,.5)); //colorIt = 100;//snareSize = 32; //if ( beat.isHat() ) flock.addBoid(new Boid(new PVector(50,random(height/2, height/2)),10.0,10.05)); // // } void stop() { // always close Minim audio classes when you are finished with them song.close(); // always stop Minim before exiting minim.stop(); // this closes the sketch super.stop(); } // Add a new boid into the System void mousePressed() { flock.addBoid(new Boid(new PVector(mouseX,mouseY),2.0f,0.05f)); } // The Boid class class Boid { PVector loc; PVector vel; PVector acc; float r; float maxforce; // Maximum steering force float maxspeed; // Maximum speed Boid(PVector l, float ms, float mf) { acc = new PVector(0,0); vel = new PVector(random(-1,1),random(-1,1)); loc = l.get(); r = 2.0; maxspeed = ms; maxforce = mf; } void run(ArrayList boids) { flock(boids); update(); // borders(); render(); } // We accumulate a new acceleration each time based on three rules void flock(ArrayList boids) { PVector sep = separate(boids); // Separation PVector ali = align(boids); // Alignment PVector coh = cohesion(boids); // Cohesion // Arbitrarily weight these forces sep.mult(20.0); ali.mult(5.20); coh.mult(5.01); // Add the force vectors to acceleration acc.add(sep); acc.add(ali); acc.add(coh); } // Method to update location void update() { // Update velocity vel.add(acc); // Limit speed vel.limit(maxspeed); loc.add(vel); // Reset accelertion to 0 each cycle acc.mult(0); } void seek(PVector target) { acc.add(steer(target,false)); } void arrive(PVector target) { acc.add(steer(target,true)); } // A method that calculates a steering vector towards a target // Takes a second argument, if true, it slows down as it approaches the target PVector steer(PVector target, boolean slowdown) { PVector steer; // The steering vector PVector desired = target.sub(target,loc); // A vector pointing from the location to the target float d = desired.mag(); // Distance from the target is the magnitude of the vector // If the distance is greater than 0, calc steering (otherwise return zero vector) if (d > 0) { // Normalize desired desired.normalize(); // Two options for desired vector magnitude (1 -- based on distance, 2 -- maxspeed) if ((slowdown) && (d < 100.0)) desired.mult(maxspeed*(d/100.0)); // This damping is somewhat arbitrary else desired.mult(maxspeed); // Steering = Desired minus Velocity steer = target.sub(desired,vel); steer.limit(maxforce); // Limit to maximum steering force } else { steer = new PVector(0,0); } return steer; }