- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一款 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/
谁能解释视线在 2d 中的工作原理?这对我的 2d 实验非常有帮助。正在进行的实验是一个简单的二维模拟。玩家在世界中从一个地方移动到另一个地方,my world exactly looks like
新程序员在我的腰带下使用了三个月的 Python(只有几周的 pygame)...... 我正在设计一个 2-D、自上而下的游戏,并且一直在尝试设置障碍来打破 sprite LOS。我目前的尝试是让每
我正在做一个游戏项目,我正在研究游戏的 AI 方面。我希望敌人的物体在看到敌人时开始瞄准并向玩家射击,我看到这篇文章是为了做到这一点: http://www.emanueleferonato.com/
我是一名优秀的程序员,十分优秀!