【问题标题】:Calculating grid cords from (x, y) position C++从(x,y)位置C ++计算网格线
【发布时间】:2013-01-13 22:22:32
【问题描述】:

我有一个数组[15][15],其中包含我的对象的路径。 0 是墙,其他任何东西都是路径(1->2->3->...->end)。它是 450px x 450px(30px x 30px 是一个场)的游戏场的反映。

对于数组[15][15],如下所示:

080000000000000
010111011101110
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
010101010101010
011101110111010
000000000000090

我得到:

我的对象以某种速度 * 速度移动:

void Enemy::move()
{
    this->get_dir();
    this->x += this->velX * this->speed; // velX = -1 is left, 1 is right
    this->y += this->velY * this->speed; // velY = -1 is up, 1 is down
}

get_dir() 检查它应该设置的方向(速度),如下所示:

void Enemy::get_dir()
{  
    Point p; // {x, y} struct

    p = this->get_grid(); // it tries to calculate X, Y axis into an array number
    if (p.y < 14 && path_grid[p.y + 1][p.x] - 1 == path_grid[p.y][p.x])
    {
        this->velY = 1;
        this->velX = 0;
        return;
    }
    /* same goes for rest of directions */

    this->velX = this->velY = 0; // if none matched it stops (reached end)
    return;
}

Point Enemy::get_grid()
{
    int x, y;

    for(y = 0;y < 15;y++)
    {
        if (this -> y >= y * 30 && this->y < (y + 1) * 30) break;
    }

    for(x = 0;x < 15;x++)
    {
        if (this -> x >= x * 30 && this->x < (x + 1) * 30) break;
    }

    return make_point(x, y);
}

但您可能会注意到,这将导致我的对象遵循如下路径:

因为它检查左上角,如果我向右移动它应该改变原点,但我不知道该怎么做。当我尝试添加 30(对象位图大小)时,如果它正确,它会停在上 -> 右角。这里有什么解决方案?

【问题讨论】:

  • 可能是我,但如果您使用括号对这些子表达式进行分组,我会发现代码更容易阅读......另外,名称很重要:我们应该如何解释get_grid(),谁的结果被分配给Point?是不是像get_position_in_grid()
  • 即使介绍对我来说听起来也很不清楚:“我有一个数组[15][15],我的对象有路径。0 是墙,其他任何东西都是路径 (1->2->3 ->...->结束)。”这是什么意思?你能改写一下吗?
  • 很遗憾您通过说“同样...”而忽略了所有其他情况,因为这可能是发现错误的关键。
  • 好的,让我们看看我是否能够正确解释它:二维数组path_grid 中的每个单元格都包含一个数字。如果数字为 0,则该单元格是一堵墙 - 不能走得更远。如果数字是其他数字,则该单元格是免费的-可以走得更远。另外,您似乎只显示了对象向下移动的情况,但是您的图片显示当对象移动向上时您遇到了麻烦
  • 我的解释是路径已经布置好了。路径单元格 1 是数字 1,下一个是 2、3、4、5 ...

标签: c++ coordinate-systems


【解决方案1】:

删除了我的大部分答案,因为它似乎误解了这个问题。但我会留下这部分关于你的get_grid 功能很疯狂。只需使用数学(我假设 xy 是整数):

Point Enemy::get_grid()
{
    return make_point(x / 30, y / 30);
}

此外,如果您想获取网格位置并将其显示在每个 30 像素正方形的图块的中心,请执行以下操作:

int pixelX = gridX * 30 + 15;
int pixelY = gridY * 30 + 15;

【讨论】:

  • 我认为他根本没有尝试找到路径,这就是他减去 1 的原因:他的路径已经编码在二维数组中,如果path[x1][y1] = path[x2][y2] + 1,那么位置 (x1, y1) 在路径中跟随位置 (x2, y2)。如果我正确理解了您的 cmets,这基本上就是您最初所做的假设。所以速度在这里并不重要,他只是将它用作更新位置的(有争议的)方式,并且他正在计算它基于他已经拥有的路径。我认为设计是错误的,但假设这种设计,错误在代码中,没有显示
  • 嗯,是的,在我回答之前我没有看到其他讨论。有点吃力。这个问题措辞不佳,似乎只显示不相关的信息。我将删除除 get_grid 替换之外的所有内容。
  • 我想这不能怪你。该问题被标记为“寻路”,而这甚至不是寻路问题。
【解决方案2】:

您应该能够从网格中找到下一个未平仓头寸,但应该跟踪最后一步,以免回溯,例如:

void movePlayer(){
  const GridMap& gridMap = getGridMap(); //returns the 14x14 grid
  const Position& currentPosition = getCurrentPosition(); //returns the current player position
  const Position& previousPosition = _getPreviousPosition(); //private helper func
  //returns a list of position where the current player at the given position can move to

  //at point (x,y) you can move to NORTH,SOUTH,EAST, or WEST one unit, from the gridMap
  //using the currentPosition, return the list of cells that the player at the given position can move to.
  const PositionList& currentMovablePositions = _getMovablePosition(currentPosition,gridMap);
  //return the first matching position that isn't currentPosition or previousPosition
  const Position& nextMovePosition = _getNextMovablePosition(currentMovablePosition,currentPosition,previousPosition);

  this->animateTo( nextMovePosition );

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-25
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多