Click on the picture above to add an object.

# A silly contraption using a JavaScript physics engine

The experiment you're seeing above may be completely pointless, but it's an interesting 15-minutes coding exercise resulting in something fun to watch for 15 seconds.

If you're still reading this, you must have decided this ratio is acceptable, so let's analyze the code.

We're using the great Matter.js physics engine. While the code below is pretty much self-explanatory, you can read about the basic setup if you want more details. Lines [1-20] are just the standard boilerplate for Matter.js.
[26-45] the main animation functions that spins the rectangles [21-26] and moves the shelves up [28-32] or down [33-36] if it reached the top.

[29-30] determine the direction of the spin (clockwise vs counterclockwise). It would have been simpler to do just `if (i < rectangles.length / 2)`, but that won't work if you increase the number of rows of spinning rectangles (try changing 4 to 6 in [65]).
[47-55] add a new circle or square (50% chance each) if the screen is clicked.
[57-91] create the world:
[57-64] the horizontal shelves
[65-74] the spinning rectangles
[79-83] the tile that allows the objects to slide onto the shelves
[84-86] the tall vertical wall
[87-89] the slanted tile that pushes the objects off the shelf



Check out these programming tutorials:

JavaScript:

Optical illusion (18 lines)

Spinning squares - visual effect (25 lines)

Oldschool fire effect (20 lines)

Fireworks (60 lines)

Animated fractal (32 lines)

Physics engine for beginners

Physics engine - interactive sandbox

Starfield (21 lines)

Yin Yang with a twist (4 circles and 20 lines)

Tile map editor (70 lines)

Sine scroller (30 lines)

Interactive animated sprites

Your first program in JavaScript: you need 5 minutes and a notepad

Fractals in Excel

Python in Blender 3d:

Domino effect (10 lines)

Wrecking ball effect (14 lines)

3d fractal in Blender Python