This commit is contained in:
Daniele Verducci (Slimpenguin) 2023-01-05 00:53:00 +01:00
parent 8e3727c870
commit c75f8555ab

View File

@ -17,12 +17,11 @@ RAYCAST_WIN_HEIGHT = 600
DUNGEON_WIDTH = MAP_WIN_WIDTH
DUNGEON_HEIGHT = MAP_WIN_HEIGHT
PLAYER_SPEED = 10
PLAYER_ROTATION_SPEED = 0.17
RAY_LENGTH = 100
MAP_SCALE = 80
DOF = 8 # Depth Of Field
DEGREE_IN_RADIANTS = 0.01745329
MAP = [
1, 1, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 1, 0, 0, 1,
@ -68,9 +67,9 @@ class Main:
if event.type == sdl2.SDL_KEYDOWN:
# Rotate player
if event.key.keysym.sym == sdl2.SDLK_LEFT:
self.player_position["r"] = self.player_position["r"] + 10*DEGREE_IN_RADIANTS
self.player_position["r"] = self.player_position["r"] - PLAYER_ROTATION_SPEED
elif event.key.keysym.sym == sdl2.SDLK_RIGHT:
self.player_position["r"] = self.player_position["r"] - 10*DEGREE_IN_RADIANTS
self.player_position["r"] = self.player_position["r"] + PLAYER_ROTATION_SPEED
# Compute deltax and deltay based on player direction
player_delta_x = math.cos(self.player_position["r"]) * PLAYER_SPEED
@ -135,11 +134,13 @@ class Main:
# Casts rays for raycasting
playerAngle = self.player_position["r"]
# Cast 60 rays from -30° to +30° (60° viewing angle)
for i in range(60):
rayAngle = playerAngle + (i - 30)*DEGREE_IN_RADIANTS
# Cast 100 rays from -0,5 rads to +0,5 rads (about 60° viewing angle)
for i in range(100):
rayAngle = playerAngle + (i/100) - 0.5
if rayAngle < 0:
rayAngle = 2 * math.pi + rayAngle
rayAngle = math.pi * 2 + rayAngle
if rayAngle > math.pi * 2:
rayAngle = rayAngle - math.pi * 2
# Check horizontal lines
dof = 0 # Depth of field
@ -192,13 +193,13 @@ class Main:
rayY = self.player_position["y"] + DOF * MAP_SCALE
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
# Looking left
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
# Looking right
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
@ -226,7 +227,7 @@ class Main:
shortestDist = horizDist
# Draw rays in 2D view
sdl2.ext.draw.line(self.mapSurface, sdl2.ext.Color(i*4,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))
# ------ Draw 3D view ------