Convex shape decomposition

I think a boolean to turn off CCD for select shapes might be wiser than a variable toiSlop. We are only talking about thin correction slivers in otherwise good bodies, so the CCD will stop the simulation a millisecond later anyway when it hits a larger object behind. Then the next frame, it would collide correctly with the small shape. While not appropriate for some circumstances, I could see using bodies that "give up" some of their finer detail when under stress from fast moving objects. It wouldn't be noticable. On the other hand a variable toiSlop is asking for people to accidentally set it too small, which I gather would be a massive performance hit.
Quote:
I think CCD could be extended to have per shape toiSlop. This would allow for a zero value on one shape. However, CCD wouldn't work if the sum was zero. So you probably should only have zero slop on static shapes.

I think I kind of agree with BorisTheBrave, having another knob to twiddle might not be the best thing to do, so this should probably not be user-adjustable. I might go further, and even argue that adding a flag to disable CCD altogether could be confusing, as you already set things as bullets if you want them to use CCD, and people would confuse the two. But if the polygon constructor check was changed so that it automatically turned down toiSlop for any shape that was too skinny to use the original value (yet still big enough for the regular slop) that could probably work if tuned right - I assume there needs to be some safety distance between linearSlop and toiSlop, so that would have to be factored in. The factor of 8x seems a bit excessive, though, as I would think an 5 cm wide box should be fair game to simulate, and it currently is not.

Don't worry too much about this, though, it's real low priority - it doesn't seem to be causing most people any headaches, and I will still have to deal with the fundamental issue of either letting decompositions fail more gracefully or fixing up the results anyways, so at best it would merely lower the number of edge cases I have to handle, not eliminate them.
Yeah, I could compute the toi slop automatically per shape. The 8x multiplier was tuned to give good performance in extreme cases. It could probably be reduced for many games.
Okay, I just committed 2.0 convex decomposition code to the SVN repo in the Contrib directory. Didn't add it to the projects or anything, but I also committed a basic usage example with the rest of the tests (ConvexDecomposition.h - to run it you'll need to add it to the TestEntries file). Let me know if there are any problems.

I haven't fixed everything with this, and the code is really messy and unorganized, but I wanted to finally get it in there just in case people find it useful as-is; I'm sorry this took so long, I was really hoping to get to a reworking of the utility before putting it in but I just haven't been able to get around to it yet. I fixed up a memory leak, and worked out a few situations that were causing crashes. The issues with some shapes not being very optimally decomposed, and missing little slivers, have not been resolved yet. Also TraceEdge fails in some situations, which will only be resolved by moving to a different method.

I think the route I'm going to take from here is to do a rewrite of this utility in Java, and try to get the rest of the kinks worked out there, including a new triangulator, and many speed improvements. Sorry to those using the C++ engine, this will mean any updates will not hit the main engine for a while, but I'm far more comfortable with Eclipse than any of the free C++ IDEs (esp. on OS X, which I use almost exclusively these days), and if I want to stand a chance of actually improving this, I've got to go with what I'm comfortable with. Once I have things stable there, I (or someone impatient for the code!) can port it over.
I'm trying to run your Testbed demo, ewjordan, but I get a bunch of errors. I'm compiling in Code::Blocks. I added an include and also edited this:

