微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何考虑尺寸选择最接近的对象

如何解决如何考虑尺寸选择最接近的对象

我正在研究基于代理的模拟。我希望每个代理选择要关注的其他代理。有很多不同的输入,但是在这里我要问的是愿景1。因此,代理还必须考虑大小,以查看哪个其他代理最接近。他们应该更关心更大的事物,而不是较小的事物,一个很小的代理商并不像一个很小的代理商那么重要。到目前为止,看来还不错,除了考虑谁最接近并且身材高大也考虑了其他人不在视觉范围之内。

到目前为止,我已经拥有了

# both seen and viewer are two different groups
def sight(seen,viewer)
    for entity in viewer:
# This is needed later to decide which way to go,not used here
        currentDir = entity.Direction
# figure out where the viewer is
        pos = pygame.math.Vector2(entity.rect.centerx,entity.rect.centery)
# Find out who is closest. 
# Uses viewer location and location of each agent in the other group to see who is closest. 
# At the end,divided by size*size of the agent in the other group (for height / width).
# Otherwise they'd pay attention to a tiny agent that's close by 
# instead of a huge agent that's just a bit further away.
        closeby = min([e for e in seen.sprites()],key=lambda e: (pos.distance_to(pygame.math.Vector2(e.rect.centerx,e.rect.centery)) / (e.size * e.size)))
        vector = pygame.math.Vector2((entity.rect.centerx - closeby.rect.centerx),(entity.rect.centery - closeby.rect.centery))
# Get the distance to that other agent
        distance = math.hypot(entity.rect.centerx - closeby.rect.centerx,entity.rect.centery - closeby.rect.centery)
# They can't see forever,so they have a sight limit
            if distance < entity.sight:
                Blah blah,rest of the code here

这是问题所在:选择“近距离”后出现“如果距离

想象一下:我是X特工。我应该担心某个小人物(Y特工)在我的视觉范围内。但是,有些人(代理Z)确实很大,超出了我的视觉范围。 Z代理是如此之大,我选择他作为“附近”。然后他不在视觉范围内,因此“ if distance

我想搜索

closeby = min([e for e in etc etc etc

应该限于在可视范围内的人(但我不知道该怎么做),或者如果选择为“附近”的特工在可视范围之外,则应选择SECOND最接近的人。如果他们再次超出视觉范围,则应选择最接近的第三者,依此类推,直到使某人进入视觉范围。但是我也不知道该怎么做。

任何人都可以提供任何帮助来限制“ min([e for e blah blah”或在该组中选择下一个分钟,如果第一个不代表“ entity.sight”标准,我们将不胜感激。像第二种选择(在组中重复)更有可能,但是我对python很陌生,所以我不知道。

非常感谢大家!

解决方法

要么使用循环查找最近且可见的元素:

import math
def sight(seen,viewer)
    for entity in viewer:
        currentDir = entity.Direction
        entityPos = pygame.math.Vector2(entity.rect.center)
    
        minE = None
        minCloseByDist = math.inf
        for e for e in seen.sprites():
            ePos = pygame.math.Vector2(e.rect.center)
            distance = entityPos.distance_to(ePos)
            if distance < entity.sight:
                closeByDist = distance / (e.size * e.size)
                if minE == None or closeByDist < minCloseByDist:
                    minE = e
                    minCloseByDist = minE

        if minE != None:
            # [...]

或者您可以先查找视图中的所有元素,然后查找最近的元素:

def sight(seen,viewer)
    for entity in viewer:
        currentDir = entity.Direction
        entityPos = pygame.math.Vector2(entity.rect.center)
    
        inSight = [e for e in seen.sprites() if entityPos.distance_to(pygame.math.Vector2(e.rect.center)) < entity.sight]
        if inSight:
            closeby = min([e for e in inSight],key=lambda e: (pos.distance_to(pygame.math.Vector2(e.rect.center)) / (e.size * e.size)))

            # [...]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。