我有一个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")
我在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)
[如果可能的话,我将有另一个请求,是否还有其他“ pythonic”方式使代码更简单或更简洁(但这是第二个问题,我需要首先解决问题)?非常感谢]]
编辑:我的代码背后的主要思想是:我有行人和建筑物类,行人是动态物体,而建筑物是静态物体。我需要行人到最近的建筑物走动并穿过“街道”(我试图在上一个问题“ Rolling a circle around a square”中找到可行的算法,在这里您可以找到我对这两个类别的定义。我能够解决滚动问题,它不能完美运行,但令人满意]
问题是,当行人试图越过“街道”时,它恰好位于两座建筑物之间的中间,它仍然没有向前移动到第二座建筑物,而是转过90°,然后“在路中间“
问题出在if语句中,当它更改最接近的建筑参数时,如果不知道该如何解决,它也会从第1个if跳过到第3个。。。
我尽力使自己清楚,但是如果仍然有关于我的代码的问题,我可以发布我的代码(我试图避免这种情况,因为它不是很短的代码)
我尝试按照下面的注释中的建议,为最近的建筑物更改函数中的代码,但是很遗憾,它没有用
[图像:这是当前操作的图像,我希望黄色圆圈(“行人”)在这些蓝色线条(“栅栏”)指定的范围内绕着这些蓝色正方形(“建筑物”)走动。我希望行人找到他们最近的建筑物(可以正常运行),然后在建筑物周围走动,或者至少在他们到达最近建筑物的拐角处时转弯,但是您可以看到,当“ nearest_building”出现时,行人改变了方向“参数已更改...很明显,他们正走在“街道”的中间...这是错误的
我有一个Box2D世界,其中的圆圈代表行人,正方形代表建筑物,我有一个算法可以找到最近的建筑物,然后它将在边缘转弯,但是我有一个...
嗯,最终,我自己找到了答案,问题是我没有正确设置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")