3

I need to be able to accurately detect and react to collision with any bitmap terrain, and the best method seems to be to use Marching Squares to generate a polygon mesh, which I can then use with a physics engine such as Bullet, which will be much better than anything I can write by myself.

The problem is while I've seen plenty of people mention using Marching Squares to do this, I can't find anything that explains how! I've not even been able to find any particularly great explanations/tutorials on the Marching Squares algorithm itself, though I think I pretty much understand it from what I have found. What I can't figure out is how to create polygons from the lines that MS will give me.

I have found this, which is pretty much exactly what I want to make, but I'll either need to find a similar physics library for C++, or write the mesh generation myself.

http://deltaluca.me.uk/docnew/swf/DestructableTerrain.html

I'd rather not simply look through the source for the physics engine to see how it works, so hopefully somewhere knows of a place that explains the process a bit better! Anything in C++ would be preferred, but other languages should be fine if the process is explained well.

  • We should be clear; MS isn't a way of generating a polygon mesh; it's a way of generating contours that can be used to make a mesh. Which problem are you having trouble with, what have you tried and what is your expected result. Do you have a specific problem you're trying to solve or is this a homework thing ? – Russ Clarke Apr 14 '12 at 01:55
  • Oh yeah, I know MS itself doesn't generate a mesh, the bit I don't understand at all is how to take what MS gives you and generate a mesh from it. I've not actually started writing it at all yet, because I wanted to plan ahead and make sure I can make what I need before I started. As for the specific problem, I want to make 2D destructable terrain with accurate collision response (Just like in the link I posted) for a game I'm working on. – Megadanxzero Apr 14 '12 at 11:25
  • Can you explain a little bit more about your problem? What is the input? For marching squares/cubes, the input is usually a grayscale image and a threshold which determines the iso-contour. The link you gave starts with a mesh. What's the initial input? – killogre Apr 16 '12 at 17:18

1 Answers1

0

What I can't figure out is how to create polygons from the lines that MS will give me.

Marching squares or 2D marching cubes give you a number of line segments, each of which goes from one edge to another edge inside one square. The edges where the lines segments start and end join two grid vertices of distinct signs. And both squares sharing such edge produce one line segment each having common point on the edge. See marching cubes tutorial.

All one needs to do later is to join the segments in common points to form polylines. One of the approaches is as follows:

  1. Use unique ids of edges in 2D grid as a key for both starting and ending points of each segment in a hash map.
  2. Then process all line segments consequently, if starting (or ending) point of the current line segment is missing in hash map, then add it there with the reference to the line segment; otherwise take the segment from the hash map and merge it with the current segment in the common point.

Anything in C++ would be preferred, but other languages should be fine if the process is explained well.

For example, take a look at MeshLib C++ library, specifically on the implementation of distanceMapTo2DIsoPolyline function.

Fedor
  • 17,146
  • 13
  • 40
  • 131