【问题标题】:A circle changes its direction when exactly between two squares当恰好在两个正方形之间时,圆会改变其方向
【发布时间】:2020-03-21 05:49:35
【问题描述】:

我有一个 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... 我不知道如何解决它

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

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

图片:这是它现在正在做什么的图片,我希望黄色圆圈(“行人”)在那些蓝线(“栅栏”)给定的范围内绕着那些蓝色方块(“建筑物”)走动。我希望行人找到他们最近的建筑物(可以正常工作)然后在建筑物周围走动,或者至少在他们到达最近建筑物的拐角时转身,但是正如你所看到的那样,行人在“nearest_building”的那一刻改变了他们的方向" 参数被改变了......很明显他们正走在一条“街道”的中间......这是错误的

【问题讨论】:

  • 也许稍微解释一下你的代码。很难理解你的主要思想/算法是什么。
  • 查看编辑,我试图解释更多
  • 在以下行中“如果 np.sqrt((pedestrian.body.position[0] - building.footprint.position[0])**2 + (pedestrian.body.position[1] - building.footprint.position[1])**2) != building.shape[0]:" 什么是可变建筑?因为在这一行,你在 for 循环之外。不应该是最近的建筑吗?
  • 也许如果你画一张你想要的图,与你的代码当前所做的相比,它会帮助人们更好地理解你的问题。
  • 图片添加,如你所愿

标签: python algorithm if-statement box2d


【解决方案1】:

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

【讨论】:

  • 我已删除新问题 - 答案仅限于答案材料。请您将您的补充问题作为一个新问题提出吗?如果你愿意,你可以回到这个页面。
猜你喜欢
  • 1970-01-01
  • 2021-01-22
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 2018-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多