xxxxxxxxxx
139
let grid;
let velocityGrid;
let w = 10;
let cols, rows;
let hueValue = 255;
let cnt = 0;
let gravity = 0.1;
let panna;
function make2DArray(cols, rows) {
let arr = new Array(cols);
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(rows);
// Fill the array with 0s
for (let j = 0; j < arr[i].length; j++) {
arr[i][j] = 0;
}
}
return arr;
}
// Check if a row is within the bounds
function withinCols(i) {
return i >= 0 && i <= cols - 1;
}
// Check if a column is within the bounds
function withinRows(j) {
return j >= 0 && j <= rows - 1;
}
function setup() {
createCanvas(500, 500);
colorMode(HSB, 360, 255, 255);
cols = (width/2-50)/w;
rows = (height/2-50)/w;
grid = make2DArray(cols, rows);
velocityGrid = make2DArray(cols, rows, 1);
panna = new Panna(width/2-100,height/2-100,200);
}
function draw() {
background(0);
let mcol = floor((width/2-150)/w);
let mrow = floor((height/2-250)/w);
// Randomly add an area of sand particles
let matrix = 5;
let extent = floor(matrix / 2);
for (let i = -extent; i <= extent; i++) {
for (let j = -extent; j <= extent; j++) {
if (random(1) < 0.75) {
let col = mcol + i;
let roww = mrow + j;
if (withinCols(col) && withinRows(roww)) {
grid[col][roww] = hueValue;
velocityGrid[col][roww] = 1;
}
}
}
}
// Draw the sand
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
noStroke();
if (grid[i][j] > 0) {
fill(grid[i][j]);
square(width/2-(cols*w)/2+i*w,height/2-(rows*w)/2+j*w,w);
}
}
}
// Create a 2D array for the next frame of animation
let nextGrid = make2DArray(cols, rows);
let nextVelocityGrid = make2DArray(cols, rows);
// Check every cell
for (let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
// What is the state?
let state = grid[i][j];
let velocity = velocityGrid[i][j];
let moved = false;
if (state > 0) {
let newPos = int(j + velocity);
for (let y = newPos; y > j; y--) {
let below = grid[i][y];
let dir = 1;
if (random(1) < 0.5) {
dir *= -1;
}
let belowA = -1;
let belowB = -1;
if (withinCols(i + dir)) belowA = grid[i + dir][y];
if (withinCols(i - dir)) belowB = grid[i - dir][y];
if (below === 0) {
nextGrid[i][y] = state;
nextVelocityGrid[i][y] = velocity + gravity;
moved = true;
break;
} else if (belowA === 0) {
nextGrid[i + dir][y] = state;
nextVelocityGrid[i + dir][y] = velocity + gravity;
moved = true;
break;
} else if (belowB === 0) {
nextGrid[i - dir][y] = state;
nextVelocityGrid[i - dir][y] = velocity + gravity;
moved = true;
break;
}
}
}
if (state > 0 && !moved) {
nextGrid[i][j] = grid[i][j];
nextVelocityGrid[i][j] = velocityGrid[i][j] + gravity;
}
}
}
grid = nextGrid;
velocityGrid = nextVelocityGrid;
panna.show();
cnt++;
if(cnt>=200){
fill('black');
stroke('black');
square(width/2-20,height/2-20,10);
square(width/2+20,height/2-20,10);
square(width/2,height/2,10);
}
}