Thank you Chrisir, that right, and I also spotted that I didn’t sub the target with the pos. Thank you for your input.
void update(ArrayList<Anchor> anchors) {
if (anchors.size()>0) {
int idx = getNearest(anchors);
println(""+idx);
Anchor a = anchors.get(idx);
PVector target = a.pos;
PVector move = target.sub(pos);
previousPos.set(pos);
pos.add(move);
anchors.remove(idx);
}
}
int getNearest(ArrayList<Anchor> anchors) {
float record = 1000;
int idx=0;
for (int i = 0; i < anchors.size(); i++) {
Anchor anchor = anchors.get(i);
float dist = pos.dist(anchor.pos);
if (dist < 10) {
if (dist < record) {
dist = record;
idx = i;
if (idx == 0){break;}
}
}
}
return idx;
I am getting closer to the desired solution :); A problem remains when the walker doesn’t find a smaller distance, the index becomes zero, so the walker jumps to the next zero of the list.
Furthermore, I am trying to stop the process if;
- the walker doesn’t find a point close enough
- the walker has to jump on another walker path
I would like to implement this kind of code on a mesh later on
thank you again