Skip to content

Instantly share code, notes, and snippets.

@KrabCode
Created September 11, 2021 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KrabCode/6350c717b00f0eebe4d4d1bff17aef28 to your computer and use it in GitHub Desktop.
Save KrabCode/6350c717b00f0eebe4d4d1bff17aef28 to your computer and use it in GitHub Desktop.
sorry for misleading naming in "water", it behaves like sand instead
float[][] grid;
float[][] temp;
float scale = 0.12;
int w, h;
boolean add = false;
float touchSize = 10;
void setup() {
fullScreen();
colorMode(HSB,1,1,1,1);
background(0);
w = floor(width * scale);
h = floor(height * scale);
grid = new float[w][h];
temp = new float[w][h];
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
grid[x][y] = 0;
}
}
}
void draw() {
if(random(1) < .2){
boolean oldAdd = add;
add = true;
inputWater(random(w), 1, 1);
add = oldAdd;
}
if(frameCount % 60 == 0){
for(int x = 0; x < w; x++){
// grid[x][h-1] = 0;
}
}
for(int i = 0; i < 1; i++){
update();
}
display();
}
void display(){
noStroke();
float rectWidth = width / w + 1;
float rectHeight = height / h + 1;
for (int x = 1; x < w-1; x++) {
float screenX = map(x,1,w-1,0,width);
for (int y = 1; y < h; y++) {
float screenY = map(y,1,h,0,height);
float val = grid[x][y];
fill(color(val));
rect(screenX, screenY, rectWidth, rectHeight);
}
}
}
void update(){
for (int x = 1; x < w-1; x++) {
for (int y = h-2; y > 0; y--) {
float me = grid[x][y];
float down = grid[x][y+1];
float botRight = grid[x+1][y+1];
float botLeft = grid[x-1][y+1];
if(me > down){
me -= 1;
down += 1;
} else if(me > botLeft){
me -= 1;
botLeft += 1;
} else if(me > botRight){
me -= 1;
botRight += 1;
}
me = constrain(me, 0, 1);
down = constrain(down, 0, 1);
botLeft = constrain(botLeft, 0, 1);
botRight = constrain(botRight, 0, 1);
grid[x][y] = me;
grid[x][y+1] = down;
grid[x+1][y+1] = botRight;
grid[x-1][y+1] = botLeft;
}
}
}
void mousePressed(){
add = !add;
inputWater(mouseX*scale, mouseY*scale, touchSize);
}
void mouseDragged(){
inputWater(mouseX*scale, mouseY*scale, touchSize);
}
void inputWater(float x, float y, float size){
for (int xi = 0; xi < w; xi++) {
for (int yi = 0; yi < h; yi++) {
if(dist(x,y,xi,yi) < size){
grid[xi][yi]= add ? 1:0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment