WIP Vertical line check

This commit is contained in:
Daniele Verducci (Slimpenguin) 2023-01-02 11:04:52 +01:00
parent 142a92b2fc
commit 082097e0d0

View File

@ -124,26 +124,24 @@ 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:
print("horiz")
# Looking left or right (ray will never intersect parallel lines)
rayY = self.player_position["y"]
rayX = self.player_position["x"]
dof = DOF # Set depth of field to maximum to avoid unneeded checks
elif rayAngle > math.pi:
aTan = -1/math.tan(rayAngle)
print("down")
# Looking up
aTan = -1/math.tan(rayAngle)
rayY = (int(self.player_position["y"] / MAP_SCALE) * MAP_SCALE) - 0.00001
rayX = (self.player_position["y"] - rayY) * aTan + self.player_position["x"]
yOffset = -MAP_SCALE
xOffset = -yOffset * aTan
else:
aTan = -1/math.tan(rayAngle)
print("up")
# Looking down
aTan = -1/math.tan(rayAngle)
rayY = (int(self.player_position["y"] / MAP_SCALE) * MAP_SCALE) + MAP_SCALE
rayX = (self.player_position["y"] - rayY) * aTan + self.player_position["x"]
yOffset = MAP_SCALE
@ -161,9 +159,45 @@ class Main:
rayX = rayX + xOffset
rayY = rayY + yOffset
dof = dof + 1
'''
# Check vertical lines
dof = 0 # Depth of field
nTan = -math.tan(rayAngle)
if rayAngle == math.pi * 0.5 or rayAngle == math.pi * 1.5:
# Looking up or down (ray will never intersect vertical lines)
rayX = self.player_position["y"]
rayY = self.player_position["x"]
dof = DOF # Set depth of field to maximum to avoid unneeded checks
elif rayAngle > math.pi * 0.5 and rayAngle < math.pi * 1.5:
# Looking right
rayX = (int(self.player_position["x"] / MAP_SCALE) * MAP_SCALE) - 0.00001
rayY = (self.player_position["x"] - rayX) * nTan + self.player_position["y"]
xOffset = -MAP_SCALE
yOffset = -xOffset * nTan
else:
# Looking left
rayX = (int(self.player_position["x"] / MAP_SCALE) * MAP_SCALE) + MAP_SCALE
rayY = (self.player_position["x"] - rayX) * nTan + self.player_position["y"]
xOffset = MAP_SCALE
yOffset = -xOffset * nTan
# Check if we reached a wall
while dof < 8:
mapX = int(rayX / MAP_SCALE)
mapY = int(rayY / MAP_SCALE)
mapArrayPosition = mapY * MAP_SIZE + mapX
print([rayX, rayY, mapX, mapY, mapArrayPosition])
if mapArrayPosition < MAP_SIZE*MAP_SIZE and MAP[mapArrayPosition] != 0:
dof = 8 # Hit the wall: we are done, no need to do other checks
else:
# Didn't hit the wall: check successive horizontal line
rayX = rayX + xOffset
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))
sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(0,0,255,255), (self.player_position["x"], self.player_position["y"], rayX, rayY))