Performance Questions

Apr 20, 2010 at 10:09 PM


I'm thinking of writing a 2d platform game that's along the lines of Sonic.  I figured it would be nice to have some realistic physics in there and things.  What's the best way of handling this if I use a 2d tiled map?  The easiest way would be to add a body to the world for each tile but with a world 200x200 that's a lot of bodies.  Does anyone have any suggestions?


Apr 21, 2010 at 1:50 AM

Well for starters I would definitely use Farseer Physics 3.0. The new version is much faster then the old and would be far more able to handle what your asking for.

All your static geometry can be attached to a single static Body. Then as the player moves I would either add/remove Fixtures (geometry) or disable them. FP 3.x is designed to allow fast adding and removal of Fixtures provided they have been pre-created.

Dynamic bodies would need to be disabled right before the static fixtures are removed and enabled right after the static fixtures are added. This would prevent them from falling through the level.

Doing this would actually also kill 2 birds with one stone. As long as you only remove/ add and disable right pass the edge of the cameras view all your object culling would be done for you. Only draw the static fixtures that are currently added and only draw dynamic bodies that aren't disabled.

There are also some down sides to this approach. If your sonic character runs through some boxes and knocks them up into the air and runs away before they land then when he returns they will start falling again. The fix for this is to keep the static bodies in the simulation until the dynamic ones come to rest. However this could add significant processing demands to the physics engine.

Let me know if this helps.