function collisionSim(particles, maxTicks = 40) {
for (let tick = 0; tick < maxTicks; tick++) {
const positions = new Map();
particles.forEach((p, i) => {
["x", "y", "z"].forEach((axis) => {
p.V[axis] += p.A[axis];
p.P[axis] += p.V[axis];
});
const posStr = `${p.P.x},${p.P.y},${p.P.z}`;
if (!positions.has(posStr)) {
positions.set(posStr, []);
}
positions.get(posStr).push(i);
});
const collidedIndices = new Set();
for (const [_, indices] of positions) {
if (indices.length > 1) {
indices.forEach((i) => collidedIndices.add(i));
}
}
particles = particles.filter((_, i) => !collidedIndices.has(i));
}
return particles.length;
}