Skip to content

Instantly share code, notes, and snippets.

@JoshuaSullivan
Created July 4, 2017 17:48
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 JoshuaSullivan/02d2fa33aa00d142d2e8c59f82fbb94f to your computer and use it in GitHub Desktop.
Save JoshuaSullivan/02d2fa33aa00d142d2e8c59f82fbb94f to your computer and use it in GitHub Desktop.
The pillars have undergone chromatic infusion…
final int PILLAR_ORDER = 6;
final float PILLAR_WIDTH = 40.0;
final float PILLAR_HEIGHT = 20.0;
final float PILLAR_SPACING = 2.0;
final float GRAVITY = -1.0;
final float ELASTICITY = -0.6;
final float MAX_HEIGHT = 500.0;
final int POP_INTERVAL = 100;
class Pillar {
float velocity, height, hue;
Pillar() {
velocity = 0.0;
height = 0.0;
hue = random(360.0);
}
void popUp() {
velocity += random(12.0, 22.0);
}
void update() {
if (velocity == 0.0 && height == 0.0) {
// At rest.
return;
}
velocity += GRAVITY;
height += velocity;
if (height <= 0) {
// Bounce!
height = 0.0;
velocity *= ELASTICITY;
if (velocity < abs(GRAVITY)) {
velocity = 0.0;
}
} else if (height >= MAX_HEIGHT) {
// Bounce off ceiling.
height = MAX_HEIGHT;
velocity *= ELASTICITY;
}
}
void draw() {
if (height == 0.0 && velocity == 0.0) {
return;
}
float pw = PILLAR_WIDTH / 2.0;
float ph = PILLAR_HEIGHT / 2.0;
float h = round(height);
noStroke();
color c;
if (h > 0.0) {
c = color(hue, 20, 100, 100);
fill(c);
quad(0, 0, -pw, -ph, -pw, -(h + ph), 0, -h);
c = color(hue, 40, 100, 100);
fill(c);
quad(0, 0, pw, -ph, pw, -(h + ph), 0, -h);
}
float hRatio = h / MAX_HEIGHT;
c = color(hue, 4 + 10 * hRatio, 100, 100);
fill(c);
quad(0, -h, -pw, -(h + ph), 0, -(h + pw), pw, -(h + ph));
}
}
float[] offsets;
int lastPop = 0;
int pillarCount;
Pillar[] pillars;
int calculatePillarCount(int order) {
return (order * (order + 1)) / 2;
}
void setup() {
size(300, 640);
colorMode(HSB, 360, 100, 100, 100);
pillarCount = PILLAR_ORDER * PILLAR_ORDER;
pillars = new Pillar[pillarCount];
for (int i = 0; i < pillarCount; i++) {
pillars[i] = new Pillar();
}
int index = 0;
float rowCount = 0.0;
offsets = new float[pillarCount * 2];
float pw = PILLAR_WIDTH / 2.0 + PILLAR_SPACING;
float ph = -(PILLAR_HEIGHT / 2.0 + PILLAR_SPACING);
for (int i = 0; i < PILLAR_ORDER; i++) {
float xOffset = float(i) * -pw;
for (int j = 0; j <= i; j++) {
float x = j * (PILLAR_WIDTH + 2.0 * PILLAR_SPACING) + xOffset;
float y = rowCount * ph;
offsets[index * 2] = x;
offsets[index * 2 + 1] = y;
index++;
}
rowCount += 1.0;
}
for (int i = PILLAR_ORDER - 2; i >= 0; i--) {
float xOffset = float(i) * -pw;
for (int j = 0; j <= i; j++) {
float x = j * (PILLAR_WIDTH + 2.0 * PILLAR_SPACING) + xOffset;
float y = rowCount * ph;
offsets[index * 2] = x;
offsets[index * 2 + 1] = y;
index++;
}
rowCount+= 1.0;
}
}
void draw() {
background(0, 0, 100, 100);
int now = millis();
int timeSincePop = now - lastPop;
if (timeSincePop >= POP_INTERVAL) {
int index = int(random(pillarCount));
pillars[index].popUp();
lastPop = now;
}
pushMatrix();
translate(150, 630);
for (int i = pillarCount - 1; i >= 0; i--) {
pushMatrix();
float x = offsets[i * 2];
float y = offsets[i * 2 + 1];
translate(x, y);
pillars[i].update();
pillars[i].draw();
popMatrix();
}
popMatrix();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment