Double Pendulemon

Apr 2019
HTMLCSSJavaScript

In first semester of uni I did one Physics course as a science elective. The course had a module on "chaos theory" that touched on the unpredictability of double pendulums, which inspired me to try to create my own from scratch.

Technology

Stack

At the low level, the simulation was built with raw JavaScript using my own UI library canvasLib.

Spring-Mass System

At a high level, the simulation uses a spring-mass system that has had it's parameters finely tuned to approximately maintain the total energy of the system over time while still permitting realtime simulation.

The pivot point and both lemons are particles with the pivot point also having its position fixed. Two fairly rigid springs are used to connect the particles in a line to produce the double pendulum. If you zoomed in, you would actually see the pendulum stretching and compressing tiny amounts, but the springs are rigid enough that this is not perceptible to the human eye.

You would be surprised just how carefully the parameters must be tweaked to get this to work. If the springs are not rigid enough it starts stretching and bouncing all over the place and looks nothing like a pendulum. If they are too rigid, the forces will be too great and the simulation will quickly spiral out of control as kinetic energy approximation errors build up until the lemons fly out of bounds.

Maybe one day I will embed examples of the above to demonstrate the point bc it's kinda funny.

A section for spring-mass appreciation

IMO spring-mass simulations are massively underrated as programming primitive as they are incredibly easy to make and can quickly produce really impressive behaviours.

A high school understanding of Newton's laws of motions and the equations that govern attractive/repulsive forces between particles/bodies is all you need to create basic simulations including but not limited to:

  • Softbodies (i.e. squishy blobs)
  • Ropes
  • Fabric/cloth sheets
  • And pendulums of the nth degree!
A meme

Personally, I had always assumed that the math underlying softbodies was necessarily a superset and thus far more complex than rigidbodies. And arbitrary rigidbody collision handling was already complex enough, so softbodies seemed out of reach without some serious dedication. Well turns out it's actually the opposite and softbodies are 10x easier and quicker to create.

So go make one!