Engineering gets easier every year because of all of the amazing tools that become available. Years ago, if you wanted to do a good physics-based simulation, that meant months of development work and probably several headache-inducing conversations with specialists about forces, free-body diagrams, vector calculus, and other topics.
Today, modern physics simulations exist for little or no cost* — and they’re getting easier to use all the time. I’ve recently started working with Unity — a development environment primarily aimed at game development, but which has many generally useful capabilities.
Most importantly for my purposes, Unity has an excellent, easy-to-use physics simulation package built in. Here is a video of a quick demo I wrote in the course of an evening while teaching myself the basics of Unity.
Such a simulation requires an insane amount of computing power. For each block generated (Unity generates a new one with each video frame), forces from each collision must be calculated and applied to the relevant objects. Simply modeling one cube falling while not perpendicular to the table would be challenging enough — you would need to calculate the time and position of first impact, adjust the time of the simulation accordingly, and predict forwards from that until the next collision.
Fortunately, Unity makes all of that very easy (for the programmer). With a few lines of code, objects are instantiated and set into motion. The built-in collision detection and physics routines and the 3D renderer handle the rest.
Here is the code to generate a brick. This command is automatically called at the start of every frame:
Instantiate(Brick, new Vector3(8 * Random.value - 4, 10, 8 * Random.value - 4), Quaternion.identity);
This code instantiates (creates a working copy of) a Brick object, at 10 units above the table and somewhere from (-4-,4) and (4,4) on the X/Z plane. So each frame, a brick will appear above the table within four units left-right and within four units forwards-backwards from the origin point at the center of the table.
From here, Unity’s magic takes over. A few more lines assign the size and color of the brick and give it a random 3D rotation. From there, the physics engine handles things completely automatically.
A “cull” script does remove any objects which fall off the table, to keep the overall number of objects limited to a finite number.
Next stop, powered hinge joints and robotics simulations!
* Unity has a very friendly pricing model — completely free for individual use and organizations under $100k in funding or annual income; $25/month for hobbyists and small businesses, and $125/month for larger ones.