Skip to content

Commit

Permalink
interpolation is now more robust with values > 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancollingwood committed Oct 17, 2018
1 parent 4bdeffe commit becbcd3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 47 deletions.
8 changes: 1 addition & 7 deletions GEOM.py
Expand Up @@ -29,8 +29,7 @@ def sort_atan(x):
if x.type == 'end':
SETTINGS.end_angle = theta

# if a consumer requires abs they can abs
#theta = abs(theta)
theta = abs(theta)

return theta

Expand Down Expand Up @@ -101,11 +100,6 @@ def sin_radians(angle):
def straight_line_distance(xpos, ypos):
return math.sqrt(xpos * xpos + ypos * ypos)

x = point.map_pos[0] + point.map_pos[1]
y = end.map_pos[0] + end.map_pos[1]
h = abs(x - y)
return h


@jit(nopython=True)
def max_grid_distance(map_pos_a, map_pos_b):
Expand Down
6 changes: 6 additions & 0 deletions MAP.py
Expand Up @@ -131,6 +131,12 @@ def calculate_render_visible(self):
self.distance = self.get_dist(SETTINGS.player_rect.center)
return self.distance <= SETTINGS.render * SETTINGS.tile_size * 1.2

def get_dist_from_map_pos(self, map_pos):
xpos = (self.map_pos[0] - map_pos[0]) * SETTINGS.tile_size
ypos = (map_pos[1] - self.map_pos[1]) * SETTINGS.tile_size

return straight_line_distance(xpos, ypos)

def get_dist(self, pos):
xpos = self.rect.center[0] - pos[0]
ypos = pos[1] - self.rect.center[1]
Expand Down
77 changes: 37 additions & 40 deletions RAYCAST.py
Expand Up @@ -5,7 +5,7 @@
import pygame
import math

from GEOM import get_camera_plane_for_angle, tan_radians, cos_radians, max_grid_distance
from GEOM import tan_radians, cos_radians, max_grid_distance
from EVENTS import EVENT_RAY_CASTING_CALCULATED
import SETTINGS

Expand Down Expand Up @@ -176,7 +176,7 @@ def calculate(self):
if ray_number % self.interpolation != 0:
# need the start and end for filling in blanks
# need the middle for player interaction
if ray_number != middle_ray_number:
if ray_number not in [1, middle_ray_number, self.res]:
self.next_zbuffer[ray_number] = None
continue

Expand Down Expand Up @@ -220,54 +220,48 @@ def get_ray_values_for_angle(self, angle):
return values

def fill_in_interpolate_gaps(self, ray_values):
left_slice = None
right_slice = None
anchor_slice = None
max_index_next_zbuffer = len(self.next_zbuffer) - 1

for i, zbuffer_slice in enumerate(self.next_zbuffer):
if zbuffer_slice is not None:
if left_slice is None and right_slice is None:
left_slice = zbuffer_slice
elif left_slice is not None and right_slice is None:
right_slice = zbuffer_slice
elif left_slice is not None and right_slice is not None:
right_slice = left_slice
left_slice = zbuffer_slice
anchor_slice = zbuffer_slice
else:
search_tiles = None
degree = ray_values[i][1]
ray_number = ray_values[i][0]
self.beta = ray_values[i][3]
new_ray = None

if left_slice is not None:

# this covers the case of the very first
# gap where we haven't found a pair
if right_slice is None:
next_right = abs(self.interpolation - i) + 1
right_slice = self.next_zbuffer[next_right]

if right_slice is not None:
search_tiles = [tile for tile in SETTINGS.rendered_tiles if
tile.map_pos in [left_slice.map_pos, right_slice.map_pos]]
else:
search_tiles = [tile for tile in SETTINGS.rendered_tiles if
tile.map_pos == left_slice.map_pos]

if len(search_tiles) > 0:
new_ray = self.cast(SETTINGS.player_rect, degree, ray_number, search_tiles = search_tiles)
else:
print("tile was rendered by in rednered?", left_slice.map_pos)

if new_ray is None:
new_ray = self.cast(SETTINGS.player_rect, degree, ray_number)
new_slice = None

if anchor_slice is not None:
next_right = i + abs(self.interpolation - i)
if next_right > max_index_next_zbuffer:
next_right = max_index_next_zbuffer
next_anchor_slice = self.next_zbuffer[next_right]

if next_anchor_slice is not None:
if anchor_slice.map_pos == next_anchor_slice.map_pos:
search_tiles = [tile for tile in SETTINGS.rendered_tiles if
tile.map_pos == anchor_slice.map_pos]
else:
distance = (max_grid_distance(anchor_slice.map_pos, next_anchor_slice.map_pos) * self.tile_size)
search_tiles = [tile for tile in SETTINGS.rendered_tiles if
tile.get_dist_from_map_pos(anchor_slice.map_pos) <= distance]

if search_tiles is not None:
new_slice = self.cast(SETTINGS.player_rect, degree, ray_number, search_tiles = search_tiles)

if new_slice is None:
new_slice = self.cast(SETTINGS.player_rect, degree, ray_number)
#print("recast", ray_number)
else:
# tile corners aren't shading correctly
if right_slice is not None:
if left_slice.vh == right_slice.vh != new_ray.vh:
new_ray.vh = left_slice.vh
if next_anchor_slice is not None:
if anchor_slice.vh == next_anchor_slice.vh != new_slice.vh:
new_slice.vh = anchor_slice.vh

self.next_zbuffer[i] = new_ray
self.next_zbuffer[i] = new_slice
anchor_slice = new_slice


def find_offset(self, position, ray_number, angle, tile, hv):
Expand Down Expand Up @@ -314,6 +308,8 @@ def cast(self, player_rect, angle, ray_number, search_tiles = None, ray_origrin
if search_tiles is None:
search_tiles = SETTINGS.rendered_tiles

# search_tiles = sorted(search_tiles, key=lambda x: (x.type, x.atan, x.distance))

# TODO could probably precompute this in get_ray_values_for_angle
cos_radians_angle = cos_radians(angle)
tan_radians_angle = tan_radians(angle)
Expand Down Expand Up @@ -350,7 +346,8 @@ def cast(self, player_rect, angle, ray_number, search_tiles = None, ray_origrin
]
else:
reduced_search_tiles = search_tiles

#reduced_search_tiles = sorted(search_tiles, key=lambda x: (x.type, x.atan, x.distance))

for tile in reduced_search_tiles:

if self.check_hit(V_hit, H_hit, H_distance, V_distance, False):
Expand Down

0 comments on commit becbcd3

Please sign in to comment.