const colorArray = "#FF0000/#FFA500/#FFFF00/#008000/#0000FF/#800080/#FFC0CB/#A52A2A/#808080/#FFD700/#C0C0C0/#40E0D0".split("/");
createCanvas(windowWidth, windowHeight, WEBGL);
for (let i = 0; i < agentAmount; i++) {
agentArray.push(new agent());
rotateY(frameCount / 120);
for (let k = 0; k < grid; k++) {
for (let j = 0; j < grid; j++) {
for (let i = 0; i < grid; i++) {
const { x, y, z } = denormalize(i, j, k);
const t = ((frameCount - 1) % subStep) / subStep;
agentArray.forEach((a) => {
const denormalize = (x, y, z) => ({
x: map(x, 0, 3, -200, 200),
y: map(y, 0, 3, -200, 200),
z: map(z, 0, 3, -200, 200),
this.curr = createVector(x, y, z);
this.next = this.curr.copy();
this.color = colorArray[agentArray.length] || "white";
this.isInBounds = (x, y, z) => {
return 0 <= x && x < grid && 0 <= y && y < grid && 0 <= z && z < grid;
this.hasNoCollision = (x, y, z) => {
const rest = agentArray.filter((a) => a != this);
(x == a.curr.x && y == a.curr.y && z == a.curr.z) ||
(x == a.next.x && y == a.next.y && z == a.next.z)
this.curr = this.next.copy();
availablePaths = vectors.filter((dif) => {
return this.isInBounds(x, y, z) && this.hasNoCollision(x, y, z)
const choice = random(availablePaths);
? createVector(pos.x + choice.x, pos.y + choice.y, pos.z + choice.z)
const pos = p5.Vector.add(
this.curr.copy().mult(1 - t),
this.next.copy().mult(t));
const { x, y, z } = denormalize(pos.x, pos.y, pos.z);