From 904d817203fcfebeeb137079b66f7b2dd5d1ba8b Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Mon, 2 Jan 2023 19:19:47 +0100 Subject: [PATCH] Casting single ray to nearest wall --- raycaster.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/raycaster.py b/raycaster.py index c083413..88bee89 100755 --- a/raycaster.py +++ b/raycaster.py @@ -123,7 +123,6 @@ class Main: # Casts rays for raycasting rayAngle = self.player_position["r"] for r in range(1): - # Check horizontal lines dof = 0 # Depth of field if rayAngle == 0 or rayAngle == math.pi: @@ -159,8 +158,9 @@ class Main: rayY = rayY + yOffset dof = dof + 1 - # Draw ray - sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(0,255,0,255), (self.player_position["x"], self.player_position["y"], rayX, rayY)) + # Save horyzontal probe rays for later comparison with vertical + horizRayX = rayX + horizRayY = rayY # Check vertical lines dof = 0 # Depth of field @@ -199,10 +199,14 @@ class Main: rayY = rayY + yOffset dof = dof + 1 - # Draw ray - #sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(0,0,255,255), (self.player_position["x"], self.player_position["y"], rayX, rayY)) + if self.dist(self.player_position["x"], self.player_position["y"], rayX, rayY) > self.dist(self.player_position["x"], self.player_position["y"], horizRayX, horizRayY): + rayX = horizRayX + rayY = horizRayY + sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(0,0,255,255), (self.player_position["x"], self.player_position["y"], rayX, rayY)) + def dist(self, ax, ay, bx, by): + return math.sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay))