I see the final result here has a left bias, just press and hold to see it stacking towards left before right. So the next fix could be a random choice.
I see the final result here has a left bias, just press and hold to see it stacking towards left before right. So the next fix could be a random choice.
Another issue is that it forms a perfect diagonal, which isn't the most pleasing to look at. It can be addressed by randomly allowing for stacks of two granules and allowing spreading further than just one position to the left or right.
I figured where I left it was appropriate for this post, but I'm happy to write follow-ups for other particle types and the kind of improvement you mentioned!
One fix I found is relatively straightforward:
class FallingGrid2 extends FallingGrid {
// Draw from the end of the list to the beginning
update() {
+ const direction = Math.random() > 0.5 ? 'ltr' : 'rtl';
for (let i = this.grid.length - this.width - 1; i > 0; i--) {
- this.updatePixel(i);
+ if (direction === 'rtl') {
+ this.updatePixel(i);
+ } else {
+ const offset = i % this.width;
+ const flipped = this.width - offset;
+ this.updatePixel(i - offset + flipped);
+ }
}
}
}
Cheers for making the source so easily hackable :) updatePixel(i) {
const below = i + this.width;
const belowLeft = below - 1;
const belowRight = below + 1;
// flag to indicate if we checked the left side already
let checkedLeft = false;
if (this.isEmpty(below)) {
swap(i, below);
// check the left side first with 50 % prob (ltr)
} else if (Math.random() <= 0.5 && this.isEmpty(belowLeft)) {
swap(i, belowLeft);
// indicate we checked the left side already
checkedLeft = true;
// check the right side first with 50 % prob
} else if (this.isEmpty(belowRight)) {
swap(i, belowRight);
// if we haven't checked the left side earlier, check it now (rtl)
} else if (!checkedLeft && window.sandgrid[belowLeft] == 1)
swap(i, belowLeft);
}3rd last line the "window.sandgrid[belowLeft] == 1" needs to be "this.isEmpty(belowLeft)" instead
Other than that I'm not sure what would be causing the weird behaviour. I use this and it seems to work just fine.