adjustVelocity = () => {
let { r1, r2, r3 } = ratios,
rnd1 = d3.randomUniform(30, 50),
rnd2 = d3.randomUniform(),
rndTheta = () => {
return rnd1() * (rnd2() > 0.5 ? 1 : -1);
};
data.map((d) => {
data.map((data) => Object.assign(data, { dist: distance(data, d) }));
let neighbours = data.filter(
(data) => (data.i !== d.i) & (data.dist < neighbourDetectionRadius)
),
center = Object.assign(
{},
{
x: d3.mean(neighbours, (d) => d.x),
y: d3.mean(neighbours, (d) => d.y)
}
);
neighbours.map((data) => d.neighbours.add(data.i));
if (neighbours.length > 0) {
neighbours.sort((a, b) => a.dist - b.dist);
let nearest = neighbours[0];
d.vx += r1 * d3.mean(neighbours, (d) => d.vx);
d.vy += r1 * d3.mean(neighbours, (d) => d.vy);
let v = getVelocity(d);
v /= Math.max(1, distance({ x: center.x - d.x, y: center.y - d.y }));
d.vx += v * (center.x - d.x);
d.vy += v * (center.y - d.y);
if (nearest.dist < overlapDetectionRadius) {
rotate(d, rndTheta());
}
}
limitVelocity(d);
});
}