Hello,
I am creating a 2D side-scroller and using box2d for the physics/collision. I ran into a problem with exceeding the max pair count. I know I can increment the constant in the settings, however I hope there is a better solution. I noticed box2d creates pairs on static objects. I am using bodies for each tile in the map that needs collision, thus creating copious amounts of 'wasted' pairs. Is there a way to not create the pairs? My maps are split into 64x64 tiles, each containing a physics body. As the maps grow, so do the pair counts.
Any ideas?
Thank you,
-T
Anyone? More information needed?
I'm weighing in only because no one else is. I don't know of any solution to your problem, and haven't seen a variant of the question before. The only thing I can think of is digging into the Box2D internals, and preventing the broad phase from adding a pair to the pair manager if both shapes are static. Come to think of it, I'm not sure why Box2D doesn't already do that, so either:
a) It's not possible for deeper technical reasons
b) It messes up the design scheme too much for inclusion (the broadphase is meant to be independent from most of the engine, all it 'sees' is a bunch of AABBs called proxies).
You're gonna need a second opinion on that.
In the mean time, I suggest you simply increase the limit. It may be costly in memory terms, but I guess that the processor impact is small.
Are your tiles in a grid? If so, then their AABBs shouldn't overlap and create pairs. I'd say that qualifies as a bug.
I just tried a simple solution, altering b2PairManager::Commit to skip all the static-static pairs (just add a proxy -> user data -> shape -> body -> IsStatic test for both the proxies to the IsRemoved test there), and at least in the testbed nothing seems to break, and the static-static pairs don't show up anymore.
Erin, is there a good reason not to do this? I could certainly be missing something, but IIRC bodies are not allowed to turn from dynamic to static or vice versa, so I can't imagine this would hurt correctness. It could affect the cache hit rate, since I'm pretty sure the bodies themselves are never touched during the broadphase currently, and this would dereference a whole bunch of pointers for each add event... Would it maybe be better to clear the pair store of all static pairs after each static body is constructed?
Internal tweaks aside, I'll echo BorisTheBrave's advice, though, and suggest that you just up the number of proxies and pairs - if you're busting the upper limit on pairs, you're probably coming pretty close on the limit of proxies anyways, and the engine could probably use a little more room to breathe.
Could be some problems when you move static shapes with SetXForm, but it's rare anyone actually wants/needs to do that.
According to the manual, it's faster to use multiple shapes inside one body rather than lots of bodies each with only one shape. Can you combine the tiles into one big body in your code?
The broad-phase is not supposed to know about bodies because it is supposed to be usable for things that don't know about bodies. In fact, the whole collision module is not supposed to know about bodies.
I did attempt to have "static" proxies (via a flag). Static-static overlap would not create a pair. But it became difficult to tract down some bugs. I'm fine if someone wants to add this feature, but it must be tested exaustively. In particular, the broad-phase test must run for an hour or so without crashing. All validation code should be turned-on.
Also, in my experience with Box2D so far is that as soon as static proxies are implemented someone will want to make dynamic bodies static and vice-versa during simulation.
Yeah, I think a dynamically growable broad-phase would be a more useful feature.