Ant’s Death Spiral
Code: https://editor.p5js.org/Xiaozao/sketches/e8ilAu4Ew
This week’s assignment is inspired by a well-known phenomenon in animals’ behavior called “the ant’s death spiral”. When ants are navigating in a dense forest, each ant always maintains a close distance from the ant ahead of them by following the pheromone trail it leaves. However, when the leading ant loses its track and accidentally runs into one of the other ants, it will form a closed circle among the ants, and make them fall into the endless loop of circular motion that leads to their death.
Therefore, I wanted to create this autonomous agent system where at first, the leading ant is wandering to find food, and every ant in the line follows the ant in front of it. But when the leading ant runs into another ant, it will stop wandering and instead decide to follow that ant’s track. Therefore, all the ants are following the ant ahead of them, meaning that no one is leading, or you can say everyone is leading. They will continue this pattern forever.
Code-wise, I mainly used two classes: the normal ant (vehicle) and the leading ant (target). All the ants can seek but the leading ant has the extra ability to wander.
I used this loop to make any ant follow the ant ahead of it:
let second_ant = ants[num-1]; let second_seek = second_ant.seek(leading_ant); second_ant.applyForce(second_seek); second_ant.update(); second_ant.show(); for (let i = 0; i < num-1; i++) { let front_ant = ants[i+1]; let back_ant = ants[i]; let steering = back_ant.seek(front_ant); back_ant.applyForce(steering); back_ant.update(); back_ant.show(); } leading_ant.update(); leading_ant.show();
And I check if the leading ant is running into any of the ants, and change its behavior if so:
if (trapped == false) { let steering_wander = leading_ant.wander(); leading_ant.applyForce(steering_wander); // checking leading ant collision for (let i = 0; i < num; i ++) { let ant = ants[i]; if (leading_ant.position.dist(ant.position) < 1) { let new_steering = leading_ant.seek(ant); leading_ant.applyForce(new_steering); followed_ant = ant; trapped = true; break; } } } else { let steering = leading_ant.seek(followed_ant); leading_ant.applyForce(steering); }
What to improve:
In this sketch, I think that the ants are over-ordered. In reality, the ants are not in a perfect line and they are slightly affected by other ants and their surroundings. Maybe I can add other factors that can affect the ants’ movement in the future.