For Kobold Kit (the Sprite Kit game engine) we’re working on Super Stick Spy, a 2D platformer game. Like so many others, we started out using the (Box2D) physics engine that’s so neatly integrated in Sprite Kit to get everything up and running quickly.
But we knew full well that for the final product, we’ll have to scrap the physics engine altogether and follow best practices when it comes to platformer-programming.
Now with the demo version nearing completion (see video) I can tell you in full detail why you don’t want to use a physics engine for a 2D platformer!
Moving Platform Hell
A moving platform with physics needs to be a dynamic body. Don’t even try moving static bodies, at least in Box2D that will end up in jumpy movement of the body. Though kinematic bodies work better (if available).
The player or other game characters standing on a moving physics body will have the platform slide underneath their feet. The characters won’t magically move along with the platform! And there is no feature in the physics engine that lets you set this up. You have to program it to synchronize character movement with the platform they’re currently standing on, and end the synchronization as soon as a character lifts its feet up from the platform.
Which can be a problem for downward-moving platforms as the player loses contact with the platform every other frame, starts falling, and lands right back on the platform. To put it in Homer’s words: “Doh, doh, doh, doh, doh, doh, doh …”. So you need to make the character stick to the platform, yet allow him to fall off of the ledges and jump, and possibly also allow him to be forced off the ground by normal collision events (projectile impact, platform moving through a tiny crevice). Continue reading »
You have a tilemap and you want physics collisions on it? The solution seems obvious: create a rectangle shape for every blocking tile.
But ouch! This solution is not just hugely wasteful and unnecessarily slows down the physics collision code, it also introduces the well known problem of characters getting stuck even on flat surfaces.
This is in particular a problem for Box2D because its collision mechanic doesn’t work well with flat surfaces subdivided into smaller segments (rectangle shapes in this case).
A workable but still very awkward solution to work around this behavior is to create characters with bevelled edges at the character shape’s bottom at the risk of bopping characters as they walk about the map.
Lupines in the Moore Neighborhood
A good solution to generate physics collisions is to implement the Moore Neighborhood algorithm to generate chain shapes which are more suitable for tilemap collisions. The downside is that adding or removing individual blocking tiles at runtime requires updating the shapes - this is not implemented in this project.
Every flat surface, no matter how many tiles form the surface, will then consist of only one straight collision segment. Here’s a quick demo video of the project discussed in this post that shows the algorithm at work and the resulting “game”:
Learn cocos2d 2 is hot off the press and it once again entered Apress’ weekly bestseller chart (see banner on far right).
Now with iOS 6 released it’s about time I updated my Affiliate Products page.
Because there have been some awesome new products released. In order of most recently released:
iOS 6 by Tutorials
Ray Wenderlich and his team wasted no time in releasing this massive 1,500 pages tutorial double-whopper that’s actually a 5-star iOS 6 gourmet menu!
Commander Cool Game Starter Kit
Create your own platformer game for iOS and Mac with this game kit made with Cocos2D. As always with CartoonSmart products, you get an hour-long video documentation. Jump, run and be cool!
Paralaxer: Cross-Platform Platformer Kit
Paralaxer is possibly the first commercial game kit available that uses Cocos2D-X. Ported to iOS, Android, Windows and Mac it also comes with a free ebook “How to Make a Platformer Game With Cocos2D-X” by Nat Weiss - who previously created the iPhone Action RPG engine.
If you’re working with Cocos2D-X you should spend $49 (limited time) on this excellent starter kit!
It’s a worthy investment, and so far the only really good example code plus documentation resource for Cocos2D-X you can find.