-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraycasting.py
More file actions
251 lines (206 loc) · 10.5 KB
/
raycasting.py
File metadata and controls
251 lines (206 loc) · 10.5 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import pygame as pg
import math
from settings import *
class RayCasting:
def __init__(self, game):
self.game = game
self.ray_casting_result = []
self.objects_to_render = []
self.textures = self.game.object_renderer.wall_textures
# def get_objects_to_render(self):
# self.objects_to_render = []
# for ray, values in enumerate(self.ray_casting_result):
# depth, proj_height, texture, offset = values
# if depth >= (MAX_DEPTH - 3):
# #renderiza cor cinza para o limite de profundidade
# wall_column = pg.Surface((SCALE, proj_height), pg.SRCALPHA)
# wall_column.fill((0, 0, 0, 75)) #RGB para cinza, criar variavel de cor para cada mapa, combinando com o céu
# wall_pos = (ray * SCALE, HALF_HEIGHT - proj_height // 2)
# elif depth >= (MAX_DEPTH - 2):
# #renderiza cor cinza para o limite de profundidade
# wall_column = pg.Surface((SCALE, proj_height), pg.SRCALPHA)
# wall_column.fill((0, 0, 0, 125)) #RGB para cinza, criar variavel de cor para cada mapa, combinando com o céu
# wall_pos = (ray * SCALE, HALF_HEIGHT - proj_height // 2)
# elif depth >= (MAX_DEPTH):
# #renderiza cor cinza para o limite de profundidade
# wall_column = pg.Surface((SCALE, proj_height), pg.SRCALPHA)
# wall_column.fill((0, 0, 0, 255)) #RGB para cinza, criar variavel de cor para cada mapa, combinando com o céu
# wall_pos = (ray * SCALE, HALF_HEIGHT - proj_height // 2)
# else:
# if proj_height < HEIGHT:
# wall_column = self.textures[texture].subsurface(
# offset * (TEXTURE_SIZE - SCALE), 0, SCALE, TEXTURE_SIZE
# )
# wall_column = pg.transform.scale(wall_column, (SCALE, proj_height))
# wall_pos = (ray * SCALE, HALF_HEIGHT - proj_height // 2)
# else:
# texture_height = TEXTURE_SIZE * HEIGHT / proj_height
# wall_column = self.textures[texture].subsurface(
# offset * (TEXTURE_SIZE - SCALE), HALF_TEXTURE_SIZE - texture_height // 2,
# SCALE, texture_height
# )
# wall_column = pg.transform.scale(wall_column, (SCALE, HEIGHT))
# wall_pos = (ray * SCALE, 0)
# self.objects_to_render.append((depth, wall_column, wall_pos))
# def get_objects_to_render(self):
# self.objects_to_render = []
# for ray, values in enumerate(self.ray_casting_result):
# depth, proj_height, texture, offset = values
# # Calcular a intensidade da cor com base na profundidade
# if depth >= MAX_DEPTH:
# color_intensity = 0 # Preto total
# else:
# color_intensity = max(0, 255 - int((depth / MAX_DEPTH) * 255))
# if proj_height < HEIGHT:
# wall_column = self.textures[texture].subsurface(
# offset * (TEXTURE_SIZE - SCALE), 0, SCALE, TEXTURE_SIZE
# ).convert_alpha()
# wall_column = pg.transform.scale(wall_column, (SCALE, proj_height))
# wall_column.fill((color_intensity, color_intensity, color_intensity), special_flags=pg.BLEND_RGBA_MULT)
# wall_pos = (ray * SCALE, HALF_HEIGHT - proj_height // 2)
# else:
# texture_height = TEXTURE_SIZE * HEIGHT / proj_height
# wall_column = self.textures[texture].subsurface(
# offset * (TEXTURE_SIZE - SCALE), HALF_TEXTURE_SIZE - texture_height // 2,
# SCALE, texture_height
# ).convert_alpha()
# wall_column = pg.transform.scale(wall_column, (SCALE, HEIGHT))
# wall_column.fill((color_intensity, color_intensity, color_intensity), special_flags=pg.BLEND_RGBA_MULT)
# wall_pos = (ray * SCALE, 0)
# self.objects_to_render.append((depth, wall_column, wall_pos))
def get_objects_to_render(self):
self.objects_to_render = []
for ray, values in enumerate(self.ray_casting_result):
depth, proj_height, texture, offset = values
# Calcular a intensidade da cor com base na profundidade
if depth >= MAX_DEPTH:
color_intensity = 0 # Preto total
else:
color_intensity = max(0, 255 - int((depth / MAX_DEPTH) * 255))
if proj_height < HEIGHT:
wall_column = self.textures[texture].subsurface(
offset * (TEXTURE_SIZE - SCALE), 0, SCALE, TEXTURE_SIZE
).convert_alpha()
wall_column = pg.transform.scale(wall_column, (SCALE, proj_height))
wall_column.fill((color_intensity, color_intensity, color_intensity), special_flags=pg.BLEND_RGBA_MULT)
wall_pos = (ray * SCALE, HALF_HEIGHT - proj_height // 2)
else:
texture_height = TEXTURE_SIZE * HEIGHT / proj_height
wall_column = self.textures[texture].subsurface(
offset * (TEXTURE_SIZE - SCALE), HALF_TEXTURE_SIZE - texture_height // 2,
SCALE, texture_height
).convert_alpha()
wall_column = pg.transform.scale(wall_column, (SCALE, HEIGHT))
wall_column.fill((color_intensity, color_intensity, color_intensity), special_flags=pg.BLEND_RGBA_MULT)
wall_pos = (ray * SCALE, 0)
self.objects_to_render.append((depth, wall_column, wall_pos))
def ray_cast(self):
self.ray_casting_result = []
#coordinator of the player on the map
ox, oy = self.game.player.pos
#coordinators of the player tile on the map
x_map, y_map = self.game.player.map_pos
texture_vert, texture_hor = 1, 1
ray_angle = self.game.player.angle - HALF_FOV + 0.0001
for ray in range(NUM_RAYS):
sin_a = math.sin(ray_angle)
cos_a = math.cos(ray_angle)
#horizontals
y_hor, dy = (y_map + 1, 1) if sin_a > 0 else (y_map - 1e-6, -1)
depth_hor = (y_hor - oy) / sin_a
x_hor = ox + depth_hor * cos_a
delta_depth = dy / sin_a
dx = delta_depth * cos_a
for i in range(MAX_DEPTH):
tile_hor = int(x_hor), int(y_hor)
if tile_hor in self.game.map.world_map:
texture_hor = self.game.map.world_map[tile_hor]
break
x_hor += dx
y_hor += dy
depth_hor += delta_depth
#verticals
x_vert, dx = (x_map + 1, 1) if cos_a > 0 else (x_map - 1e-6, -1)
depth_vert = (x_vert - ox) / cos_a
y_vert = oy + depth_vert * sin_a
delta_depth = dx / cos_a
dy = delta_depth * sin_a
for i in range(MAX_DEPTH):
tile_vert = int(x_vert), int(y_vert)
if tile_vert in self.game.map.world_map:
texture_vert = self.game.map.world_map[tile_vert]
break
x_vert += dx
y_vert += dy
depth_vert += delta_depth
#depth
if depth_vert < depth_hor:
depth, texture = depth_vert, texture_vert
y_vert %= 1
offset = y_vert if cos_a > 0 else (1 - y_vert)
else:
depth, texture = depth_hor, texture_hor
x_hor %= 1
offset = (1 - x_hor) if sin_a > 0 else x_hor
#remove fishbowl effect
depth *= math.cos(self.game.player.angle - ray_angle)
#projection
proj_height = SCREEN_DIST / (depth + 0.0001)
#ray casting result
self.ray_casting_result.append((depth, proj_height, texture, offset))
ray_angle += DELTA_ANGLE
# def ray_cast(self):
# self.ray_casting_result = []
# ox, oy = self.game.player.pos
# x_map, y_map = self.game.player.map_pos
# ray_angle = self.game.player.angle - HALF_FOV + 0.0001
# for ray in range(NUM_RAYS):
# sin_a = math.sin(ray_angle)
# cos_a = math.cos(ray_angle)
# # Horizontais
# y_hor, dy = (y_map + 1, 1) if sin_a > 0 else (y_map - 1e-6, -1)
# depth_hor = (y_hor - oy) / sin_a
# x_hor = ox + depth_hor * cos_a
# delta_depth = dy / sin_a
# dx = delta_depth * cos_a
# for i in range(MAX_DEPTH):
# tile_hor = int(x_hor), int(y_hor)
# if tile_hor in self.game.map.world_map:
# texture_hor = self.game.map.world_map[tile_hor]
# break
# x_hor += dx
# y_hor += dy
# depth_hor += delta_depth
# # Verticais
# x_vert, dx = (x_map + 1, 1) if cos_a > 0 else (x_map - 1e-6, -1)
# depth_vert = (x_vert - ox) / cos_a
# y_vert = oy + depth_vert * sin_a
# delta_depth = dx / cos_a
# dy = delta_depth * sin_a
# for i in range(MAX_DEPTH):
# tile_vert = int(x_vert), int(y_vert)
# if tile_vert in self.game.map.world_map:
# texture_vert = self.game.map.world_map[tile_vert]
# break
# x_vert += dx
# y_vert += dy
# depth_vert += delta_depth
# # Escolher a menor profundidade
# if depth_vert < depth_hor:
# depth, texture = depth_vert, texture_vert
# y_vert %= 1
# offset = y_vert if cos_a > 0 else (1 - y_vert)
# else:
# depth, texture = depth_hor, texture_hor
# x_hor %= 1
# offset = (1 - x_hor) if sin_a > 0 else x_hor
# # Corrigir distorção da perspectiva
# depth *= math.cos(self.game.player.angle - ray_angle)
# # Calcular a altura da projeção
# proj_height = SCREEN_DIST / (depth + 0.0001)
# # Adicionar resultados do ray casting
# self.ray_casting_result.append((depth, proj_height, texture, offset))
# ray_angle += DELTA_ANGLE
def update(self):
self.ray_cast()
self.get_objects_to_render()