Chipmunk 2D Physics on the playdate


Chipmunk2D is reasonably easy to get building for the playdate, because it's a set of cross platform C code, so plays nicely with the playdate C SDK. It's also small enough that you can just drag the Chipmunk2D code base into your playdate solutions wholesale, if like me, you are too lazy to build it to a .lib or .so file.

Messing around with this 2d physics simulation I've discovered a few obvious things, and a couple of playdate specific things along the way.

Obvious Physics Things

- Only a simulation: You'll likely have to mess around with mass, moments, scales, etc. to get something that 'feels good' on screen. This means expect some fiddling around with even the simplest physics engine.

- Test, test, test and test some more: Related to the fact it's only a simulation, make small changes to your physics code and test each change thoroughly, and particularly on the hardware if you have a playdate. There's nothing worse than the physics suddenly going crazy after you've spend days refactoring your code, potentially leading you down a dark and confusing rabbit hole.

- Drill through hurts: This is a common physics engine problem, even for frameworks like Unity. Drill through is when your physics bodies pass through each other, or pass through walls and can do fun stuff like escape your play area, or get trapped inside walls in your game. 

Avoid small bodies travelling very quickly because this is a recipe for drill through. Small bodies travelling fast equals bullets in many games - resist the urge to use physics bodies for bullets! Use ray casting against bodies instead if you have to, or don't use a physics engine at all.

- Chipmunk2d examples are excellent: There's around 24 of them and they showcase all sorts of useful features of the physics engine. Definitely examine the ones that interest you in detail. They're also handy for quietly borrowing initialization code from too as many
of them set up interesting spaces and body shapes. 

Playdate Specific Physics Things

- Build Chipmunk2D for float support only: The playdate Arm M7 Cortex CPU doesn't have hardware support for doubles so make sure to #undef CP_USE_DOUBLES and make sure cpFloat type ends up as a float in your code and IDE not a double. 

Also when you're compiling your code pay attention to any warnings about float/double type conversion. This can mean something is wrong with your types, either in Chipmunk2D or your own code.

Avoid GCC's -ffast-math option: It leads to Chipmunk2D crashing in the playdate simulator and the hardware device. This probably isn't a surprise when you read what the GCC 'fast-math' option actually does: 

"In addition GCC offers the -ffast-math flag which is a shortcut for several options, presenting the least conforming but fastest math mode. It enables -fno-trapping-math, -funsafe-math-optimizations, -ffinite-math-only, -fno-errno-math, -fno-signaling-nans, -fno-rounding-math, -fcx-limited-range and -fno-signed-zeros."

Sounds a little too exciting, and it definitely causes issues with Chipmunk2D.

Get Coin Chaos

Leave a comment

Log in with itch.io to leave a comment.