Last active
July 3, 2017 18:44
-
-
Save JoshuaSullivan/26dbb17f971f84458ee694f11bb0ea20 to your computer and use it in GitHub Desktop.
Pillars lie beneath the surface, popped up by frequent puffs of air…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| Pillar() { | |
| velocity = 0.0; | |
| height = 0.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(); | |
| if (h > 0.0) { | |
| fill(204); | |
| quad(0, 0, -pw, -ph, -pw, -(h + ph), 0, -h); | |
| fill(153); | |
| quad(0, 0, pw, -ph, pw, -(h + ph), 0, -h); | |
| } | |
| fill(240); | |
| 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); | |
| 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(255); | |
| 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