Python 具有可变移动速度的2D寻路?
嗨,我对寻路领域相当陌生,我已经在互联网上搜索了我的问题的答案,所以我想是时候问问专家了: “我的环境”由一个二维矩形地图组成,地图上有墙和单元,用于遍历环境 (单元之间无碰撞,但不能穿过墙壁) 环境中的单位以基于回合的方式直线移动,但移动速度为 0到单位移动速度(例如5)(您可以控制每个移动的移动速度)。(贴图大小的范围是从20,20到200200个单元格,但您可以移动到浮点位置添加墙可以位于类似eg wall的位置[(x=10.75,y=9.34),(x=33.56,y=62.43]) 在夏季,每个滴答声,你告诉一个单位移动到一个浮动类型的目的地(x,y) 我尝试过A*和算法,但我一直遇到的问题是计算它们的移动速度,因为很明显,最佳移动速度应该是最大速度,但如果它们总是以最大速度移动,它们很容易撞到墙,因为这些算法没有考虑到可变的移动速度。 有什么想法吗? 具有*星型和基于目标的矢量寻路问题的movespeed问题图像 代码:Python 具有可变移动速度的2D寻路?,python,2d,path-finding,Python,2d,Path Finding,嗨,我对寻路领域相当陌生,我已经在互联网上搜索了我的问题的答案,所以我想是时候问问专家了: “我的环境”由一个二维矩形地图组成,地图上有墙和单元,用于遍历环境 (单元之间无碰撞,但不能穿过墙壁) 环境中的单位以基于回合的方式直线移动,但移动速度为 0到单位移动速度(例如5)(您可以控制每个移动的移动速度)。(贴图大小的范围是从20,20到200200个单元格,但您可以移动到浮点位置添加墙可以位于类似eg wall的位置[(x=10.75,y=9.34),(x=33.56,y=62.43]) 在夏
class Cell:
def __init__(self,coords,vector=None,distance=None,obstacle=False):
self.coords = coords
self.vector = vector
self.distance = distance
self.obstacle = obstacle
class VectorMap:
def __init__(self,unit, dest, map=HardCoded._make_map()):
self.size = Coords(len(map), len(map[0]))
self.map = map
VectorMap.map_converter()
self.unit = unit
self.dest = dest
def _create_vector_map(self):
return self._cell_def(1,self.map[self.dest])
def _cell_def(self,dist,current_cell):
neighbors = [Coords(0, 1), Coords(0, -1), Coords(1, 0), Coords(-1, 0), Coords(1, 1), Coords(1, -1),
Coords(-1, 1), Coords(-1, -1)]
for neighbor in neighbors:
#check if out of range of arr then return map
if current_cell.coords.y + neighbor.y < self.size[1] and current_cell.coords.x + neighbor.x < self.size[0]:
neighbor_cell = self.map[current_cell.coords.x + neighbor.x][current_cell.coords.y + neighbor.y]
if neighbor_cell.obstacle:
continue
neighbor_cell.distance = dist
#neighbor_cell.vector = current_cell
return self._cell_def(self.map,dist+1,neighbor_cell)
def map_converter(self):
nmap = []
for x,element in enumerate(self.map):
tmap = []
for y,value in enumerate(element):
tmap.append(Cell(Coords(x,y),vector=None,distance=None,obstacle=False if value == 0 else True))
nmap.append(tmap)
self.map = nmap
self._create_vector_map()
for x,c in enumerate(self.map):
for y,cell in enumerate(c):
cell.vector = Coords(0,0)
right_tile_distance = (self.map[x+1][y].distance if x+1 < self.size.x else self.map[x][y].distance)
up_tile_distance = (self.map[x][y+1].distance if y+1 < self.size.y else self.map[x][y].distance)
cell.vector.x = (self.map[x-1][y].distance if x is not 0 else self.map[x][y].distance) - right_tile_distance
cell.vector.y = up_tile_distance - (self.map[x][y-1].distance if y is not 0 else self.map[x][y].distance)
if cell.vector.x == 0 and right_tile_distance < self.map[x][y].distance:
cell.vector.x = 1
if cell.vector.y == 0 and up_tile_distance < self.map[x][y].distance:
cell.vector.y = 1
cell.vector = Util.normalize(cell.vector)
def vetor_move(self):
vector = self.map[self.unit.coords.x][self.unit.coords.y].vector
movespeed = self.desired_movespeed()
dest = Coords(vector.x * movespeed,vector.y * movespeed)
return Action(mechanic='move', params=[dest], report='Moving With Maths')
def desired_movespeed(self):
pass
class Util:
@staticmethod
def normalize(vector):
norm=np.linalg.norm(vector,ord=1)
if norm == 0:
norm = np.finfo(vector.dtype).eps
return Coords(*(vector/norm))
类单元格:
定义初始化(self,coords,vector=None,distance=None,barrier=False):
self.coords=coords
self.vector=vector
self.distance=距离
自我障碍
类向量映射:
定义初始值(自身、单位、目的地、映射=硬编码。_make_map()):
self.size=Coords(len(map),len(map[0]))
self.map=map
VectorMap.map_转换器()
self.unit=单位
self.dest=dest
定义创建向量映射(自):
返回self.\u单元格定义(1,self.map[self.dest])
定义单元定义(自身、区域、当前单元):
邻居=[Coords(0,1),Coords(0,-1),Coords(1,0),Coords(-1,0),Coords(1,1),Coords(1,-1),
Coords(-1,1),Coords(-1,-1)]
对于邻居中的邻居:
#检查是否超出arr范围,然后返回map
如果当前_cell.coords.y+邻居.y