Previous page : Bouncing balls, the maths
This is a simulation of a Galton Board. One thousand balls are randomly positioned a bit above the funnel on the top of the board. They can then be released, bouncing on a bunch of pegs to finally be collected in one of several compartments at the bottom of the board.
You will have a few buttons to click on:
Start/Pause/Continue: Controls the simulation.
Relaxation steps will be discussed further down the page, but keep it at 6.
Colours selected by Initial Position/Random/Final Position. The latter will give the colours of the balls depending on where they will land.
After a while, a New Balls button will occur. If you click that button the balls will be replaced by a new set of balls.
You will also see the main loop time in ms, the Worker loop time in ms, the actual time the current run has run in ms, and the time according to the worker.
You are also given the average horizontal position of the balls and the standard deviation of the horizontal positions. The board is 1200 units wide in those calculations.
How does this work?
To make it fast enough I used the trick from my other pages where the program has to handle many balls/particles. I split the screen into several compartments. See this.
Secondly, I use several threads. In the main thread, everything that has to do with the DOM is running – it’s also where the balls are created – and all the other objects too
The motion and collisions are calculated in a second thread using a web worker, and at the same time, the same calculations are performed for a new set of balls in a third thread. When those have reached their final destination, their positions are used to figure out the corresponding colours. The starting positions are then used in the main and second threads to calculate the same thing again.
A random set selected from six precalculated sets is used in the first run.
The problem of relaxation
As the balls are pressed into a wall or each other, they need to be pushed back. One ball might then be pushed into another, which in turn has to move, and so on. This can create almost explosive behaviours of the balls. To mitigate this the program will perform six motion/relaxation steps after it has moved all the balls and checked collisions with fixed objects. In each particle reaction, the balls bounce, but they are also moved back 95% of the distance necessary for the particles to not overlap.
This is done six times in a row before a new set of ordinary motion steps is calculated. You can click on the 6 after “Relaxation steps:” to test how it would look with fewer collision/relaxation steps.
Up a level : Fractals and ChaosPrevious page : Bouncing balls, the mathsLast modified: