Hi Chrisir, thank you for your advice. I like what the lerp() does to the motion.
My goal is to uses this logic to fill 3d Mesh. Here is the version I updated, there is probably still some problem but, it is already very interesting.
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(800, 800);
background(51);
frameRate(60);
walkers = new ArrayList<Walker>();
anchors = new ArrayList<Anchor>();
for (int i = 0; i < anchorCount; i++) {
PVector position = new PVector(random(width), random(height));
anchors.add(new Anchor(position));
}
for (int i = 0; i < walkerCount; i++) {
int idx = (int)random(anchors.size());
Anchor origin = anchors.get(idx);
PVector originVec = origin.pos;
walkers.add(new Walker(originVec, color(random(256), random(256), random(256))));
anchors.remove(idx);
}
}
void draw() {
background(51);
for (int i = 0; i < anchors.size(); i++) {
Anchor a = anchors.get(i); // (Anchor) anchors.get(i);
a.show();
}
for (int i = walkers.size()-1; i >= 0; i--) {
Walker w = walkers.get(i);
w.update(anchors);
w.show();
if (w.alive == false && walkers.size() <= walkerCount) {
addWalker(anchors);
walkers.remove(i);
}
println("walkers"+walkers.size());
}
for (Line l : lines) {
l.show();
}
}//draw()
// ===========================================================================
void addWalker(ArrayList<Anchor> anchors) {
int idx = (int)random(anchors.size());
Anchor o = anchors.get(idx);
PVector originVec = o.pos;
walkers.add(new Walker(originVec, color(random(256), random(256), random(256))));
anchors.remove(idx);
}
// ===========================================================================
class Walker {
PVector pos;
PVector previousPos;
color col;
// for lerp
float amt;
float amtAdd = 0.1;
boolean running = false;
boolean alive;
PVector move;
// ID of anchor
int idx;
Walker(PVector position, color c) {
alive = true;
col = c;
pos = position.copy();
previousPos = pos.copy();
}
void show() {
pushMatrix();
noStroke();
fill(col);
ellipse(pos.x, pos.y, 8, 8);
popMatrix();
}
void update(ArrayList<Anchor> anchors) {
if (anchors.size() > 0) {
if (running) {
idx = getNearest(anchors);
pos.x = lerp(previousPos.x, move.x, amt);
pos.y = lerp(previousPos.y, move.y, amt);
amt += amtAdd;//.1;
if (amt > 1) {
lines.add(new Line(previousPos, pos, col));
amt = 0; //reset
running = false;
}
} else {
// find the new position and prepare lerp
running=true;
idx = getNearest(anchors);
// println(""+idx);
if (idx < 0) {
alive = false;
return;
}
Anchor a = anchors.get(idx);
move = a.pos.copy();
previousPos.set(pos);
amtAdd = 1 / move.dist(pos) ;
amtAdd*=3;
anchors.remove(idx);
}
}
}
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 < 30) {
if (dist < record) {
record = dist;
idx = i;
}
}
}
if (record<30)
return idx; // give index of nearest target
else
return -1;
}
}
// ===========================================================================
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);
}
void update() {
}
}
// ===========================================================================
class Line {
PVector f;
PVector t;
color col;
Line(PVector f_, PVector t_, color inCol){
f=f_.copy();
t=t_.copy();
col = inCol;
}
void show() {
stroke(col);
strokeWeight(3);
line(f.x, f.y, t.x, t.y );
}
}
Thank’s again.