如何确定 2D 可见性

发布于 2024-08-26 10:17:15 字数 307 浏览 8 评论 0原文

我正在开发一个人工智能沙箱,我想计算每个生物体可以看到的内容。

规则是简单地从实体的角度隐藏形状边缘后面的内容。该图像澄清了一切:

alt 文本 http://img231.imageshack.us/img231/2972 /shadows.png

我需要它作为人工智能的输入,或者以图形方式显示它,以在特定实体移动时显示它。

有什么很酷的想法吗?

I'm developing an AI sandbox and I would like to calculate what every living entity can see.

The rule is to simply hide what's behind the edges of the shapes from the point of view of the entity. The image clarifies everything:

alt text http://img231.imageshack.us/img231/2972/shadows.png

I need it either as an input to the artificial intelligence either graphically, to show it for a specific entity while it moves..

Any cool ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

享受孤独 2024-09-02 10:17:15

李氏算法。它会生成一个多边形,这可能对 AI 的进一步分析有用:

  1. 对于每个线段,计算实体与两个端点之间的角度。
  2. 按角度对点进行排序。
  3. 围绕 360°“扫描”,跟踪哪些线段与扫描线相交。当您越过线段的起始点时,您将该线段添加到集合中;当您越过线段结束点时,您将从集合中删除该线段。
  4. 最接近的线段形成可见的多边形。多边形是三角形条的并集。

我意识到这个解释不太好,但我有这里有一个在线演示,您可以尝试一下,了解它是如何工作的。将其扩展到与圆圈一起使用可能还不错(著名的遗言)。

2d 可见性演示

Lee's Algorithm. It produces a polygon which might be useful for further analysis by your AI:

  1. For each line segment, calculate the angle between the entity and the two endpoints.
  2. Sort the points by angle.
  3. “Sweep” around 360°, keeping track of which line segments intersect with the sweep line. When you cross the beginning-of-segment point, you add that segment to the set; when you cross the end-of-segment point, you remove that segment from the set.
  4. The closest line segments form a polygon of what's visible. The polygon is the union of triangle slivers.

I realize this explanation isn't great, but I have an online demo here that you can play with to get a sense for how it works. Extending it to work with circles probably isn't too bad (famous last words).

2d visibility demo

伤感在游骋 2024-09-02 10:17:15

如果您使用简单的形状来阻挡实体的视图,我已经实现了一种简单的方法来执行此操作:

创建一个可以水平或垂直移动的 VisionWave 对象。您可以使用源坐标、与该点相交的两条线以及距源点的距离来定义 VisionWave。

您应该有 4 个波浪:一个向上、一个向下、一个向左、一个向右,定义它们的线的斜率应为 1 和 -1(即 X)。下面我的粗略绘图显示了一个波浪(向右),由 > 字符表示。

  \     /
   \   />
    \ / >
     @  >
    / \ >
   /   \>
  /     \

创建一个循环,每次将每个波传播一个像素。传播波时,您需要执行以下操作:

  1. 将波接触的每个像素标记为可见。
  2. 如果波接触到的任何像素阻挡了光,那么您需要将波分成两部分,并递归地传播每个像素。

我在 Roguelike 中实现了这样的系统,速度非常快。确保分析您的代码是否存在瓶颈。

如果你很聪明,你可以尝试圆形波而不是直线,但我不知道由于三角计算,它是否会更快。

If you're using simple shapes to block the entity's view, there is an easy way to do this that I have implemented:

Create a VisionWave object which can move either horizontally or vertically. You can define a VisionWave using a source coordinate, two lines that intersect that point, and a distance from the source point.

You should have 4 waves: one going up, one down, one left, and one right, and the lines that define them should have a slope of 1 and -1 (i.e., an X). My crude drawing below shows one wave (going right) as represented by the > character.

  \     /
   \   />
    \ / >
     @  >
    / \ >
   /   \>
  /     \

Make a loop that propagates each wave one pixel at a time. When you propagate the wave, you want to do the following:

  1. Mark every pixel that the wave is touching as visible.
  2. If any of the pixels that the wave touches block light, then you want to split the wave into two, and recursively propagate each one.

I implemented a system like this in my Roguelike and it is very fast. Make sure to profile your code for bottlenecks.

If you're very clever you might try circular waves instead of straight lines, but I don't know if it would be faster due to the trigonometric calculations.

旧时模样 2024-09-02 10:17:15

确定哪些顶点对您的视点可见,然后将这些顶点投影到远离视点的直线上以形成新顶点。闭合形状,您将创建一个代表不可见区域的多边形。

请参阅 阴影体积 了解 3D 等效项。

Determine which vertices are visible to your eye point, then project those vertices back away from the eye point on a straight line to make new vertices. Close the shape and you will have created a polygon representing the invisible area.

See shadow volumes for the 3D equivalent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文