About Box2D and possible improvements

Hello all! I am new to this form and Box2D... which seams so far to be a Godsend. I am working on a 2D game engine and have been for a while. At first, I tried to do my own physics, which was coming allong well, but I got burnt out and finally relised how much work it really is to make a good physics engine (more than I wanted to "re-invent"). So, I started looking for good physics engines out there... after going through a few, I finnally found Box2D, and I must say I am pretty excited to start using it.

I have noticed a few things reading the documentation however, and I have a few questions about Box2D:

1) In the docs, under the "bullet" section, it says Box2D will "sweep" objects if they are in bullet mode. Does this mean that Box2D will actually test the object multiple times along different positions until it reaches the destination?

2) What collision detection method does Box2D use? Is it simple "AABB interesect -> Do more complicated testing -> Get contact information -> Send evernts", or is it mathimatically based? The reason I ask the above two questions is because I ran across this guy on the GameDev.net forms that was really bright, and he had this idea (I don't know if it is his) on how to do collision detection extremely efficiently where the objects can have any number of sides (but must still be convex), and have a velocity vector of ANY speed and still acurately report collisions. It is a great way to handle collisions because it is so simple and because the objects can be traveling at insane speeds and still acurately report collision with any object, including a line (no tunneling). Also, because of the nature of the process, you automatically get resolution normals, and easily get contact points.

I am posting the zip file with the documentation, source, and GLUT examples that he gave me. If this would be benifical to Box2D it should be fairly easy to implement. I just was wondering whats going on because I don't know, and was looking for feedback on my ideas.

Here is the file: http://uk.geocities.com/olivier_rebellion/Polycolly.zip

The collision process (incase you don't want to download the zip):
Basically, all you do is "flatten" the two polygons you are testing against, and see of the "flattened" polygons intersect based on there "projected" positions. This is acomplished with very simple fast math, and, as I said above, works with extreme volocities WITH OUT SWEEPING, because all you do is include the objects velocities into the equation, and the equation basically EXTRUDES the objects along the velocity vector.
th317erd wrote:
1) In the docs, under the "bullet" section, it says Box2D will "sweep" objects if they are in bullet mode. Does this mean that Box2D will actually test the object multiple times along different positions until it reaches the destination?

Nope, it's even better - Box2d will actually find the time of impact (or at least do its best to find it), which is a bit better than discrete sampling along the path.

Quote:
2) What collision detection method does Box2D use? Is it simple "AABB interesect -> Do more complicated testing -> Get contact information -> Send evernts", or is it mathimatically based? The reason I ask the above two questions is because I ran across this guy on the GameDev.net forms that was really bright, and he had this idea (I don't know if it is his) on how to do collision detection extremely efficiently where the objects can have any number of sides (but must still be convex), and have a velocity vector of ANY speed and still acurately report collisions. It is a great way to handle collisions because it is so simple and because the objects can be traveling at insane speeds and still acurately report collision with any object, including a line (no tunneling). Also, because of the nature of the process, you automatically get resolution normals, and easily get contact points.

Basically, for CCD at least, the steps are, for each pair (though Box2d doesn't check each pair, it uses a broadphase to figure out which ones matter):

1) See if the swept AABBs overlap
2) If so, get a conservative TOI (time of impact) estimate based on the velocities, angular velocities, and minimum distance between the objects
3) Advance both bodies to that TOI
4) Repeat 2) until bodies are touching (within some tolerance)
5) Generate contacts
6) Pass to the usual (non-CCD) contact solver to resolve the collision
7) Repeat until we've completed the entire time step

Quote:
The collision process (incase you don't want to download the zip):
Basically, all you do is "flatten" the two polygons you are testing against, and see of the "flattened" polygons intersect based on there "projected" positions. This is acomplished with very simple fast math, and, as I said above, works with extreme volocities WITH OUT SWEEPING, because all you do is include the objects velocities into the equation, and the equation basically EXTRUDES the objects along the velocity vector.

Whether or not that is better than Box2d's method depends a lot on the performance, which I'm not sure about without seeing it in action. Box2d will handle extreme velocities quite nicely, and doesn't suffer too much from the sweeping (all that it actually sweeps geometrically is the AABB, not the shape itself - look up "conservative advancement" for more detail on that), and the iterative bit only usually goes through a few iterations, so it's pretty snappy.

One problem I notice about the method, at least as explained in the docs, is the following:
Quote:
Sorting collisions. By that I mean, it might not process collisions in order. This can be a problem for very fast objects. Once a collision is detected, it is processed straight away. Ideally, you’d look for the first collision, process it, and then look for some more. But for a 2D action game, this is usually overkill.

This is minor, but it could lead to problematic events like being able to shoot an object on the "wrong" side of a supposedly impenetrable wall if the collisions were processed in the wrong order, and situations like that are actually fairly common in real uses of CCD. Box2d will always handle things in the proper order, so that could never happen.
Cool, that makes sense. Why is it then that I've already had objects pass through other objects at high velocity?
th317erd wrote:
Cool, that makes sense. Why is it then that I've already had objects pass through other objects at high velocity?

One possibility is that the objects were not marked as bullets - because CCD is expensive (compared to normal collision handling) it needs to explicitly be turned on for fast moving objects, though it happens automatically when testing against static scenery.

If you've done that and you're still seeing missed collisions, please come up with a reproducible example and submit it as a bug, as that shouldn't be happening. FWIW, I've put the engine through a lot of stress tests, and the few situations where CCD fails are usually very extreme and contrived, usually involving insane velocities or highly constrained situations (if you put a ball inside a moving box, both with restitutions greater than 1, the ball will eventually escape) so I'd guess that the most likely case is that your objects are not flagged to use CCD (the isBullet property in the b2BodyDef must be set to true to use CCD).
The other possibility for objects moving through each other with CCD, is that two fast moving objects that cross the same space at the same time will only really cross the same space if more than just one of their motions are considered for the collision detection. I suspect that this is too expensive.
pTymN wrote:
The other possibility for objects moving through each other with CCD, is that two fast moving objects that cross the same space at the same time will only really cross the same space if more than just one of their motions are considered for the collision detection. I suspect that this is too expensive.

No, Box2d (and conservative advancement in general) should handle that type of situation correctly as well, and has a benefit over a naive sweep test in that it will also avoid false positives where the sweeps overlap but the bodies actually miss.

As far as I know the only failure modes of the CCD in Box2d are a) when the bodies rotate so much in a frame that the swept aabb is wrong or the linearization of angular motion is a bad approximation, and b) when the substeps get so tiny that we give up to avoid near infinite loops.
Cool. I didn't know that.

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