We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › newb question about array.remove
Page Index Toggle Pages: 1
newb question about array.remove (Read 1790 times)
newb question about array.remove
Dec 20th, 2008, 9:20am
 
hello.. so i am completely new at processing (but have done some actionscripting). I am trying to remove items from an array but cant seem to get it working. The 'remove' syntax is not appearing as highlighted correct syntax??

As in:

void removeBoid(Boid b) {
   boids.remove(b);
 }


Oh and while i'm here, is it possible to capture the screen output with a transparent background? Using the saveFrame() function? I want to bring a png sequence into After Effects with an alpha channel.

Cheers :)
Re: newb question about array.remove
Reply #1 - Dec 20th, 2008, 11:26am
 
You use here the syntax of ArrayList's remove.
You might want to transform boids from array to ArrayList, or use... nothing, I see there is shorten() or subset(), no remove() (which would have used an index anyway).
You can implement it yourself, or use the more efficient (for such operations) ArrayList.
Note: array is more efficient (faster) for updates (read, overwrite), ArrayList is good for expanding, removing, etc.
Re: newb question about array.remove
Reply #2 - Dec 23rd, 2008, 5:42am
 
ok.. so i'm not sure how to implemement this in the project i'm working on. How do i post my code here? I keep getting a 'message too long' error. Sorry for being such a dumbass! Sad

Re: newb question about array.remove
Reply #3 - Dec 23rd, 2008, 5:56am
 
Cut it in half and post twice.  And you probably can't beat me on the clueless category Tongue
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;
 }
 
Re: newb question about array.remove
Reply #5 - Dec 23rd, 2008, 6:02am
 
void render() {
   //------------------------------------------------------------------------------
------------------------
   if((loc.x < -r-50) || (loc.y < -r) ||(loc.x > width+r) || (loc.y > height+r+50)) {
     return;
    //------------------------------ REMOVE BOIDS HERE ----------------------------------------------------
   // Flock removeBoid(loc);
   }
   else {
     fill(255,85,0,200);
     stroke(255,85,0,200);
     pushMatrix();
     //------------------------------------------------------------------------------
----------------------
     translate(loc.x++,loc.y+4);
    // rotate(theta);
    rotate(80.1);
     beginShape();
     vertex(30, 20);
     vertex(40, 15);
     vertex(40, 60);
     vertex(32, 60);
     endShape(CLOSE);
   }
   popMatrix();

 }
 //------------------------------------------
 // Separation
 // Method checks for nearby boids and steers away
 PVector separate (ArrayList boids) {
   float desiredseparation = 20.0;
   PVector sum = new PVector(0,0,0);
   int count = 0;
   // For every boid in the system, check if it's too close
   for (int i = 0 ; i < boids.size(); i++) {
     Boid other = (Boid) boids.get(i);
     float d = loc.dist(other.loc);
     // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
     if ((d > 0) && (d < desiredseparation)) {
       // Calculate vector pointing away from neighbor
       PVector diff = loc.sub(loc,other.loc);
       diff.normalize();
       diff.div(d);        // Weight by distance
       sum.add(diff);
       count++;            // Keep track of how many
     }
   }
   // Average -- divide by how many
   if (count > 0) {
     sum.div((float)count);
   }
   return sum;
 }
 // Alignment
 // For every nearby boid in the system, calculate the average velocity
 PVector align (ArrayList boids) {
   float neighbordist = 200.0;
   PVector sum = new PVector(0,0,0);
   int count = 0;
   for (int i = 0 ; i < boids.size(); i++) {
     Boid other = (Boid) boids.get(i);
     float d = loc.dist(other.loc);
     if ((d > 0) && (d < neighbordist)) {
       sum.add(other.vel);
       count++;
     }
   }
   if (count > 0) {
     sum.div((float)count);
     sum.limit(maxforce);
   }
   return sum;
 }

 // Cohesion
 // For the average location (i.e. center) of all nearby boids, calculate steering vector towards that location
 PVector cohesion (ArrayList boids) {
   float neighbordist = 0.0;
   PVector sum = new PVector(0,0);   // Start with empty vector to accumulate all locations
   int count = 0;
   for (int i = 0 ; i < boids.size(); i++) {
     Boid other = (Boid) boids.get(i);
     float d = loc.dist(other.loc);
     if ((d > 0) && (d < neighbordist)) {
       sum.add(other.loc); // Add location
       count++;
     }
   }
   if (count > 0) {
     sum.div((float)count);
     return steer(sum,false);  // Steer towards the location
   }
   return sum;
 }
}

// The Flock (a list of Boid objects)
class Flock {
 ArrayList boids; // An arraylist for all the boids
   Flock() {
   boids = new ArrayList(); // Initialize the arraylist
 }

 void run() {
   for (int i = 0; i < boids.size(); i++) {
     Boid b = (Boid) boids.get(i);  
     b.run(boids);  // Passing the entire list of boids to each boid individually
   }
 }

 void addBoid(Boid b) {
   boids.add(b);
 }
 
   void removeBoid(Boid b) {
   boids.remove(b);
 }

}

class BeatListener implements AudioListener
{
 private BeatDetect beat;
 private AudioPlayer source;

 BeatListener(BeatDetect beat, AudioPlayer source)
 {
   this.source = source;
   this.source.addListener(this);
   this.beat = beat;
 }

 void samples(float[] samps)
 {
   beat.detect(source.mix);
 }

 void samples(float[] sampsL, float[] sampsR)
 {
   beat.detect(source.mix);
 }
}
Re: newb question about array.remove
Reply #6 - Dec 23rd, 2008, 6:18am
 
Gets me in a morbid mood. Smiley

try this bit:

Code:

// The Flock (a list of Boid objects)
class Flock {
ArrayList boids; // An arraylist for all the boids
Flock() {
boids = new ArrayList(); // Initialize the arraylist
}

void run() {
ArrayList abattoir = new ArrayList();
for (int i = 0; i < boids.size(); i++) {
Boid b = (Boid) boids.get(i);
b.run(boids); // Passing the entire list of boids to each boid individually
if(b.loc.x > width){
abattoir.add(b);
}
}

for(int i = 0; i < abattoir.size(); i++){
Object b = abattoir.get(i);
boids.remove(b);
println("aaaaaaaaaaargh! boids left:" + boids.size());
}
}
Re: newb question about array.remove
Reply #7 - Dec 23rd, 2008, 6:53am
 
Ahhh... yes! Thanks that is perfect Smiley
Re: newb question about array.remove
Reply #8 - Dec 23rd, 2008, 4:05pm
 
sw01 wrote on Dec 23rd, 2008, 5:56am:
Cut it in half and post twice.

Personally, I advice to either reduce the problem to something smaller but significant, or to use some snippet site, like http://pastebin.com.
With option "permanent", so people searching this forum can always find the code.
Re: newb question about array.remove
Reply #9 - Dec 23rd, 2008, 4:11pm
 
That's a great advice.  I didn't know about the site - thanks!
Re: newb question about array.remove
Reply #10 - Dec 30th, 2008, 1:04am
 
Thanks for the hint PhiLho.

So the code is now here:
http://pastebin.com/f25b960b3

What i would like to be able to do now is to have a boid created in response to the kick drums/snare etc of the music. Which i have done.. but i would like them to be colour coded for each type. So for eg. blue for kick, white for snare. I can easily set a variable for the fill based on this, but this changes the color of all the boids as they are being redrawn each frame. Will it be possible to somehow set the boids colour on creation and then retain this color throughout the movie? I am thinking some kind of boid.color variable, but not sure how this works when each boid is drawn freshly each frame?? Any suggestions welcome - and thanks so much for your help so far.. i am learning!!
Re: newb question about array.remove
Reply #11 - Dec 30th, 2008, 7:59am
 
Interesting code, you did simple but smart use of vectors (I should get used to them too!).

The solution to your problem is simple: add a color field to the Boid class, use it in the fill() call.
You can initialize it at creation (constructor) time, and perhaps add an updateColor method.
Re: newb question about array.remove
Reply #12 - Dec 30th, 2008, 12:35pm
 
Ah yes that was easy! Thanks for the direction.

http://pastebin.com/f30237fa7

It doesnt work as nicely as i was hoping however. I guess what would be nice is if the color shifted across a spectrum of blues according to the average frequency of the audio track using something like minim FFT and a live line in. I think thats a bit beyond me right now though.. baby steps. Thanks for all your assistance Smiley
Page Index Toggle Pages: 1