Aug-06-2022, 11:46 PM
I have this raycasting code I've been working on. It mostly functions except I can't figure out how to break out of all the loops when the ray hits something. As it is I can see blocks that are supposed to be invisible behind he first block that the ray hits. The points in the ray are in order from the player. If I can break out further when contact is made then further blocks will not be illuminated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
def cast_rays( self ): rays = [] for b in self .game.level.block_dict.values(): b.illuminated = False for ray in range ( 0 , 360 , 20 ): ray_points = [] for depth in range ( 20 , 100 , 5 ): x = self .rect.centerx - math.sin(ray) * depth y = self .rect.centery + math.cos(ray) * depth ray_points.append((x,y)) rays.append(ray_points) for block in self .game.level.block_dict.items(): if abs (block[ 0 ][ 0 ] - self .rect.centerx) < 200 : if abs (block[ 0 ][ 1 ] - self .rect.centery) < 200 : for ray in rays: for point in ray: if block[ 1 ].rect.collidepoint(point): block[ 1 ].illuminated = True break else : continue break |