diff --git a/raycaster.py b/raycaster.py index 88bee89..b428eb6 100755 --- a/raycaster.py +++ b/raycaster.py @@ -19,6 +19,8 @@ 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, @@ -60,9 +62,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"] - 0.1 + self.player_position["r"] = self.player_position["r"] - 5*DEGREE_IN_RADIANTS elif event.key.keysym.sym == sdl2.SDLK_RIGHT: - self.player_position["r"] = self.player_position["r"] + 0.1 + self.player_position["r"] = self.player_position["r"] + 5*DEGREE_IN_RADIANTS # Compute deltax and deltay based on player direction player_delta_x = math.cos(self.player_position["r"]) * PLAYER_SPEED @@ -108,6 +110,8 @@ class Main: "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 } + 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): # 2D map @@ -121,14 +125,18 @@ class Main: def drawRays(self): # Casts rays for raycasting - rayAngle = self.player_position["r"] - for r in range(1): + playerAngle = self.player_position["r"] + + # Cast 60 rays from -30° to +30° (60° viewing angle) + for r in range(60): + rayAngle = playerAngle - (r - 30)*DEGREE_IN_RADIANTS + # Check horizontal lines dof = 0 # Depth of field if rayAngle == 0 or rayAngle == math.pi: # Looking left or right (ray will never intersect parallel lines) rayY = self.player_position["y"] - rayX = self.player_position["x"] + rayX = self.player_position["x"] + DOF * MAP_SCALE dof = DOF # Set depth of field to maximum to avoid unneeded checks elif rayAngle > math.pi: # Looking up @@ -150,7 +158,7 @@ class Main: mapX = int(rayX / MAP_SCALE) mapY = int(rayY / MAP_SCALE) mapArrayPosition = mapY * MAP_SIZE + mapX - if mapArrayPosition < MAP_SIZE*MAP_SIZE and MAP[mapArrayPosition] != 0: + if mapArrayPosition >= 0 and 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 @@ -171,7 +179,7 @@ class Main: #if rayAngle == 0 or rayAngle == math.pi: # Looking up or down (ray will never intersect vertical lines) rayX = self.player_position["x"] - rayY = self.player_position["y"] + 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