简体   繁体   English

有效地计算二维int数组中的距离

[英]Calculate distance in 2D int array efficiently

I am making a bomberman type game. 我正在制作一个轰炸机类型的游戏。 I want to calculate the distance between two points; 我想计算两点之间的距离; players can move in four directions. 玩家可以向四个方向移动。 If I am at (0,0) and want to go to (5,5), I can calculate the distance by using the taxicab distance formula ((5-0) + (5-0)) as I can only move horizontal and vertical. 如果我在(0,0)并想转到(5,5),我可以使用计程车距离公式((5-0)+(5-0))计算距离,因为我只能水平移动和垂直。

But now I want to implement walls in the game which the player cannot move through. 但是现在我想在游戏中实现玩家无法穿越的墙。

My current implementation of distance that just uses taxicab formula. 我当前的距离实现仅使用出租车公式。

int distanceTo(Position Pos)
{
    return (abs(this->x - Pos.x) + abs(this->y - Pos.y));
}

How do I find the distance between two points (x1,y1) and (x2,y2), considering the immovable boxes/walls that come in between? 考虑到介于两个点之间的不固定盒子/墙,如何找到两个点(x1,y1)和(x2,y2)之间的距离?

With the obstacles, you'll need to replace your simple taxicab distance formula with a more sophisticated pathfinding algorithm. 遇到障碍,您将需要使用更复杂的寻路算法来替换简单的出租车距离公式。

How big is your grid? 您的网格有多大? Dijkstra's algorithm would work for small grids, and A* (a modification of Dijkstra's) for larger ones. Dijkstra的算法适用于小型网格,而A *(Dijkstra的修改)适用于较大的网格。

Amit Patel has an excellent resource describing Dijkstra's and A*, as well as implementation details for each. 阿米特·帕特尔(Amit Patel)拥有出色的资源,描述了Dijkstra和A *,以及每种实现的详细信息。 You may view it here . 您可以在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM