Experimenting with cocos2d + physics engine

Coding real games is the best way to evaluate how useful a game library is (like cocos2d).

I’m really happy with cocos2d, and the way it has evolved, but I think cocos2d needs to have better integration with a physics engine (box2d, chipmunk, or any other physics engine).

And by integration I mean:

  • An easy way to edit a physics world (bodies, shapes, etc.)
  • An easy way to edit visual world (eg: using tile map)
  • An easy way to match the physics world and the visual world

Ideally all these steps should be integrated using just 1 editor.

But before coding 1 editor that integrates physics + visual worlds, I’m exploring the existent alternatives.

For example, the tiled editor is a really good tile map editor (supported since cocos2d v0.8.1), and inkscape can be used to draw physics edges. But is it easy to mix those editors ? What are the alternatives ?

Well, that’s what I’m doing now… I’m building a platform game with the idea to evaluate how easy is to code it using cocos2d.

The 1st step I took was to learn box2d. If you want to test the latest box2d version (from svn) inside the iPhone without cocos2d, use this patch. And if you want to use the latest box2d version with cocos2d, then you should use the latest cocos2d svn revision.

Did you code a platform game using cocos2d ? Would you mind sharing your experience ?

4 Responses to “Experimenting with cocos2d + physics engine”

  • I am working towards a game based on cocos2d 0.8 with Box2D. I switched from v0.7.2 mainly because I had seen some issues with Chipmunk, so wanted to switch to Box2D, which I hear is more mature, stable and widely used.

    Box2D was definitely a great addition to cocos2d, although you have to work through little quirks of using C++ with Objective-C. The API for Box2D is definitely more mature and detailed and more attuned to the needs of the programmer, I would say.

    Now, on their own, both cocos2d Sprites/AtlasSprites and Box2d are awesome APIs. But there is always this need for bridging the two and make them work together. It would be nice to see Box2D well integrated with Sprites.

    For example, if I have a set of physical bodies with arbitrary shapes attached to it, the following work is needed:
    - define Sprite textures that are identical in dimensions to the shapes.
    - move/rotate the sprites as and when the bodies move/rotate.
    - (more specific) keep track of the center of gravity of a body so that the Sprite can be positioned/rotated around the center of gravity, and not just around the texture image's center.

    If these correlations and others can be offloaded a bit, that will lead to a closer integration of cocos2d and Box2d.

  • syncing movement between box2d (or chipmunk) and cocos2d is more or less easy.

    If you want to sync the cocos2d objects based on the box2d objects, you need to query the objects, and then update the cocos2d properties (box2dtest.m shows how to do it).

    If you want to update the box2d objects based on the cocos2d objects, you might want to create a Box2dSprite that overrides setPosition and setRotation.

    center of gravity can be changed by using [sprite setAnchorPoint:ccp(x,y)];

  • I've been building a game using Box2D for active objects and tilemapatlas for the fully destructible ground/envrionment. I agree with the sprite size != boxee object size issue, and have wrestled with that myself.

    So far it is working very well. I haven't checked the integration recently, but I had to do a little bit of framework stuff to make the Box2D objects integrate well with cocos2d objects, but now it is running smoothly and it's easy to add new objects!

  • Use Box2D + Chipmunk (v5) + SpaceManager.

Leave a Reply