首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Roguelike问题

Roguelike问题
EN

Stack Overflow用户
提问于 2011-03-02 23:22:24
回答 1查看 2K关注 0票数 3

我正在研究一个大学的compsci项目,我想要一些关于视野算法的帮助。我主要是工作,但在某些情况下,算法可以看到墙壁和高墙,玩家不应该看到。

代码语言:javascript
复制
void cMap::los(int x0, int y0, int radius)
{ //Does line of sight from any particular tile

for(int x = 0; x < m_Height; x++) {
    for(int y = 0; y < m_Width; y++) {
        getTile(x,y)->setVisible(false);
    }
}

double  xdif = 0;
double  ydif = 0;
bool    visible = false;
float   dist = 0;

for (int x = MAX(x0 - radius,0); x < MIN(x0 + radius, m_Height); x++) {      //Loops through x values within view radius
    for (int y = MAX(y0 - radius,0); y < MIN(y0 + radius, m_Width); y++) {         //Loops through y values within view radius

        xdif = pow( (double) x - x0, 2);
        ydif = pow( (double) y - y0, 2);

        dist = (float) sqrt(xdif + ydif); //Gets the distance between the two points

        if (dist <= radius) {               //If the tile is within view distance,
            visible = line(x0, y0, x, y);       //check if it can be seen.

            if (visible) {                          //If it can be seen,
                getTile(x,y)->setVisible(true);        //Mark that tile as viewable
            }
        }
    }   
}
}

bool cMap::line(int x0,int y0,int x1,int y1)
{
bool steep = abs(y1-y0) > abs(x1-x0);

if (steep) {
    swap(x0, y0);
    swap(x1, y1);
}

if (x0 > x1) {
    swap(x0,x1);
    swap(y0,y1);
}

  int deltax = x1-x0;
  int deltay = abs(y1-y0);
  int error = deltax/2;
  int ystep;
  int y = y0;

  if (y0 < y1)
     ystep = 1;
  else
     ystep = -1;

  for (int x = x0; x < x1; x++) {

      if ( steep && getTile(y,x)->isBlocked()) {
          getTile(y,x)->setVisible(true);
          getTile(y,x)->setDiscovered(true);
        return false;
      } else if (!steep && getTile(x,y)->isBlocked()) {
          getTile(x,y)->setVisible(true);
          getTile(x,y)->setDiscovered(true);
        return false;
      }

     error -= deltay;

     if (error < 0) {
        y = y + ystep;
        error = error + deltax;
     }

  }

  return true;
}

如果有人能帮我使第一个阻塞的瓷砖可见,但停止其余的,我会很感激。

谢谢,Manderin87

EN

回答 1

Stack Overflow用户

发布于 2011-03-03 00:01:42

你似乎在尝试创建一个光线投射算法。我想你已经知道布雷森汉姆的台词是怎么工作的了,所以我就直截了当了。

与其检查每个单元在潜在视场中的可见性,您只需将Bresenham线从FOV中心发射到每个细胞在潜在FOV区域的非常周长处(方形您循环通过)。在Bresenham线的每一步,检查单元格状态。每条射线的伪码都是这样的:

代码语言:javascript
复制
while (current_cell != destination) {
    current_cell.visible = true;
    if (current_cell.opaque) break;
    else current_cell = current_cell.next();
}

请记住,光线投射会产生大量的工件,在计算视图之后,您可能还需要进行后处理。

一些有用的资源:

  • 罗格盆地的射线铸造
  • 光线投射视场的实现 in 利布鳕鱼 (在C中,您可以在存储库中挖掘它的C++包装器)
  • FOV研究
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5174781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档