Skip to content

Instantly share code, notes, and snippets.

@stevensona
Last active May 10, 2016 19:23
Show Gist options
  • Save stevensona/124dbd5a3e2cbdd7c547 to your computer and use it in GitHub Desktop.
Save stevensona/124dbd5a3e2cbdd7c547 to your computer and use it in GitHub Desktop.
Leafless trees growing on a grid
int COLS = 6;
int ROWS = 6;
Tile[][] grid;
float wind = 0;
PGraphics pick_buffer;
class Tile {
float age, mass, water, food;
Tile() {
age = 0;
mass = 1;
water = random(0.25, 1.5);
food = random(0.25, 1.5);
}
private void update() {
age += 1 / 60.0;
mass += food / 60.0 + water / 60.0;
food *= 0.995;
water *= 0.99;
}
private void draw_branch(int seed, float size) {
float seg_height = size * 4;
translate(0, 0, seg_height / 2.0);
fill(185, 122, 87);
box(size, size, seg_height);
translate(0, 0, seg_height / 2.0);
randomSeed(seed);
rotateX(random(-0.2, 0.2) + 0.02 * sin(wind));
rotateY(random(-0.2, 0.2) + 0.02 * sin(3 * wind+3));
rotateZ(random(-PI, PI));
if(random(100) < 40) {
pushMatrix();
rotateX(random(PI / 2));
rotateY(random(PI / 2));
draw_branch(seed * seed, size * 0.5);
popMatrix();
}
if(size > 1.0) {
draw_branch(seed * seed + 1, size * 0.8);
}
}
void draw(int seed) {
update();
fill(34, 177, 76);
box(50, 50, 1); // base
draw_branch(seed, mass);
}
}
void setup() {
frameRate(60);
size(800, 600, P3D);
pick_buffer = createGraphics(600, 500, P3D);
smooth(4);
//noStroke();
grid = new Tile[COLS][ROWS];
for(int x = 0; x < COLS; x++) {
for(int y = 0; y < ROWS; y++) {
grid[x][y] = new Tile();
}
}
}
float angle = 0;
void draw() {
background(75);
translate(width / 2, height / 2, 0);
rotateX(1);
rotateZ(angle+=0.005);
//pointLight(255, 255, 255, 0, 0, 200);
wind += 0.01;
for(int x = 0; x < COLS; x++) {
for(int y = 0; y < ROWS; y++) {
pushMatrix();
translate((x - COLS / 2.0) * 50 + 25, (y - COLS / 2.0) * 50 + 25, 0);
grid[x][y].draw(x * y + x + y);
popMatrix();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment