C#:2D子Tile线相交

发布于 2024-08-31 06:36:15 字数 713 浏览 1 评论 0原文

我在让我的游戏运行算法时遇到一些问题,希望这里有人可以帮助我。谷歌似乎没有提供很好的帮助,因为大多数解决方案仅适用于完整的图块。

在游戏中,单位可以占据图块内的不同位置,即它们可以位于图块的左上角、中心、右下角……位置(2/3),即(2.2/3.1)、(2.5/3.5) ),(2.8/3.9)。

如果它们从位置(2.2/3.1)移动到(5.7/4.1),我需要检查路径中是否有障碍物。

我当前的算法是:

  1. 从 (2.2/3.1) 开始
  2. 计算移动的角度(即 70 度) 朝
  3. 该方向移动 0.1 步
  4. 检查我在哪个图块上 (floor(pX)/floor(pY))
  5. 从 2 开始重复

该算法有效,但对我来说它看起来不是很有效,因为障碍物只能是完整的图块,而不是图块的一部分(单位不会碰撞)。如果我增加步长,我就会开始错过仅轻微交叉的瓷砖(即仅穿过左下角)。即使步长为 0.1,仍然有可能错过障碍物。

我试图找到一个解决方案来获取子地图(所有带有角的图块(floor(start.X)/floor(start.Y))和(ceil(start.X)/ceil(start.Y)),移动遍历每个图块并以数学方式检查它是否交叉。遗憾的是,我似乎缺乏此检查所需的数学知识。

我的最后一个想法是将图块的所有 4 个边界作为一条线并进行线相交,但这似乎较慢。比我原来的方法

有什么提示吗

I have some problems getting an algorithm for my game to work and hope someone here can help me. Google didn't seem to be a good help as most solutions just work for full tiles.

In the game units can occupy different positions inside a tile, i.e. they can be in the upper left corner, center, bottom right, ... position of tile (2/3), i.e. (2.2/3.1), (2.5/3.5), (2.8/3.9).

If they move from position (2.2/3.1) to (5.7/4.1) i require a check to see if there is an obstacle in the path.

My current algorithm is:

  1. Starting from (2.2/3.1)
  2. Calculate the angle of the movement (i.e. 70 degree)
  3. Move 0.1 steps in that direction
  4. Check which tile i'm on (floor(p.X)/floor(p.Y))
  5. Repeat from 2

This algorithm works but to me it doesn't look very efficient as an obstacle can be only a full tile, not a part of a tile (units don't collide). If i increase the step size i begin to miss tiles that are only crossed slightly (i.e. you only cross the lowest left corner). Even with a step size of 0.1 it's still possible to miss an obstacle.

I tried to find a solution to take the sub map (all tiles with the corners (floor(start.X)/floor(start.Y)) and (ceil(start.X)/ceil(start.Y)), move through every tile and check mathmatically if it gets crossed. Sadly i seem to lack the required math knowledge for this check.

My last idea was to take all 4 borders of a tile as a line and do line-intersection but that seems to be slower than my original approach.

Any hints?

Thanks.

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

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

发布评论

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

评论(1

无悔心 2024-09-07 06:36:15

您不想通过沿着线步进来追踪路径 - 您想要直接跳到下一个可能的图块(边界)。这可以相当简单地计算。我将使用您上面的样本编号。

  1. 计算线 eqn (y= .286x + 2.471)
  2. 您从图块 2,3 开始,向图块 5,4 移动。因此,计算 x 变为 3 时的 y 值(紧邻右侧图块的边框)。是 3.329。
  3. 然后计算 y 变为 4 时的 x 值(紧邻上方图块的边框)。是 5.346。
  4. 从 2,3 开始向右移动达到 3,3.329。向上移动至 5.346,4。您在右侧相交(在 x 上移动 2 -> 3 不会移动 y 上的图块)。直到到达 x 中的图块 5 时,才会与上方相交。
  5. 4 中计算出的图块将成为您的新比较 (3,3)。从步骤 2 开始重复。

此过程仅对每个移动的图块进行一次计算(无论您的精度如何或图块有多大)并且是准确的。请注意,计算出的值可以存储并重复使用,而不是盲目地一遍又一遍地计算两个交点。在上面我们知道(步骤 4),直到 x=5 为止我们不会向上移动一个图块。因此无需再次计算即可推断出整个路径(2,3 -> 3,3 -> 4,3 -> 5,3 -> 5,4)。

也可以预先计算所有转换,而不是逐步进行转换,尽管这只有在您始终需要整个路径时才有用(您不需要,因为一旦发现障碍就想停止处理)。

两个警告。小心标志和线路走向——许多错误都是由于不密切注意负坡度而发生的。另外,使用实数你几乎永远不会跨越对角线(一次两个边界),但你应该意识到这一点(在代码中处理它)以防万一。

这个方法有一个名字,但我一时想不起来。我相信它可能来自游戏编程宝石系列,但也许其他人可以提供更好的参考。

Instead of tracing the path by stepping along the line - you want to jump right to the next possible tile (the border). This can be calculated fairly simply. I will use your sample numbers above.

  1. Calculate the line eqn (y= .286x + 2.471)
  2. You are starting on tile 2,3 and moving towards tile 5,4. So calculate the y value when x goes to 3 (the border to the tile immediately to the right). It is 3.329.
  3. Then calculate the x value when y goes to 4 (the border to the tile immediately above). It is 5.346.
  4. Starting at 2,3 and moving right gets to 3,3.329. Moving up gets to 5.346,4. You intersect on the right(moving 2 -> 3 on x doesn't move a tile on y). You don't intersect above until you are on tile 5 in the x.
  5. The tile calculated in 4 becomes your new comparison (3,3). Repeat from step 2.

This process only incurs one calculation per tile moved (regardless of your precision or how big the tiles are) and is exact. Note that the values calculated can be stored and reused instead of blindly calculating both intersections over and over. In the above we know (step 4) that we don't move up a tile until x=5. So the entire path can be inferred without another calculation (2,3 -> 3,3 -> 4,3 -> 5,3 -> 5,4).

It is also possible to precalculate all the transistions instead of doing them stepwise although this would only be beneficial if you always need the entire path (you don't since you want to stop processing once you find an obstacle).

Two caveats. Be careful about signs and which way the line is going - many a bug happen by not paying close attention to negative slopes. Also, using reals you almost never will cross diagonally (two borders at once) but you should be aware of it (handle it in the code) just in case.

There is a name for this method but I can't remember it off the top of my head. I believe it might be from Game Programming Gems series but maybe someone else can provide a better reference.

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