Mec*_*rnk 5 python algorithm if-statement box2d
我有一个 Box2D 世界,圆圈代表行人,正方形代表建筑物,我有一个算法可以找到最近的建筑物,然后它会在边缘转动,但我有一个问题,当它正好在建筑物之间的中间时它会转动,所以算法有点工作,但不是我想要的方式
def pedestrian_walk(pedestrian, buildings):
## Find the building nearest to the pedestrian
list_of_distances = []
for building in buildings:
list_of_distances.append(np.sqrt((pedestrian.body.position[0] - building.footprint.position[0])**2 + (pedestrian.body.position[1] - building.footprint.position[1])**2))
pedestrian.nearest_building= list_of_distances.index(min(list_of_distances))
#print(f"Nearest builing index is: {pedestrian.nearest_building}")
building = buildings[pedestrian.nearest_building]
#Pedestrian walks around the left side of the nearest building
if pedestrian.body.position[0] <= building.position[0] and building.position[1] - building.shape[1] <= pedestrian.position[1] <= building.position[1] + 1.05*building.shape[1]:
pedestrian.body.__SetLinearVelocity(b2Vec2(0,pedestrian.ped_velocity))
print("1st if")
#Pedestrian walks around the right side of the nearest building
elif pedestrian.body.position[0] > building.position[0] and building.position[1] - building.shape[1] <= pedestrian.position[1]<= building.position[1] + 1.05*building.shape[1]:
pedestrian.body.__SetLinearVelocity(b2Vec2(0,-pedestrian.ped_velocity))
print("2nd if")
#Pedestrian walks around the upper side of the nearest building
elif pedestrian.body.position[1] > building.position[1] and building.position[0] - building.shape[0] <= pedestrian.position[0] <= building.position[0] + 1.05*building.shape[0]:
pedestrian.body.__SetLinearVelocity(b2Vec2(pedestrian.ped_velocity,0))
print("3rd if")
#Pedestrian walks around the lower side of the nearest building
elif pedestrian.body.position[1] <= building.position[1] and building.position[0] - building.shape[0] <= pedestrian.position[0] <= building.position[0] + 1.05*building.shape[0]:
pedestrian.body.__SetLinearVelocity(b2Vec2(-pedestrian.ped_velocity,0))
print("4th if")
Run Code Online (Sandbox Code Playgroud)
我在 pygame 主游戏循环中调用该函数,它工作正常:
Skyscrapers = create four buildings
while running:
...
while k <= MAX_AMOUNT_PEDESTRIANS:
random_skyscraper = random.choice(skyscrapers) #Randomly choose a skyscraper next to which a pedestrian is generated
skyscraper_to_walk_around.append(random_skyscraper)
new_walker = Pedestrian(box2world, position = (random_skyscraper.position[0] -random.randint(-random_skyscraper.shape[0],random_skyscraper.shape[0]),\
random_skyscraper.position[1] -random.randint(-random_skyscraper.shape[1],random_skyscraper.shape[1])))
walkers.append(new_walker)
positive_negative.append([-1,1][random.randrange(2)])
k = k+1
for ped in range(MAX_AMOUNT_PEDESTRIANS):
pedestrian_walk(walkers[ped],skyscrapers)
Run Code Online (Sandbox Code Playgroud)
如果可能的话,我会有另一个请求,是否有更多“pythonic”方式如何使代码更简单或更干净(但这是次要问题,我需要先解决转弯问题)?非常感谢
编辑:我的代码背后的主要思想是:我有行人和建筑类,行人是动态物体,建筑物是静态物体。我需要行人绕过最近的建筑物并穿过“街道”(我试图在我之前的问题“围绕正方形滚动一个圆圈”中找到工作算法,您可以在其中找到我对这两个类别的定义。我能够解决滚动问题,它工作不完美但令人满意
问题在于,当行人试图穿过“街道”并且正好在两座建筑物的中间时,它不会继续向前移动到第二座建筑物,而是转了 90° 并“在中间”一条路”
问题出在 if 语句中,当它更改最近的建筑参数时,它也会从第 1 个 if 跳到第 3 个 if ……我不知道如何解决它
我尽力让自己清楚,但如果我的代码仍有任何问题,我可以发布我的代码(我试图避免这种情况,因为它不是很短的代码)
我尝试按照下面评论中的建议更改最近建筑物的函数中的代码,但不幸的是,它不起作用
图像:这是它现在正在做什么的图像,我希望黄色圆圈(“行人”)在这些蓝线(“围栏”)给定的范围内绕着那些蓝色方块(“建筑物”)走动。我希望行人找到他们最近的建筑物(工作正常)然后在建筑物周围走动,或者至少在他们到达最近建筑物的角落时转弯,但是正如您所看到的,当“nearest_building”出现时,行人会改变方向” 参数改变了……很明显他们正走在“街道”中间……那是错误的
好吧,我自己找到了答案,问题是我没有正确设置 if 语句的限制,现在它可以工作了。我做了这样的改进:
def pedestrian_walk(pedestrian, buildings):
## Find the building nearest to the pedestrian
list_of_distances = []
for building in buildings:
list_of_distances.append(np.sqrt((pedestrian.body.position[0] - building.footprint.position[0])**2 + (pedestrian.body.position[1] - building.footprint.position[1])**2))
pedestrian.nearest_building= list_of_distances.index(min(list_of_distances))
#print(f"Nearest builing index is: {pedestrian.nearest_building}")
building = buildings[pedestrian.nearest_building]
#Pedestrian walks around either the left or right side of the nearest building
if building.position[0] - 1.15*building.shape[0] <=pedestrian.body.position[0] <= building.position[0] + 1.15*building.shape[0] and building.position[1] - building.shape[1] <= pedestrian.position[1] <= building.position[1] + 1.05*building.shape[1]:
pedestrian.body.__SetLinearVelocity(b2Vec2(0,pedestrian.ped_velocity))
print("1st if")
#Pedestrian walks around either lower or upper side of the nearest building
elif building.position[1] - 1.15*building.shape[1] <= pedestrian.body.position[1] <= building.position[1] + 1.15*building.shape[1]:
pedestrian.body.__SetLinearVelocity(b2Vec2(-pedestrian.ped_velocity,0))
print("2nd if")
Run Code Online (Sandbox Code Playgroud)