Wednesday, September 2, 2015

the phun of physics, the nature of code

I mentioned my efforts to retool my old Processing/Java applet games and toys for the modern web. Previously I discussed patching the sound playing, but lately I took on what I think will be the toughest challenge: the few games that used physics.

Back then I used JM Couillard's pphys2d library. It had a lovely set of examples and friendly syntax. Unfortunately, Processing updates a few year back rendered it inoperable, even in Java mode.

For these games I've decided to use one of the many variants of "box2d". The learning curve is a bit steep! The best introduction I've found for it is The Nature of Code by Daniel Shiffman. The whole book is a delight. (His code is so elegant it makes everything look easy... highly recommended.)

He published versions of the source code using box2d-html and p5.js. For the physics games I'm going to recreate from scratch (more or less) in p5, rather than the other games where I aim for a version that works in both Processing java and .js modes -- I might come back and see if i can make some kind of patch for pphys2d calls once I get a more solid feel for box2d. (Shiffman is a bit misleading... it's all not that easy!)

Anyway, here is my Hello, World-ish program I'm linking here for my own reference. (Here's a tarball with all 5 files: beebox.tar.gz) It's almost 100% just the Chapter 5, Example 2 from Shiffman redone, except I tweaked the createWorld() helper to take an argument to disable gravity, moved some files around, and then add a bee/ball character that tries to chase the mouse a bit, but is hit by the box. Unlike the original, this may well clog up your system after running for a while, since boxes are less likely to fall into no-need-to-compute irrelevance. Also, I realized that this example skimps by not explicitly setting the bee's fixture's attributes (density, friction, restitution), so look at the "box" code for that.

No comments:

Post a Comment