Running time of broadphase actions

I had a question, probably just for Erin - do you happen to know what the running times of the Box2D broadphase operations are? I'm mainly interested in the asymptotics of additions, removals, and queries.

The reason I'm interested is that the last time I manually put together a broadphase (a simple sweep and prune), I was working on an Asteroids-style game where the player could fire at a fairly high rate, the bullets bounced everywhere, and the asteroids exploded into many chunks upon being hit (hard enough), some of which might even be exploded themselves within the same step. Sometimes quite a few objects get created and destroyed each frame. It turned out that while the algorithm worked quite fast to handle normal simulation, the additions and removals happened often enough that the broadphase was more or less useless, causing visible lags whenever things exploded.

There I used the probably too naive method of starting a newly spawned object at infinity and swapping it into place (or reverse for removals), which should be O(N), though with a somewhat high constant since every other swap causes pairs to be added and removed. I've seen people comment on stabbing counts, which I'm pretty sure Box2D uses, to accelerate these actions - if this is the case, do you know a good reference or explanation of what's going on there? I'm curious as to whether replacing my old broadphase with Box2D's might be helpful, but I'd like to better understand the details as well.
The idea of the stabbing count is to enable the use of a binary search for additions and removals. Ideally this leads to O(logN) running time instead of O(N). I learned about the stabbing count from Gino van den Bergen's book http://www.dtecta.com. Using the stabbing count tends to limit the distance you must search through the end-point array to find the overlapped pairs.

The stabbing count method falls apart if you have some big shapes in the SAP structure and the performance returns to O(N). Some people have proposed using so-called markers and/or batching of additions/removals.
Looked at my original code, discovered that my problems are ultimately unavoidable even with stabbing counts due to my use of arrays instead of linked lists in the sorted endpoint list - an insert to an array works out to O(N) just about any way you cut it...I considered that before I started the thing, but I thought overall speed would be better with an array because of memory proximity. Of course, since I'm stupid, I did all the physics before I decided that the game was going to be so heavy on the inserts and deletions.

I might just swap in b2BroadPhase, then, as I have no large objects at all. The only reason I didn't use Box2D to begin with was that my stuff moves fast enough that I need swept circle-circle tests, so if I don't get around to it before CCD is done, I may swap the physics altogether.
Ah right. There is a memcpy that is O(N) no matter what. However these can be fairly fast. I think a batch add/remove would help you out the most.
Have you tried using a simple grid? For 2D it seems like the added complexity of SAP might not be worth it.
I have used grids in the past. Even with hashing, I found that grids required a lot of memory. Another problem is that you need to pick a cell size, which introduces another tuning parameter for the user. You can view SAP as a dynamic grid that automatically picks the cell size.

Unfortunately there are no comprehensive articles out there on broad-phase collision that compare the timings of various algorithms. Much of what I've heard on the matter is subjective or without hard data.

There are many possible broad-phase algorithms:
- quadtree
- loose quadtree
- SAP
- dynamic AABB trees
- dynamic sphere trees
- simple grids
- hashed grids
- hierarchical hashed grids

There are also several operations to consider
- addition
- removal
- pair management
- volume queries
- movement
- ray casts

I think it would make a great master's thesis for someone to compare all these methods and operations.
I did consider gridding, I've used it before successfully: I find grid approaches to be the simplest to code, and they perform well (about the same as SAP for collision queries if you use them right, with "immediate" adds and deletes - again, purely anecdotal evidence) but problems arise when you have large scale differences between objects. This can lead to huge memory usage even with a good hash because large objects cut across way too many cells. Worse, when they move, all these cells need to be updated. At the other end, small objects pack tightly enough that you get a bit of O(N^2) behavior (not asymptotically, of course), so that's bad, too. Multilevel gridding algorithms attempt to handle this problem, but once you've gone that route, you've moved away from simplicity quite a bit, so it's a tough call. All depends on what you're using it for, I guess.

One difficulty with rigorously testing all of these algorithms is that everyone has their pet preferences for what data structures and implementations to use in each one - linked lists vs. dynamic arrays and all that fun stuff. A poorly designed SAP will always be put to shame by a carefully optimized but simple grid, so it would be a fairly major piece of work to test all the options. I'd LOVE to see the results, though, even in a few simple cases.

Original thread: https://box2d.org/forum/viewtopic.php?t=95