Casting 60 rays
This commit is contained in:
parent
904d817203
commit
ec11c67f06
22
raycaster.py
22
raycaster.py
@ -19,6 +19,8 @@ RAY_LENGTH = 100
|
|||||||
MAP_SCALE = 80
|
MAP_SCALE = 80
|
||||||
DOF = 8 # Depth Of Field
|
DOF = 8 # Depth Of Field
|
||||||
|
|
||||||
|
DEGREE_IN_RADIANTS = 0.01745329
|
||||||
|
|
||||||
MAP = [
|
MAP = [
|
||||||
1, 1, 1, 1, 1, 1, 1, 1,
|
1, 1, 1, 1, 1, 1, 1, 1,
|
||||||
1, 0, 0, 0, 1, 0, 0, 1,
|
1, 0, 0, 0, 1, 0, 0, 1,
|
||||||
@ -60,9 +62,9 @@ class Main:
|
|||||||
if event.type == sdl2.SDL_KEYDOWN:
|
if event.type == sdl2.SDL_KEYDOWN:
|
||||||
# Rotate player
|
# Rotate player
|
||||||
if event.key.keysym.sym == sdl2.SDLK_LEFT:
|
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:
|
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
|
# Compute deltax and deltay based on player direction
|
||||||
player_delta_x = math.cos(self.player_position["r"]) * PLAYER_SPEED
|
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
|
"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
|
||||||
@ -121,14 +125,18 @@ class Main:
|
|||||||
|
|
||||||
def drawRays(self):
|
def drawRays(self):
|
||||||
# Casts rays for raycasting
|
# Casts rays for raycasting
|
||||||
rayAngle = self.player_position["r"]
|
playerAngle = self.player_position["r"]
|
||||||
for r in range(1):
|
|
||||||
|
# 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
|
# 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:
|
||||||
# Looking left or right (ray will never intersect parallel lines)
|
# Looking left or right (ray will never intersect parallel lines)
|
||||||
rayY = self.player_position["y"]
|
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
|
dof = DOF # Set depth of field to maximum to avoid unneeded checks
|
||||||
elif rayAngle > math.pi:
|
elif rayAngle > math.pi:
|
||||||
# Looking up
|
# Looking up
|
||||||
@ -150,7 +158,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
|
||||||
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
|
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
|
||||||
@ -171,7 +179,7 @@ class Main:
|
|||||||
#if rayAngle == 0 or rayAngle == math.pi:
|
#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["x"]
|
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
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user