From what I understand you created a grid and calculated collisions/ gravity between particles found only in the same partition is that correct? .

Only for the collisions, and it checks for collisions in the same partition (cell) and in the up, down, left, and right cells. It does not check diagonal cells, so occasionally misses a few (leading to temporary overlaps). It also only does a simple radius overlap check for collisions, so will also miss quite a few at higher speeds (not leading to overlaps). However that's acceptable for simulating gasses, as the individual collision are not each important, just that you get a bunch of them.

For gravity it does the full N*N calculations, which is why it's slow with lots of particles. The math is simpler though.

Hah, just spotted a bug:

Code:

```
if (d2>0) {
var d = Math.sqrt(d2);
dx /= d;
dy /= d;
var a = g / d2;
p.vx += dx * g;
p.vy += dy * g;
}
```

I thought the gas gravity was a bit boring, fix to come.