Code:
TestEntry g_testEntries[] =
{
    {"Convex Decomposition", ConvexDecomposition::Create},
   {"Polygon Shapes", PolyShapes::Create},


I'm sure i'm just missing something simple but i'm not very familiar with c++.

Getting a bunch of errors like this:

obj\Release\Examples\TestBed\Tests\TestEntries.o:TestEntries.cpp:(.text$_ZN19ConvexDecompositionC1Ev[ConvexDecomposition::ConvexDecomposition()]+0x2be)||undefined reference to `b2Polygon::~b2Polygon()'|
dc443 wrote:
Getting a bunch of errors like this:

obj\Release\Examples\TestBed\Tests\TestEntries.o:TestEntries.cpp:(.text$_ZN19ConvexDecompositionC1Ev[ConvexDecomposition::ConvexDecomposition()]+0x2be)||undefined reference to `b2Polygon::~b2Polygon()'|

I don't use Code::Blocks, but if I had to guess, the /Contrib/ConvexDecomposition/ cpp and h files are not properly added to the project (I only added the files, I didn't edit the projects; I use too-new versions of XCode and Visual C++ to match the files that are there, and don't have Code::Blocks) and thus are not being linked correctly. I'm not sure how to set this up in Code::Blocks, but in XCode you can just right click on a folder and choose add->existing files, and then as long as the paths are correct it should work (you might also have to add the files to the proper executable). It's something similarly simple in Visual C++, though I forget the exact command.

Or I could have done something wrong, and there's a coding error that XCode and Visual C++ have ignored - let me know if you can't figure it out, I'll try to help track down the problem.
I really hate how I don't quite understand how all the files and linking stuff works for c++ since I have not taken any c/c++ courses yet and until I find a really nice article somewhere to clear it up for me, I'll just be digging around until I get it working somehow.

I just went and got the free VC++ 2008 express edition or whatever, I'll see if it works in that.
I think you were right, I just had to add the files into the project, and then fix a couple of warnings-turned-into-errors (they were all a couple of variables declared but never referenced), and it worked just fine in VC9. Screw code blocks, i'm more comfortable with VC++ anyway.

The convex decomp examples look great! I wonder why you have a couple of round shapes that are decomposed into smaller pieces, they look like theyre already convex to begin with. I guess it's just an example? Does it check if the input polygon is already convex or not?

Couple quick questions: I noticed that if there are small slivers, it will just not put the polygon in there at all... from reading through this thread, is this because of the toiSlop limitation? What exactly is the toiSlop? It's got something to do with the CCD, right?

From my understanding...: the CCD works by scanning bullet objects with other objects for collisions in some special complicated (slow and bad for performance) way to catch instances where it passes though things. In order to reduce the number of CCD checks, Box2D uses another set of polygons that lie inside the actual collision polygons and checks for CCD against these, so that if two colliding objects don't penetrate eachother far enough to hit the internal polygon, no CCD will be needed. And on the TestBed these internal polygons are the "Core Shapes" and what happens is it creates a limitation on how small a polygon can get before the core shape becomes nonexistant.

edit: Oh I know!! it takes the core shapes, and sweeps out an area between the current step and the last one, using the core shape, and then checks for collisions with other core shapes. Then calculates the time of impact (aha! thats what toi stands for) somehow, goes back to the previous step at that time, and resolves the collision.

Please correct me if I'm wrong, I'm just listing all of that because it's a little less obnoxious than asking like 10 questions especially if I already pretty much know the answers.

If I am correct in assuming all of that, why can't we just make it so that we can still have really small convex decomp polygons which do not have a core shape and thus will not trigger CCD, but we don't really care about that anyway since they are so small?

And somewhat related: shouldn't this core shape be the same as the regular shape in "bullet" shapes? So we could have very small bullets, for whatever reason one might need low-caliber bullets. I guess you could just scale up the rest of the sim but I don't like the sound of that.
dc443 wrote:
I think you were right, I just had to add the files into the project, and then fix a couple of warnings-turned-into-errors (they were all a couple of variables declared but never referenced), and it worked just fine in VC9. Screw code blocks, i'm more comfortable with VC++ anyway.

Glad it worked out, and re:IDEs, as much an open source fan as I am, in this arena I think you're right, VC++ is pretty much unsurpassed.

Quote:
The convex decomp examples look great! I wonder why you have a couple of round shapes that are decomposed into smaller pieces, they look like theyre already convex to begin with. I guess it's just an example? Does it check if the input polygon is already convex or not?

Just testing to make sure that the algorithm did a fairly decent job at decomposing convex shapes into a near minimal number of polygons even when you pass them through triangulation - the main reason you'd want to decompose already convex shapes is that there is a maximum number of vertices restriction in the physics engine, and the triangle->polygon phase automatically factors this in.

The existing tests are not great, they're little more than syntax examples, feel free to contribute something more interesting if you've got it!

Quote:
Couple quick questions: I noticed that if there are small slivers, it will just not put the polygon in there at all... from reading through this thread, is this because of the toiSlop limitation? What exactly is the toiSlop? It's got something to do with the CCD, right?

From my understanding...: the CCD works by scanning bullet objects with other objects for collisions in some special complicated (slow and bad for performance) way to catch instances where it passes though things. In order to reduce the number of CCD checks, Box2D uses another set of polygons that lie inside the actual collision polygons and checks for CCD against these, so that if two colliding objects don't penetrate eachother far enough to hit the internal polygon, no CCD will be needed. And on the TestBed these internal polygons are the "Core Shapes" and what happens is it creates a limitation on how small a polygon can get before the core shape becomes nonexistant.

edit: Oh I know!! it takes the core shapes, and sweeps out an area between the current step and the last one, using the core shape, and then checks for collisions with other core shapes. Then calculates the time of impact (aha! thats what toi stands for) somehow, goes back to the previous step at that time, and resolves the collision.

Please correct me if I'm wrong, I'm just listing all of that because it's a little less obnoxious than asking like 10 questions especially if I already pretty much know the answers.

That's essentially correct. CCD handles moving the shapes so that the core shapes are almost touching, which leaves the full shapes overlapping (and thus able to be handled by the normal collision resolution algorithms).

Slivers probably should be fixed up in some way, maybe by either trying to retriangulate without them or by growing them a bit. I'll be taking a look at that for the next version of this code.

Quote:
If I am correct in assuming all of that, why can't we just make it so that we can still have really small convex decomp polygons which do not have a core shape and thus will not trigger CCD, but we don't really care about that anyway since they are so small?

And somewhat related: shouldn't this core shape be the same as the regular shape in "bullet" shapes? So we could have very small bullets, for whatever reason one might need low-caliber bullets. I guess you could just scale up the rest of the sim but I don't like the sound of that.


If you look a little earlier in this thread, Erin seemed willing to consider variable amounts of core shape reduction, so that's definitely a possibility, though there would still be some cutoff. You can't have the core equal to the full shape, because there needs to be some difference so the CCD can leave the full shapes slightly overlapping. But yes, at some point hopefully we can work out a good solution to this so that CCD doesn't place too severe restrictions on geometry.
It seems to me that all the tough parts to get working are already working. Shapes are getting decomposed into convex polygons and they even work correctly with CCD. Excellent work, you guys.

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