Java - Snowflake Animation

One of my classmates recently introduced me to Processing, and the first project I attempted was animating snowflakes.

Though admittedly, they're just white circles.

Here's a preview of what it looks like:



Either way, it was interesting to play around with Processing and learn a bit of Java while I'm at it. Some things to note about scalability, though. Each snowflake has individual properties and there's no instances or anything to speed up processing, so having larger amounts of snowflakes tends not to be a great idea while running on CPU. One of Processing's renderers, FX2D, can be used to allow for more ridiculous numbers of snowflakes at higher resolutions, however.

For now, there's no interaction built into it, however, I'm considering implementing some functions that allow the user to create wind and blow snowflakes around using their mouse. I'm fairly certain this was done once on YouTube.

Below's the source for anyone who's interested in seeing how this was achieved.

int amount = 800;
Snowflake[] flakes = new Snowflake[amount];

class Snowflake
{
  float x, y, direction, size, xVelocity, yVelocity, wind;

  Snowflake(int min, int max) {
    x = random(0, width);
    y = random(0, height);
    size = random(min, max);
    xVelocity = 0;
    yVelocity = size * 0.5;
    wind = random(-0.1, 0.1);
  }

  void updatePos() {
    if (random(0, 1) > 0.999)
      wind = random(-0.1, 0.1);
    y += yVelocity;
    x += xVelocity + wind;

    if (y > height) {
      x = random(0, width);
      y = 0;
      size = random(1, 8);
      yVelocity = size * 0.5;
    }
    if (x < 0)
      x = width;
    else if (x > width)
      x = 0;
  }

  void show() {
    ellipse(x, y, size, size);
  }
};

void setup() {
  size(500, 300);
  background(0);
  frameRate(60);
  noStroke();
  smooth();

  for (int i = 0; i
    if (i > int(amount * 0.75))
      flakes[i] = new Snowflake(2, 8);
    else
      flakes[i] = new Snowflake(1, 3);
  }
}

void draw() {
  background(0);
  
  for (Snowflake flake : flakes) {
    flake.show();
    flake.updatePos();
  }
}

Popular posts from this blog

Creating a Messenger Bot: Part 1

Lego EV3 Project - Software