-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathray_casting.py
More file actions
71 lines (59 loc) · 2.41 KB
/
Copy pathray_casting.py
File metadata and controls
71 lines (59 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pygame
from settings import *
from map import world_map, WORLD_WIDTH, WORLD_HEIGHT
from numba import njit
@njit(fastmath=True)
def mapping(a, b):
return (a // TILE) * TILE, (b // TILE) * TILE
@njit(fastmath=True)
def ray_casting(player_pos, player_angle, world_map):
casted_walls = []
ox, oy = player_pos
texture_v, texture_h = 1, 1
xm, ym = mapping(ox, oy)
cur_angle = player_angle - HALF_FOV #actual angulo
for ray in range(NUM_RAYS):
sin_a = math.sin(cur_angle)
cos_a = math.cos(cur_angle)
sin_a = sin_a if sin_a else 0.000001
cos_a = cos_a if cos_a else 0.000001
# verticals
x, dx = (xm + TILE, 1) if cos_a >= 0 else (xm, -1)
for i in range(0, WORLD_WIDTH, TILE):
depth_v = (x - ox) / cos_a
yv = oy + depth_v * sin_a
tile_v = mapping(x + dx, yv)
if tile_v in world_map:
texture_v = world_map[tile_v]
break
x += dx * TILE
# horizontals
y, dy = (ym + TILE, 1) if sin_a >= 0 else (ym, -1)
for i in range(0, WORLD_HEIGHT, TILE):
depth_h = (y - oy) / sin_a
xh = ox + depth_h * cos_a
tile_h = mapping(xh, y + dy)
if tile_h in world_map:
texture_h = world_map[tile_h]
break
y += dy * TILE
#print("pos X=",x,"Pos Y=",y)
# projection
depth, offset, texture = (depth_v, yv, texture_v) if depth_v < depth_h else (depth_h, xh, texture_h)
offset = int(offset) % TILE
depth *= math.cos(player_angle - cur_angle)
depth = max(depth, 0.00001)
proj_height = min(int(PROJ_COEFF / depth), PENTA_HEIGHT)
casted_walls.append((depth, offset, proj_height, texture))
cur_angle += DELTA_ANGLE
return casted_walls
def ray_casting_walls(player, textures):
casted_walls = ray_casting(player.pos, player.angle, world_map)
walls = []
for ray, casted_values in enumerate(casted_walls):
depth, offset, proj_height, texture = casted_values
wall_column = textures[texture].subsurface(offset * TEXTURE_SCALE, 0, TEXTURE_SCALE, TEXTURE_HEIGHT)
wall_column = pygame.transform.scale(wall_column, (SCALE, proj_height))
wall_pos = (ray * SCALE, HALF_HEIGHT - proj_height // 2)
walls.append((depth, wall_column, wall_pos))
return walls