Snow Men From Snow Flakes
By Omar Essilfie-Quaye
Snowfall - The Original
This quick project is based on the great live stream by The Coding Train titled Snowfall . Dan Shiffman from The Coding Train is great at making fun little projects easy and accessible please go and visit his site and if you have time watch the full live stream it will be worth it. I have decided to expand on his project and take it one step further by having a Snowman grow out of my falling snow.
Forces of the Fall
The first step in producing the code above was deciding on how to generate snowflakes. Snowflake generation is a random process which means that each new snowflake will be created at a random horizontal location above the canvas with a random size. The size is limited to be between a minimum and maximum of 1 and 7 pixels, this mimics the effect of having some snowflakes being nearby and others being much further away. This process is repeated until 200 snowflakes are made. If a snowflake is blown off the edge of the canvas it will be recycled and used again at the top with all values randomised once more.
Once the size and position of each snowflake has been generated we need to make the snowflake fall. In the real world how objects fall is determined by gravity. Basic physics tells us that all objects fall at a rate of 9.8(m/s) . Whilst we can mimic the maths behind falling there is one crucial difference between the real world and a computer generated one, time only goes forwards during each frame, i.e. time is discrete. So to simulate our falling snowflake we need to think about time going forwards in steps and not smoothly.
Applying forces in discrete time steps is a common occurrence in computer graphics. The goal is to use the knowledge we have about the applied forces and figure out the position of the snowflake for each frame so that we can draw it. Starting with Newton's Second law of motion \( F=ma \) we can see that the acceleration due to the force of gravity will be \( a=F/m \). If we take the mass of the particle to be related to it's size we now have the acceleration.
Diagram showing the forces being applied to each snowflake. (Snowflake image from Sun Catcher Studio )
Acceleration is the rate of change of velocity so the particles velocity will change and the velocity is rate of change of position so that will change too. If we follow the chain of changes from Newton's Second law we can calculate a new position for the snowflake. This can be seen in the equation below where \(\Delta t\) is the time step, or time between frames. As this is not meant to be a real world simulation we can just say that \(\Delta t\) is 1 so that we don't have to do as many calculations.
$$ Vel_{new} = Vel_{old} + \Delta t * accel $$ $$ Pos_{new} = Pos_{old} + \Delta t * vel $$
The beauty of this system is that to simulate additional forces all that needs to be done is to find the new acceleration with all the forces applied and follow the steps just as before. This means that to simulate the wind all that needs to be done is to add a sideways force to the snowflakes as they fall. These forces are generated randomly using Perlin noise. The advantage of Perlin noise is that it is a type of gradient noise which varies smoothly over time, this means that the wind won't be blowing left in one frame and then right in the next.
An organic surface generated with Perlin noise created by Simon Strandgaard from Kastrup, Denmark using GraphDesignerToolbox. Note how the surface has smooth transitions from one colour to the next.