I am working on an iOS endless-running game (Robot Unicorn, Canabalt, etc). I'm using box2d for the Physics and Collision Checking and cocos2d for graphics.

To achieve the notion of an endless level, I instantiate "boards" which encapsulate a set of box2d bodies (floors, enemies, etc). I place two of these "boards" alongside and I check for their current position every frame. When they leave the visible area of the iPhone screen, I move the board and it's set of bodies to a new position, further ahead.

To do this I use the SetTransform() method of the b2Body class.

I also put all my bodies to sleep before doing this.

My issue is that every time I perform this task, the FPS has a noticeable drop.

I ran the Profiler and it seems the problem is within the SetTransform(). Apparently, box2d is removing and inserting leafs in the dynamic tree.

I am moving around 50 bodies in one frame.

My question to you guys is if anyone knows of a more convenient way to translate bodies without it taking a toll in performance. Perhaps it's cheaper to create new bodies, instead of moving the existing ones? Am I missing some kind of flag I should activate before calling SetTransform()?

Thanks in advance!

  • \$\begingroup\$ Besides the drop in framerate, has this worked well for what you are trying to do? Do you have any problems with moving objects (like the runner) smoothly between "boards"? \$\endgroup\$
    – Amplify91
    Mar 19, 2012 at 16:27

1 Answer 1


In a more general point of view (ie. not talking about box2d), you could double the amount of objects present in the scene (if you want to display one enemy, you should actually have two of them instantiated). Once the first got out of the screen, enable the second one (which will then enter the screen), and recreate the first one (hidden, then) in the background. That way, although theoretically slower in average, permits to keep a constant framerate.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .