gpt4 book ai didi

python - 当圆正好位于两个正方形之间时,它会改变方向

转载 作者:行者123 更新时间:2023-12-02 20:43:20 26 4
gpt4 key购买 nike

我有一个 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 语句中,当它更改最近的建筑参数时,它也会从第一个 if 跳到第三个 if...我不知道如何修复它

我尽力让自己说清楚,但如果对我的代码有任何疑问,我可以发布我的代码(我试图避免这种情况,因为它不是很短的代码)

我尝试按照下面评论中的建议更改函数中最近建筑物的代码,但不幸的是,它不起作用

图像:这是它现在正在执行的操作的图像,我希望黄色圆圈(“行人”)在那些蓝线(“栅栏”)给出的限制内围绕那些蓝色方 block (“建筑物”)行走。我希望行人找到他们最近的建筑物(效果很好),然后绕着建筑物走动,或者至少在到达最近建筑物的拐角处时转弯,但是正如您所看到的,行人在“nearest_building”时改变了方向"参数已更改...可以清楚地看到他们正在“街道”中间行走...这是错误的

enter image description here

最佳答案

好吧,我自己找到了答案,问题是我没有正确设置 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")

关于python - 当圆正好位于两个正方形之间时,它会改变方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59040268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com