当恰好在两个正方形之间时,圆会改变方向
A circle changes its direction when exactly between two squares
我有一个 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"方法可以让代码更简单或更清晰(但这是次要问题,我需要先解决转向)?
非常感谢
编辑:我的代码背后的主要思想是:我有 类 行人和建筑物,行人是动态物体,建筑物是静态物体。我需要行人绕过最近的建筑物并穿过 "streets"(我试图在我之前的问题“Rolling a circle around a square”中找到可行的算法,您可以在其中找到我对两者的定义 类。我是能解决滚动的问题,不是很完美,但是很满意
问题是,当行人试图穿过 "street" 并且恰好在两座建筑物之间的中间时,它并没有继续向前移动到第二座建筑物,而是转了 90° 并且它会 "in the middle of a road"
问题出在 if 语句中,当它更改最近的建筑参数时,它也会从假设第一个 if 跳到第三个 if...我不知道如何解决它
我尽力让自己清楚,但如果对我的代码仍有任何疑问,我可以 post 我的代码(我尽量避免这种情况,因为它不是很短的代码)
我尝试按照下面评论中的建议更改函数中最近建筑物的代码,但不幸的是,它没有用
图片:这是它现在正在做的事情的图片,我希望黄色圆圈 ("pedestrians") 在那些蓝线给定的范围内绕过那些蓝色方块 ("buildings") ("fences")。我希望行人找到他们最近的建筑物(可以正常工作)然后绕着建筑物走动,或者至少在到达最近建筑物的拐角处时转弯,但是正如您所看到的那样,行人在 "nearest_building"参数改了...清晰可见他们是走在中间a"street"...错了
好吧,我自己找到了答案,问题是我没有正确设置 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")
我有一个 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"方法可以让代码更简单或更清晰(但这是次要问题,我需要先解决转向)? 非常感谢
编辑:我的代码背后的主要思想是:我有 类 行人和建筑物,行人是动态物体,建筑物是静态物体。我需要行人绕过最近的建筑物并穿过 "streets"(我试图在我之前的问题“Rolling a circle around a square”中找到可行的算法,您可以在其中找到我对两者的定义 类。我是能解决滚动的问题,不是很完美,但是很满意
问题是,当行人试图穿过 "street" 并且恰好在两座建筑物之间的中间时,它并没有继续向前移动到第二座建筑物,而是转了 90° 并且它会 "in the middle of a road"
问题出在 if 语句中,当它更改最近的建筑参数时,它也会从假设第一个 if 跳到第三个 if...我不知道如何解决它
我尽力让自己清楚,但如果对我的代码仍有任何疑问,我可以 post 我的代码(我尽量避免这种情况,因为它不是很短的代码)
我尝试按照下面评论中的建议更改函数中最近建筑物的代码,但不幸的是,它没有用
图片:这是它现在正在做的事情的图片,我希望黄色圆圈 ("pedestrians") 在那些蓝线给定的范围内绕过那些蓝色方块 ("buildings") ("fences")。我希望行人找到他们最近的建筑物(可以正常工作)然后绕着建筑物走动,或者至少在到达最近建筑物的拐角处时转弯,但是正如您所看到的那样,行人在 "nearest_building"参数改了...清晰可见他们是走在中间a"street"...错了
好吧,我自己找到了答案,问题是我没有正确设置 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")