This browser has no WebGPU, so the sketch cannot run here. The program is below, and it runs in a current Chrome, Edge or Safari.
Boids 3D
Flocking in three dimensions from three local rules: separation, alignment and cohesion.
// ── main ──
/////////////////////////////////////////
// Boids V5 - canonical sep / coh / ali
&dim = (64,64,1);
&eps = 0.001;
&damp = 0.99;
&mxv = 0.005;
&sepRadius = 0.035;
&cohRadius = 0.10;
&aliRadius = 0.14;
&sepStrength = 0.008;
&cohStrength = 0.0003;
&aliStrength = 0.008;
&speed = 0.015;
&rel = dom_del(#pos,V,2);
&rel_dist = len(&rel);
&acc = add(add(&separation,&cohesion),&align);
&vraw = add(mlt(#vel,&damp),&acc);
&vlim = mlt(&vraw,clp(div(&mxv,add(dst(&vraw,(0)),&eps)),0,1));
#pos{
dim = &dim;
set = (0);
run = dom_del(0, add(#,#vel), 2);
}
#vel{
dim = &dim;
set = mlt(srand(1,2,3),0.001);
run = &vlim;
}
~swarm{
dim = &dim;
shape = CONE;
i_pos = #pos;
i_scl = (0.25, 1, 0.5);
i_rot = aim(#vel);
}
// ── separation ──
&awayVec =
mlt(
div(sub(0, &rel),add(&rel_dist, &eps)),
fof(&rel_dist, &sepRadius)
);
&separation =
mlt(
#pos.SUM[(-1,-1),(1,1)]{ &awayVec },
&sepStrength
);
// ── cohesion ──
&cohCount =
#pos.SUM[(-1,-1),(1,1)]{
mlt(
lrg(&rel_dist, &sepRadius),
fof(&rel_dist, &cohRadius)
)
};
&cohSum =
#pos.SUM[(-1,-1),(1,1)]{
mlt(
&rel,
lrg(&rel_dist, &sepRadius),
fof(&rel_dist, &cohRadius)
)
};
&cohesion =
div(
mlt(&cohSum, &cohStrength),
add(&cohCount, &eps)
);
// ── alignment ──
&aliCount = #pos.SUM[(-1,-1),(1,1)]{
fof(&rel_dist, &aliRadius)
};
&aliSum = #pos.SUM[(-1,-1),(1,1)]{
mlt(
#vel[U.x,U.y],
fof(&rel_dist, &aliRadius)
)
};
&aliAvg = div(&aliSum, add(&aliCount,&eps));
&aliDesired =
mlt(
div(
&aliAvg,
add(dst(&aliAvg, 0), &eps)
)
,&speed
);
&align = mlt(sub(&aliDesired, #vel), &aliStrength);