Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
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"? –  Amplify91 Mar 19 '12 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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.