Python Forum
[PyGame] Breaking Raycasting loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Breaking Raycasting loop
#1
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.

    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
                                
        
Reply


Messages In This Thread
Breaking Raycasting loop - by XavierPlatinum - Aug-06-2022, 11:46 PM
RE: Breaking Raycasting loop - by deanhystad - Aug-07-2022, 09:41 PM
RE: Breaking Raycasting loop - by XavierPlatinum - Aug-08-2022, 12:37 AM
RE: Breaking Raycasting loop - by deanhystad - Aug-08-2022, 03:23 PM
RE: Breaking Raycasting loop - by XavierPlatinum - Aug-08-2022, 05:12 PM
RE: Breaking Raycasting loop - by squeamishfall - Oct-03-2023, 02:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Raycasting(again) walls with different heights robie972003 0 2,512 Mar-23-2019, 01:18 AM
Last Post: robie972003
  raycasting error robie972003 4 3,086 Mar-14-2019, 12:46 AM
Last Post: robie972003

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020