2017-05-21 3 views
0

Je viens de commencer à jouer avec pyBox2D et faisait une raycasting pour un jeu de haut en bas pour voir autour des objets, similaire à http://ncase.me/sight-and-light/. Mais certains rayons sont lancés sur un objet et continuent à travers l'objet et en frappent d'autres derrière celui-ci http://imgur.com/C3Wyg4T
Comme vous pouvez le voir, certains rayons traversent des objets, mais lorsque je bouge la boîte dynamique commence à travail (ish) mais parfois autour glitchespyBox2D raycasting ne fonctionne pas comme il se doit

Voici mon rappel raycasting

class lightRayCasting(b2RayCastCallback): 
    def __init__(self, point, ignoreBody): 
     b2RayCastCallback.__init__(self) 
     self.hit = False 
     self.point = point 
     self.ignoreBody = ignoreBody 

    def ReportFixture(self, fixture, point, normal, fraction): 
     if fixture.body != self.ignoreBody and fixture.filterData.groupIndex != -1 and not self.hit: 
      self.hit = True 
      self.point = point 
      return 0 
     return 1 

Et c'est là j'appelle le raycasting

def lightRayCasting(self, position, **kwargs): 
    points = [] 
    for ray in range(kwargs.get("n", 100)): 
     angle = (ray/kwargs.get("n", 100) * 360 * (b2_pi/180)) 
     rayDirection = b2Vec2(math.sin(angle), math.cos(angle)) * kwargs.get("length", 10) 
     callback = lightRayCasting(rayDirection, ignoreBody = kwargs.get("ignoreBody", None)) 
     self.world.RayCast(callback, self.convertPosition(position), rayDirection) 
     points.append(self.convertPosition(callback.point, 1)) 
     if kwargs.get("debugDraw", False): 
      if callback.point != rayDirection: pygame.draw.aaline(self.surface, (255, 0, 0), position, self.convertPosition(callback.point, 1)) 
      else: pygame.draw.aaline(self.surface, (0, 255, 0), position, self.convertPosition(callback.point, 1)) 
    if not kwargs.get("debugDraw", False): 
     pygame.gfxdraw.filled_polygon(self.surface, points, (255, 255, 255)) 
     pygame.gfxdraw.aapolygon(self.surface, points, (255, 255, 255)) 

self.convertPosition convertit simplement les pixels en mètres pour que Box2D fonctionne. Je ne vois pas pourquoi ça marche parfois mais d'autres fois ça ne marche pas. Est-ce que les corps doivent être éveillés pour que les raycastings puissent travailler dessus?

Répondre

0

J'ai résolu mon problème, pour tous ceux qui veulent savoir, je prends les fractions du rappel et pour chaque rappel s'il est plus petit que le courant et s'il est défini le point d'intersection au point sur le rappel

def ReportFixture(self, fixture, point, normal, fraction): 
     if fraction < self.fraction: 
      if not fixture in self.ignoreFixtures: 
       if not fixture.filterData.groupIndex in self.ignoreIndexes: 
        self.hit = True 
        self.point = point 
        self.fraction = fraction 
     return 1