I got the scooter (mostly) working today. The scooter is my game’s answer to the morph ball, a way to get into tight passages. It’s kind of a little powered wheel that will pull you along as you crouch way down, helped by some zero-friction knee pads I guess, probably. Luckily, it’s easy to science-fiction your way out of this kind of issue. Also, there’s going to be some handy little bombs you can drop while rolling along. Here’s what it looks like.
After implementing a (mostly) working solution, I understand the wisdom of just rolling your main character up into a one-tile-wide sphere, despite that being physically impossible. The main difficulty with having your main character suddenly lie down or stand back up is that you have to change the shape of their simulated body in the Box2D physics engine. There are two ways to do this, both of which have drawbacks.
- Destroy the Body and recreate it in the same place but with a different shape. Don’t forget to register all of your collision handlers on this new object, and make sure none of them fire incorrectly on the first simulation time step!
- Get the shape of the body and tweak it. This will cause you to pass right through solid walls if you’re not careful about it.
This latter problem is caused, as an educated guess, by Box2D incorrectly caching the collision data for a body after its shape has been altered, but I wouldn’t bet money on that analysis. Changing the shape of a body in Box2D is surrounded by all sorts of caveats in the documentation and online, which is why most people recommend destroying and replacing it instead. But as long as you’re not changing the number of vertices in the shape, you should be (mostly) fine.
In my case, I just needed to nudge the character away from the wall before changing the shape. This problem was exacerbated by the fact that I’m doing a translation as well as a shape change, so the Body ended up farther inside the wall than it would have otherwise.
Now I just need to solve the opposite problem — not allowing standing back up when the space overhead is too tight.