This project is read-only.

Body.SetActive() getting expensive

Jun 20, 2010 at 5:37 PM

I've noticed recently that calling Body.SetActive() in my game is getting expensive - expensive enough that it is causing visible delays.

Doing some digging, I think I understand why. My scene is composed of a large number (thousands) of static objects - mostly edges. Box2D handles the scene very well - it scales beautifully with the complexity of the scene. Unfortunately, it looks like a side effect of the structures used to make things scale nicely make it expensive to modify large scenes on the fly.

I'm going to look into modifying the code to see if setting the body's Active flag without doing the other aspects of deactivating it works - if so, I can always have it in the simulation, but flip it's active bit at will.

Interestingly, only activating a body seems to be expensive - SetActive(false) is no problem.

Any suggestions on things that I can try without having to maintain forked code would be much appreciated.

Jun 21, 2010 at 8:25 PM
More details on this. The expensive part is re-balancing the tree structure used for broad-phase collision. This is done every time a body is added to the system. I added a flag to suppress the re-balancing so that I can add multiple bodies at once and only get the re-balance hit once. Works great, and also took a huge delay out of my game startup when I add all of my scene objects to the physics simulator. I'm still not sure why the re-balancing is so expensive, and why it mostly seems to be an issue on the Xbox.
Jun 21, 2010 at 8:33 PM
A bit more digging. Turns out to be death by recursion when the tree size is relativately large. I think this also explains why it is more of a problem on the Xbox.
Jul 6, 2010 at 3:14 AM

Recent changes made some changes to the dynamic tree.  They may have improved the perf.  It looks like Erin (from the C++ version) is actively working in this area, so expect good things to come!