Created
January 27, 2015 13:30
-
-
Save CSchoel/ebc09ad9ceffe8212b61 to your computer and use it in GitHub Desktop.
Snow falling on a randomly generated mountain
This file contains 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
//Autor: Christopher Schölzel | |
float FRONT_WIDTH = 20; | |
class Snowflake { | |
float noiseX; | |
float noiseY; | |
float x,y; | |
float vx,vy; | |
float w; | |
Snowflake(float x, float y, float vx, float vy, float w) { | |
this.x = x; | |
this.y = y; | |
this.vx = vx; | |
this.vy = vy; | |
this.w = w; | |
noiseX = random(1000); | |
noiseY = random(1000); | |
} | |
void move() { | |
/*vx += 0.1*(noise((noiseX++)*0.1)-0.5); | |
vy += 0.05*(noise((noiseY++)*0.1)-0.5);*/ | |
vx += random(-0.1,0.1); | |
vy += random(-0.1,0.1); | |
vy = max(vy,0.5); | |
vy = min(vy,5); | |
vx = max(vx,-3); | |
vx = min(vx, 3); | |
x += vx; | |
y += vy*w/FRONT_WIDTH; | |
if(!visible() && y > 0) reset(); | |
} | |
void reset() { | |
y = -w; | |
x = random(width); | |
vx = random(-3,3); | |
vy = random(0.5,3); | |
} | |
boolean visible() { | |
return x > -w && x < width+w && y > -w && y < height+w; | |
} | |
void display() { | |
fill(255); | |
noStroke(); | |
ellipse(x,y,w,w); | |
} | |
} | |
ArrayList<Snowflake> snow; | |
void setup() { | |
size(600,400); | |
snow = new ArrayList<Snowflake>(); | |
int flakes = 400; | |
for(int i = 0; i < flakes; i++) { | |
Snowflake sf = new Snowflake( | |
random(width),-10-random(height), | |
random(-3,3),0.2+random(2), | |
5+random(15) | |
); | |
snow.add(sf); | |
} | |
} | |
void draw() { | |
background(0); | |
for(int i = 0; i < width; i++) { | |
float n = 300*noise(i*0.01); | |
stroke(180); | |
line(i,height/2.0+n,i,height); | |
} | |
for(Snowflake s : snow) { | |
s.move(); | |
s.display(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment