how to delete particles from an array list?

edited December 2017 in Questions about Code

I was wondering how to go about deleting particles from my array list particle trail. I want a stream of particles to be shown when the mouse cursor is moved and when the mouse cursor is moved away a certain distance I want the particles to be deleted. This is the code I have so far

`ArrayList particles = new ArrayList(); ArrayList gone = new ArrayList();

class particle { float X; float Y; float speed_X; float speed_Y;

void draw() { strokeWeight(1); stroke(255); fill(random(0, 255), random(0, 255), random(0, 255)); ellipse(X, Y, 8, 8); X = X + speed_X; Y = Y + speed_Y; } }

void P_Particles() {

for (particle Pa : particles) { Pa.draw(); if (Pa.X > mouseX + 50) { continue; } if (Pa.X < mouseX - 50) { continue; } if (Pa.Y > mouseY + 50) { continue; } if (Pa.Y < mouseY - 50) { continue; } gone.add(Pa); } particles = gone; }

particle Pa = new particle(); Pa.X = mouseX; Pa.Y = mouseY; Pa.speed_X = random(0.1, 0.3); Pa.speed_Y = random(0.1, 0.3); particles.add(Pa); } void draw() { //the rest of my game code to much to put it all in here P_Particles(); }`

Answers

  • ArrayList particles = new ArrayList(); 
    ArrayList gone = new ArrayList();
    
    class particle { 
    float X; 
    float Y; 
    float speed_X; 
    float speed_Y;
    
    void draw() { 
    strokeWeight(1); 
    stroke(255); 
    fill(random(0, 255), random(0, 255), random(0, 255)); 
    ellipse(X, Y, 8, 8); 
    X = X + speed_X; 
    Y = Y + speed_Y; }
     }
    
    void P_Particles() {
    
    for (particle Pa : particles) {
     Pa.draw(); 
    if (Pa.X > mouseX + 50) { 
    continue; 
    } 
    if (Pa.X < mouseX - 50) { 
    continue; 
    } 
    if (Pa.Y > mouseY + 50) { 
    continue; 
    } 
    if (Pa.Y < mouseY - 50) { 
    continue; 
    } 
    gone.add(Pa); 
    } 
    particles = gone; 
    }
    
    particle Pa = new particle(); 
    Pa.X = mouseX; 
    Pa.Y = mouseY; 
    Pa.speed_X = random(0.1, 0.3); 
    Pa.speed_Y = random(0.1, 0.3); 
    particles.add(Pa); 
    } 
    void draw() { 
    //the rest of my game code to much to put it all in here 
    P_Particles(); 
    }
    
  • If you press Ctrl-t in the editor then it'll indent your code nicely and it won't hurt so much to read it.

    There are methods on ArrayList to remove particles both using an index or, iirc, the object you want removing. If using the former then iterate backwards over the list so that it doesn't renumber things you haven't processed yet.

  • There is an explicit example in the reference: https://processing.org/reference/ArrayList.html

    Kf

  • Shameless self-promotion: I wrote a tutorial on ArrayLists in Processing (including removing via index) available here.

  • edited March 2018

    Nice CodePen processing.js sketch for the tutorial. My future dream P5 forum would make embedding those for p5.js / processing.js easy here -- stackoverflow-style easy, with those libraries in the quick dropdown menu.

    This could be really useful for teaching.

  • jeremydouglass: Discourse uses OneBox to automatically embed CodePens in their forum. That's what I use for my forum, it's really nice.

Sign In or Register to comment.