Raster-to-b2PolyDef

I was thinking about writing an algorithm that would convert a a strictly black and white .gif image into a b2Poly (or rather, several). I was happily coding away for a few minutes until I realized my naive algorithm would never work.

The idea was to find the first black pixel (moving from left to right, top to bottom) and then call a recursive function that would trace around the black blob. I was going to simply move left until I hit a white pixel, and if I couldn't move left, I'd move down, then right, then up... and hopefully it would eventually get back to the start. However, there are two problems with this... (1) It would end up filling in the shape, which we don't want to do, and (2) Box2D only excepts convex shapes (for now).

If I were to only draw convex shapes... does anyone know how we might trace them? I'm sure other people would find this useful. Would be an easy way to create maps for games.
Google "potrace", that is probably the best FOSS for tracing, the source is available and the author explains his method. It outputs splines as svgs but you can either retrofit the algo to strictly use polygons or use the spline output and reduce it to polys.
Thanks. I've been working on a tool that lets you load up an image as the background so that you can trace over it in the meantime. It works fairly well, but damn those b2Poly's are finicky!

Image
How did you do it? I've been trying to do the same for a while now.



Thanks.
I think, better create polygon by points. Use mouse-click on the picture to get data. Save the polygon to a script file.
Reaper wrote:
How did you do it? I've been trying to do the same for a while now.



Thanks.


Uh...that's really old. Loading an image you should be able to figure out on your own, or you'll never be able to make a game. To create the polygons... just push each mouse click onto an stl vector, and then when you close the poly, decompose it into convex regions with one of the many algorithms on these forums. Pretty simple :p
Why on earth would you not use edge shapes for this? I've found them to be ideal.
Because edge shapes didn't exist back then >.<
I've fought with trying to turn arbitrary graphics into polygons for a while. So far our best algorithm.

marching squares. get the raw outline
Nothing fancy the a list of border pixels in order.

Douglas Peucker: downsample the outlines
The algorithm is very straight forward to implement. We fancied it up a bit so we could fine tune the number of resulting points. The default algorithm halting condition is by error which could be good enough.

glu tesselator: convert the outlines to polygons
marching squares will give reverse outlines for holes in the image. GLU tesselator handles this case beautifully. GLUu tesselator is an extremely high quality tesselator. I haven't found a set of outlines it can't chop up.

post processing: remove invalid triangles (actually merge them with other triangles).
this might not be needed once the new broad phase is finished. The trouble was glu tesselate would sometime produce triangles that where too skinny for Box2d. We also merge triangles together to produce 8 point convex polygons. It wasn't hard to implement, just compare the resulting angle of the merged polygons. If it's less than pi the merge is good. There is a name for this algorithm. I can't remember what it is however. Also GLU tesselator can make this easier if you're careful. It will tell you when two triangle share a point.

-nick
I've been writing an algorithm that skips the tessellation step... it generally yields as large convex polygons as possible, but with no guarantees. It's currently buggy as stink though. But fast. I'll start working on that again after I figure out how to rig up my property editor -- http://img13.imageshack.us/img13/5844/s ... ditor1.png -- and get back to the decomposing stage... if that ever happens. La de da... I think there are a couple decomposers on these forums already though, you can use that after your first two steps.

Delaunay triangles shouldn't be skinny though eh...? Or maybe I'm thinking of something else too... I'm so out of it.

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