1

我正在使用精灵艺术,我需要为碰撞检测器生成一个多边形(顶点数组)。

我有一种getPixel(x, y)方法可以用来获取像素的颜色。我不需要任何花哨的颜色检测或任何东西,只需要实心像素和透明像素。在我的大脑开始融化之前,这是我开始的:

boolean[] hasColor = new boolean[size];

for (int i = 0; i < size; i++) {
    int row;
    row = i % width;
    if ((pixmap.getPixel(i, row) != 0) || (pixmap.getPixel(row, i) != -256)) {
        hasColor[i] = true;
    } else {
        hasColor[i] = false;
    }
}

这应该跟踪哪些像素是空的,哪些不是。但我不知道我应该从这里去哪里。

有没有算法或我可以用来提供帮助的东西?有人可以提供输入吗?

4

1 回答 1

0

What you have is raster artwork.

What you need is a vector outline.

Converting from vector to raster is easy, raster to vector, not so much.

Here is one possible workflow:

  1. Convert your artwork into a black-and-white image (like your pixel color present/not-present matrix).
  2. Use Adobe Illustrator's "Live Trace" feature to vectorize this image.
  3. Export the outline polygon into a format that you can read back easily.
  4. Use this as your input for the collision detection.

Here's another approach:

A) Assume that the outline looks like a hexagon, like this:

       *****
      *     *
     *       *
    *         *
     *       *
      *     *
       *****

B) Define "fit" as the goodness of an outline, calculated by checking what percentage of the pixels are inside the hexagon (does not have to be the regular-shaped figure shown above). C) Change the positions of the vertices, until you find an optimum fit (or until you get tired).

Step (C) is, of course, the hardest one. Plus, if your sprite needs more vertices, you may need to start out with an octagon/n-gon instead.

于 2013-03-24T23:54:28.823 回答