The first version of the inverted pendulum demo was just a hack, with messy code to show that the PID controller can work within the box2d and p5.js framework. I’ve rewritten the code again using more clean object oriented patterns. In addition, rather than using pixel space, I’ve used box2d-world space, and setup a camera object to project the box2d space onto the screen, so that the objects fill up the screen.
The camera object can be also used in the future to follow box2d objects in some frame of reference, so that the focused object stays in the middle of the screen while the universe moves around.
Also incorporated the device orientation effects, if the device supports it, so that the pendulum and all simulation objects follow the direction of actual gravity of the smartphone device viewing the demo.
Link to the new version here.