I've been using jbox2d for a while and I've made several attempts to dig into the source and try to learn a thing or two, and while I have a good handle on physics and collision detection there are a lot of things that escape me, right now the most obtuse things to me are the proxies. Googling hasn't helped me too much, when I search stabbing count I just get hits about stabbings and counts of murder.
How do I learn about the methods box2d uses?
General DiscussionThe proxies and stabbing count are involved in the broadphase. Very roughly speaking, the purpose of the broadphase is to take a bunch of rectangles in 2d space and figure out which ones overlap. The proxies are stand-ins to represent these rectangles, and the stabbing counts keep track of overlaps on each axis.
Now, as to how this all actually works in detail, Erin would know a lot better than I do - I'll be honest, even after porting and debugging it, I never quite understood the entire flow of logic through the broadphase (a lot of it is implementation details, maintaining the internal data structures by-hand). Maybe he knows of a good reference that describes the specific method? A lot of this stuff isn't well documented on the Internet, but is described in detail in collision detection books.
IMO, it's probably better to learn about the sweep and prune method in general rather than worrying about the details of how it was implemented in Box2d (FWIW, it's not used anymore in current versions, having been replaced by a dynamic tree). The gist of the method is that you keep a sorted list of rectangle beginning and ending points on each axis, and as things move, you just swap the edges in the list and update the data structures to account for these swaps. With a sorted list of edges and some overlap details stored away, it's a relatively simple (and fast) matter to figure out which rectangles overlap.
The trick is to get it working correctly and to get it optimized, and that's where most of the complexity in the code comes in.
Now, as to how this all actually works in detail, Erin would know a lot better than I do - I'll be honest, even after porting and debugging it, I never quite understood the entire flow of logic through the broadphase (a lot of it is implementation details, maintaining the internal data structures by-hand). Maybe he knows of a good reference that describes the specific method? A lot of this stuff isn't well documented on the Internet, but is described in detail in collision detection books.
IMO, it's probably better to learn about the sweep and prune method in general rather than worrying about the details of how it was implemented in Box2d (FWIW, it's not used anymore in current versions, having been replaced by a dynamic tree). The gist of the method is that you keep a sorted list of rectangle beginning and ending points on each axis, and as things move, you just swap the edges in the list and update the data structures to account for these swaps. With a sorted list of edges and some overlap details stored away, it's a relatively simple (and fast) matter to figure out which rectangles overlap.
The trick is to get it working correctly and to get it optimized, and that's where most of the complexity in the code comes in.
My implementation of SAP follows Gino van den Bergen's book on collision detection, stabbing numbers and all. I too was never happy about the complexity of SAP. It is also a poor structure for ray casts.
It's a shame, really, that for a method that's so conceptually simple (at least at its core) SAP is so complicated to actually implement and optimize. When first starting with physics programming that was the first broadphase I tried to write, but I quickly scrapped it in favor of a grid because I never could get it to work fast enough while still being correct.
Of course, even though it was complex, the SAP in Box2d works/worked quite well (aside from raycasts).
@fictiveLaark: if you're interested in the more recent methods and you're better versed in Java than C++, toucansam has ported the dynamic tree broadphase to Java, you can take a look at http://code.google.com/p/jbox2d/source/ ... cTree.java
Of course, even though it was complex, the SAP in Box2d works/worked quite well (aside from raycasts).
@fictiveLaark: if you're interested in the more recent methods and you're better versed in Java than C++, toucansam has ported the dynamic tree broadphase to Java, you can take a look at http://code.google.com/p/jbox2d/source/ ... cTree.java
Original thread: https://box2d.org/forum/viewtopic.php?t=5379