Casting X and Y collision rays

This commit is contained in:
Daniele Verducci 2023-01-02 19:00:17 +01:00
parent 082097e0d0
commit ada9598e25

View File

@ -108,7 +108,6 @@ class Main:
"x": int(self.player_position["x"] + math.cos(self.player_position["r"]) * 50), # deltaX + playerX "x": int(self.player_position["x"] + math.cos(self.player_position["r"]) * 50), # deltaX + playerX
"y": int(self.player_position["y"] + math.sin(self.player_position["r"]) * 50) # deltaY + playerY "y": int(self.player_position["y"] + math.sin(self.player_position["r"]) * 50) # deltaY + playerY
} }
sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(255,0,0,255), (self.player_position["x"], self.player_position["y"], ray["x"], ray["y"]))
def draw2Dmap(self): def draw2Dmap(self):
# 2D map # 2D map
@ -124,7 +123,7 @@ class Main:
# Casts rays for raycasting # Casts rays for raycasting
rayAngle = self.player_position["r"] rayAngle = self.player_position["r"]
for r in range(1): for r in range(1):
'''
# Check horizontal lines # Check horizontal lines
dof = 0 # Depth of field dof = 0 # Depth of field
if rayAngle == 0 or rayAngle == math.pi: if rayAngle == 0 or rayAngle == math.pi:
@ -159,15 +158,20 @@ class Main:
rayX = rayX + xOffset rayX = rayX + xOffset
rayY = rayY + yOffset rayY = rayY + yOffset
dof = dof + 1 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))
# Check vertical lines # Check vertical lines
dof = 0 # Depth of field dof = 0 # Depth of field
nTan = -math.tan(rayAngle) nTan = -math.tan(rayAngle)
xOffset = 0
yOffset = 0
if rayAngle == math.pi * 0.5 or rayAngle == math.pi * 1.5: if rayAngle == math.pi * 0.5 or rayAngle == math.pi * 1.5:
#if rayAngle == 0 or rayAngle == math.pi:
# Looking up or down (ray will never intersect vertical lines) # Looking up or down (ray will never intersect vertical lines)
rayX = self.player_position["y"] rayX = self.player_position["x"]
rayY = self.player_position["x"] rayY = self.player_position["y"]
dof = DOF # Set depth of field to maximum to avoid unneeded checks dof = DOF # Set depth of field to maximum to avoid unneeded checks
elif rayAngle > math.pi * 0.5 and rayAngle < math.pi * 1.5: elif rayAngle > math.pi * 0.5 and rayAngle < math.pi * 1.5:
# Looking right # Looking right
@ -187,8 +191,7 @@ class Main:
mapX = int(rayX / MAP_SCALE) mapX = int(rayX / MAP_SCALE)
mapY = int(rayY / MAP_SCALE) mapY = int(rayY / MAP_SCALE)
mapArrayPosition = mapY * MAP_SIZE + mapX mapArrayPosition = mapY * MAP_SIZE + mapX
print([rayX, rayY, mapX, mapY, mapArrayPosition]) if mapArrayPosition >= 0 and mapArrayPosition < MAP_SIZE*MAP_SIZE-1 and MAP[mapArrayPosition] != 0:
if mapArrayPosition < MAP_SIZE*MAP_SIZE and MAP[mapArrayPosition] != 0:
dof = 8 # Hit the wall: we are done, no need to do other checks dof = 8 # Hit the wall: we are done, no need to do other checks
else: else:
# Didn't hit the wall: check successive horizontal line # Didn't hit the wall: check successive horizontal line
@ -197,7 +200,7 @@ class Main:
dof = dof + 1 dof = dof + 1
# Draw ray # 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)) #sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(0,0,255,255), (self.player_position["x"], self.player_position["y"], rayX, rayY))