Random walker through points

You can make here:

if (idx<0) {
    alive=false; 
    return; // !!!
}

when alive is a boolean inside the walker class

Then make a for loop to remove walkers with !alive (NOT alive), this for loop must go backward by the way

for(int i=walkers.size()-1; i>=0; i--) {
    Walker w=walkers.get(i); 
    if(!alive) 
        w.remove();
}

3D version ================================

HERE is a 3D version:


ArrayList<Walker> walkers;
ArrayList<Anchor> anchors;
ArrayList<Line> lines= new ArrayList<Line>();

PVector pos;

int walkerCount = 1;
int anchorCount = 1500;

float x = 0;
float y = 0;

float a; 

void setup() {
  size(1800, 900, P3D);
  background(51);

  //frameRate(69);

  walkers = new ArrayList<Walker>();
  anchors = new ArrayList<Anchor>();

  for (int i = 0; i < walkerCount; i++) {
    PVector origin = new PVector(random(4, 400), 4);
    walkers.add(new Walker(origin, color(random(256), random(256), random(256))));
  }

  for (int i = 0; i < anchorCount; i++) {
    PVector position = new PVector(random(-400, 400), random(4, height-4), random(-800, 800));
    anchors.add(new Anchor(position));
  }
}//setup()

void draw() {
  background(51);
  lights();

  translate(width/2, 0); 
  rotateY(a);
  a+=0.01; 

  for (Line l : lines) {
    l.show();
  }

  for (Walker w : walkers) {  // !!!!!!!!!!!!!!!!!!!!!!!
    w.update(anchors);
    w.show();
  }

  for (int i = 0; i < anchors.size(); i++) {
    Anchor a = anchors.get(i); // (Anchor) anchors.get(i);
    a.show();
  }
}//draw()

// ===========================================================================

class Walker {
  PVector pos;
  PVector previousPos;
  color col;

  // for lerp
  float amt;
  float amtAdd = 0.1;   
  boolean running=false;
  PVector move; 

  // ID of anchor
  int idx;

  Walker(PVector position, color c) {
    col = c;
    pos = position.copy(); 
    previousPos = pos.copy();
  }

  void show() {

    /*
    strokeWeight(2);
     stroke(col);
     point(pos.x, pos.y, pos.z);
     */

    pushMatrix();
    translate(pos.x, pos.y, pos.z);
    noStroke();
    fill(col);
    sphere(14);
    popMatrix(); 

    /*
    strokeWeight(1);
     noFill();
     stroke(col); 
     ellipse(pos.x, pos.y, 8, 8);
     */


    /*
    strokeWeight(4);
     stroke(col);
     line(previousPos.x, previousPos.y, pos.x, pos.y);
     */
  }

  void update(ArrayList<Anchor> anchors) {

    //using lerp() !!! 

    if (anchors.size()>0) {

      if (running) {
        // lerping to the new pos
        pos.x=lerp(previousPos.x, move.x, amt);
        pos.y=lerp(previousPos.y, move.y, amt);
        pos.z=lerp(previousPos.z, move.z, amt);

        amt+=amtAdd;//.1;
        if (amt>1) {
          lines.add(new Line(previousPos, move)); 
          //reset 
          amt=0; 
          running=false;
          anchors.remove(idx);
        }
      } else {
        // find the new position and prepare lerp
        running=true; 

        idx = getNearest(anchors);
        // println(""+idx);

        Anchor a = anchors.get(idx);
        move = a.pos.copy();
        previousPos.set(pos);

        amtAdd = 1 / move.dist(pos) ;  
        amtAdd*=3; 
        // amtAdd = .5; 

        // pos.add(move); // WRONG 
        //pos=move.copy(); // OKAY
      }
    }
  }

  int getNearest(ArrayList<Anchor> anchors) {
    float record = 100000;
    int idx = 0;
    for (int i = 0; i < anchors.size(); i++) {
      Anchor anchor = anchors.get(i);
      float dist = pos.dist(anchor.pos);
      if (dist < record) {
        record = dist; 
        idx = i;
      }
    }
    return idx;    // give index of nearest target
  }
}

// ===========================================================================

class Anchor {
  PVector pos;
  color col;

  Anchor(PVector position) {
    col = color(0);
    pos = position.copy();
  }

  void show() {
    strokeWeight(4);
    stroke(col);
    point(pos.x, pos.y, pos.z);
  }

  void update() {
  }
}
//======================================================================================================

class Line {
  PVector f;
  PVector t;
  Line(PVector f_, PVector t_) { 
    f=f_.copy(); 
    t=t_.copy();
  }

  void show() {
    Walker w = walkers.get(0);
    stroke(w.col);
    line(f.x, f.y, f.z, 
      t.x, t.y, t.z );
  }
}//class
//
2 Likes