# How can I convert a 2D bitmap (Used for terrain) to a 2D polygon mesh for collision?

So I'm making an artillery type game, sort of similar to Worms with all the usual stuff like destructible terrain etc... and while I could use per-pixel collision that doesn't give me collision normals or anything like that. Converting it all to a mesh would also mean I could use an existing physics library, which would be better than anything I can make by myself.

I've seen people mention doing this by using Marching Squares to get contours in the bitmap, but I can't find anything which mentions how to turn these into a mesh (Unless it refers to a 3D mesh with contour lines defining different heights, which is NOT what I want). At the moment I can get a basic Marching Squares contour which looks something like this (Where the grid-like lines in the background would be the Marching Squares 'cells'):

That needs to be interpolated to get a smoother, more accurate result but that's the general idea. I had a couple ideas for how to turn this into a mesh, but many of them wouldn't work in certain cases, and the one which I thought would work perfectly has turned out to be very slow and I've not even finished it yet! Ideally I'd like whatever I end up using to be fast enough to do every frame for cases such as rapidly-firing weapons, or digging tools.

I'm thinking there must be some kind of existing algorithm/technique for turning something like this into a mesh, but I can't seem to find anything. I've looked at some things like Delaunay Triangulation, but as far as I can tell that won't correctly handle concave shapes like the above example, and also wouldn't account for holes within the terrain.

I'll go through the technique I came up with for comparison and I guess I'll see if anyone has a better idea. First of all interpolate the Marching Squares contour lines, creating vertices from the line ends, and getting vertices where lines cross cell edges (Important). Then, for each cell containing vertices create polygons by using 2 vertices, and a cell corner as the 3rd vertex (Probably the closest corner).

Do this for each cell and I think you should have a mesh which accurately represents the original bitmap (Though there will only be polygons at the edges of the bitmap, and large filled in areas in between will be empty). The only problem with this is that it involves lopping through every pixel once for the initial Marching Squares, then looping through every cell (image height + 1 x image width + 1) at least twice, which ends up being really slow for any decently sized image...

-
"that doesn't give me collision normals or anything like that." Says who? You can check the nearby pixels and generate a collision normal just fine. In fact, you can use marching cubes on-demand to do exactly this. No need for a mesh; you just need a normal, right? – Nicol Bolas May 6 '12 at 21:42
Oh yeah, I have already done something similar actually, but it's not great. I mean it works fairly well, but definitely not perfectly. Using Marching Squares on a small area around the collision point would probably be a slightly better solution than my previous one, but I can still see problems with it. For example too small a sample area will produce an inaccurate normal because you miss detail, whereas too large an area means you'd probably have to average the angle of the edges you get (which again could be inaccurate). – Megadanxzero May 6 '12 at 22:41
And of course that's ignoring the fact that I'd then still have to make my own physics, which probably wouldn't end up being very good, instead of just using an existing robust physics engine. I don't know of any physics engines that let you override the collision detection with your own per-pixel stuff and then just give it a collision normal to react to. Could be that something like that exists though. – Megadanxzero May 6 '12 at 22:44
How accurate of a normal do you need? You're making a Scorched Earth/Worms-style game. I think your players will forgive you if the bounce isn't perfect. I mean, do you think that the old DOS Scorched Earth was building highly accurate meshes and so forth? Do you think that even modern Worms games go through a great deal of effort to compute this stuff? – Nicol Bolas May 6 '12 at 22:44
The "physics" part of a physics system (especially in 2D) is the easiest part. It's the "collision" part that's hard. – Nicol Bolas May 6 '12 at 22:45