gpt4 book ai didi

Java RogueLike 视线问题

转载 作者:行者123 更新时间:2023-11-30 07:09:07 24 4
gpt4 key购买 nike

我正在制作一款 roguelike 游戏,我正在尝试将“快速而肮脏”的 FOV 技术应用到我的游戏中,但我遇到了一些问题。我几乎可以肯定它在我的函数中完成了线计算,其中我有一个 x 和 y 变量沿着 2 点(我正在检查的图 block 和玩家)之间的角度移动。

如果您不知道“快速而肮脏”的 FOV 是什么,这里是链接 http://www.roguebasin.com/index.php?title=Quick_and_dirty_FOV/LOS

代码

  public static boolean lineOfSight( int xx, int yy, int xx2, int yy2) {
float deltaX = xx - xx2;
float deltaY = yy - yy2;
float angle = (float) Math.atan2(deltaY,deltaX);
int distance = distanceCheck(player.x,player.y,xx2,yy2);
int counter = 0;
while(counter < distance) {
counter ++;
xx += (int) (Math.sin(angle));
yy += (int) (Math.cos(angle));
if(map[xx][yy] == 1) {
return false;
}
}
return true;
}

最佳答案

首先,我注意到函数中有一些奇怪的东西。您实际上拥有三组坐标 - (xx, yy)、(xx2, yy2) 和 (player.x, player.y)。据我了解,该算法是从 A 点到 B 点画一条线,然后查看沿线是否有任何瓷砖阻挡访问。为此,您只需要两组坐标,所以您的错误可能就像将 (player.x, player.y) 替换为 (xx, yy) 一样简单。

其次,在 roguelike 中,您通常拥有基于图 block 的环境,其中三角函数是不必要的,甚至被认为是浪费。由于您所有的图 block 都有整数位置,因此您绝对应该能够为像这样的关键低级实现实现纯整数实现。

最后,由于您将输入坐标存储为整数,因此当角度的 sin 和 cos 小于 1 时,内部循环将无法按预期工作。具体来说,坐标变量不会存储部分瓷砖移动。在循环中放置一个 print 语句,看看 (xx, yy) 到底发生了什么。

如果你想更快地测试你的,我从 Github 上撤下了我的 my roguelike 实现。当然,您必须根据表示 map 的方式更改它,并且可能会删除阻塞检查。

/**
* Check if the point (x0, y0) can see point (x1, y1) by drawing a line
* and testing for the "blocking" property at each new tile. Returns the
* points on the line if it is, in fact, visible. Otherwise, returns an
* empty list (rather than null - Efficient Java, item #43).
*
* http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
*/
public static List<Point> isVisible(GameMap map, int x0, int y0, int x1, int y1) {
List<Point> line = new LinkedList<Point>();
line.add(new Point(x0, y0));
int dx = Math.abs(x1 - x0);
int dy = Math.abs(y1 - y0);
int sx = (x0 < x1) ? TILE_X : -TILE_X;
int sy = (y0 < y1) ? TILE_Y : -TILE_Y;
int err = dx - dy;
int e2;

while (!(x0 == x1 && y0 == y1)) {
if (map.isBlocked(x0, y0)) {
line.clear();
return line;
}

e2 = 2 * err;

if (e2 > -dy) {
err -= dy;
x0 += sx;
}

if (e2 < dx) {
err += dx;
y0 += sy;
}

line.add(new Point(x0, y0));
}

return line;
}

该代码实现了 Bresenham 的画线算法,这是流氓类游戏中视线计算的常用方法。为了我自己的目的,我还返回沿线的点(然后您可以使用它以某种方式标记这些坐标 - 例如使用额外的照明参数)。此信息很有值(value),在您费尽心思确定磁贴是否可见后,不应将其丢弃!

关于Java RogueLike 视线问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23400272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com