5

I need help creating a specialized line-collision algorithm that allows "cutting corners" at certain angles.

In the following pictures, let the blue square represent the player and the black square represent a wall. The white squares, then, represent squares in a player's "line of sight" (valid squares), and the grey squares are squares outside a player's "line of sight" (invalid squares):

img1

The second image is where things get more interesting as we start cutting corners:

img2

Let's take a closer look at this line which is allowed despite passing over the corner of the wall:

img3

The line is allowed because:

  • dx <= 0.5 (with a square being 1x1)
  • dx/dy is above a certain ratio (say, 2 - I'm not sure of the exact value represented in these images.)

The converse line is not allowed because the ratio (of dy/dx in this case) is too low:

img4

Or perhaps I should talk about the angle of entry vs exit from the square....

The main problem I'm having is that I can't figure out how to generalize a solution for vectors traveling at any angle between two points on the grid. I can't decide if I should use trigonometry or what. My closest solution so far has been to use the decimal parts of line interceptions with each square as the dx and dy's and check whether it's allowed based on the slope of the line and what quadrant it's in.

Can anyone help?

I've also looked at borrowing or starting from other line algorithms, but I haven't found anything too useful. Most of them that I've seen want a line from (x1, y1) to (x2, y2) to be the same as from (x2, y2) to (x1, y1) which makes this problem quite different.

4

1 回答 1

1

我建议使用圆圈,它们非常适合角落。

我假设坐标取自每个像素的中心。

知道 (xo,yo) 是否将视图从 (xa,ya) 隐藏到 (xb,yb) 的算法将是:

  • 在从 (xa,ya) 到 (xb,yb) 的线上计算 (xc,yc) 到 (xo,yo) 的最近点
  • 计算从 (xo,yo) 到 (xc,yc) 的距离 d
  • 如果 d < 半个像素,则视图被隐藏

您可以通过直接计算点和线http://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line之间的距离来简化前两个阶段,如果您想避免 sqrt,最终计算平方距离

于 2013-08-22T12:32:02.220 回答