[Python] Rendering using Raycasting - Wall texture gets distorted when limited to specific...

Discussão em 'Python' iniciado por Stack, Fevereiro 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a Raycasting Renderer in pygame. so far I am doing good, however I could not find out why I'm having this problem. I have this code to limit the wall size in case the player gets too close to it. The problem is that the textures get distorted when i get close to them. What could be causing this? Here is my code for the raycaster:

    class RayCaster:
    def __init__(self, character, world):
    self.character = character
    self.world = world

    # render
    self.test = pygame.image.load("testassets/1.png").convert_alpha()

    # ray casting
    self.fov = math.pi / 3
    self.half_fov = self.fov / 2
    self.num_of_rays = comms.display.get_width() // 6
    self.depth = 80

    # pseudo 3d projection
    self.screen_distance = comms.display.get_width() / math.tan(self.half_fov)
    self.scale = comms.display.get_width() / self.num_of_rays

    self.rays = []
    current_angle = self.character.angle - self.half_fov
    for ray in range(self.num_of_rays):
    self.rays.append(Ray(self.depth, character, world, offset=current_angle))
    current_angle += self.fov / self.num_of_rays

    def render_walls(self, offset, proj_height, horizon, ray_num):
    if proj_height < comms.display.get_height():
    wall_column = self.test.subsurface(
    offset * (self.test.get_width() - self.scale), 0, self.scale, self.test.get_height()
    )
    wall_column = pygame.transform.smoothscale(wall_column, (self.scale, proj_height))
    wall_pos = (ray_num * self.scale, horizon - proj_height // 2)
    else:
    texture_height = self.test.get_height() * comms.display.get_height() / proj_height
    wall_column = self.test.subsurface(
    offset * (self.test.get_width() - self.scale),
    (self.test.get_height() // 2) - (texture_height // 2),
    self.scale, texture_height
    )
    wall_column = pygame.transform.scale(wall_column, (self.scale, comms.display.get_height()))
    wall_pos = (ray_num * self.scale, horizon - texture_height // 2)

    return wall_column, wall_pos

    def update(self):
    for ray_num, ray in enumerate(self.rays):
    x, y, depth, offset = ray.update()

    depth *= math.cos(self.character.angle - ray.angle)
    proj_height = ((self.screen_distance * self.world.cell_size / 2) / (depth + 1e-6))
    brightness = max(255 * (0.9 ** (depth * 0.02)), 30)
    color = (int(brightness), int(brightness), int(brightness))
    horizon = (comms.display.get_height() // 2) - int(math.tan(self.character.pitch) * self.screen_distance)

    pygame.draw.rect(comms.display, color,
    (ray_num * self.scale, (horizon - proj_height // 2),
    self.scale, proj_height))

    wall_column, wall_pos = self.render_walls(offset, proj_height, horizon, ray_num)
    comms.display.blit(wall_column, wall_pos)


    This is a photo of the problem:

    [​IMG]

    Thanks in advance.

    Continue reading...

Compartilhe esta Página